SQL Query by date
Best Answer
-
Leigh_Kilday Member Ninja IT Monkey ✭✭✭✭
Just to clarify, are you adding the FROM statement that I (oddly) left off? I also missed a ')' in my transcription...
The following works for me:SELECT CAST(YEAR(FirstAssignedDate) as varchar(4)) + RIGHT('0' + CAST(MONTH(FirstAssignedDate) as varchar(2)), 2) as DateSort<p></p>
FROM WorkItem
6
Answers
I'd create a derived date from the datetime field for this.
E.g. Derived value for YYYYMM for sorting. You can add the day in the same way.
SELECT CAST(YEAR(FirstAssignedDate) as varchar(4) + RIGHT('0' + CAST(MONTH(FirstAssignedDate) as varchar(2)), 2) as DateSort
But from the SQL Chart Widget, this fails:
SELECT TOP 25 CAST(MONTH(wi.Created) AS varchar(2)) + CAST(DAY(wi.Created) AS varchar(2)) + CAST(YEAR(wi.Created) AS varchar(4)) AS DateSort
Note that per the example the wi.Field seems to be the required format. For example, this works:
SELECT TOP 25 wi.WorkItemId, wi.Created, wi.Title, ds.DisplayString Tier, wi.AssignedUser FROM [WorkItem] wi LEFT JOIN [Enumeration] enum ON enum.EnumerationID = wi.TierId INNER JOIN [DisplayString] ds ON ds.ElementID=enum.EnumerationID
Just to clarify, are you adding the FROM statement that I (oddly) left off? I also missed a ')' in my transcription...
SELECT CAST(YEAR(FirstAssignedDate) as varchar(4)) + RIGHT('0' + CAST(MONTH(FirstAssignedDate) as varchar(2)), 2) as DateSort<p></p>
FROM WorkItem