-1

I have the following query in which I am trying to order dates in British format using SQL in a SSRS package for a parameter

SELECT DISTINCT  convert(varchar, date, 103) AS  Date
,convert(varchar, date, 103) AS Date Name


  FROM [Server].[dbo].[date_Table] 

  WHERE ([date] >= '01-Jan-2018' AND [date]  <= CURRENT_TIMESTAMP)


union 
select '0', 'All Dates'

order by convert(varchar, date, 103)asc

Column [date] is of type datetime.

All the dates appear not to order by correctly.

Any ideas team why?

please help

1

1 Answer 1

1

Format is one thing and order is another. You can format a date in any format you like but for ordering, it just makes no sense to "order in British format".

I guess that the DISTINCT and conversion to format 103 is used to get only the distict dates with the actual values of the [date] column have a time part as well. In that case, I suggest you cast to DATE type and use GROUP BY instead:

SELECT
    convert(varchar, CAST(d.[date] AS date), 103)
        AS date_in_british_format
FROM
    [Server].[dbo].[date_Table] AS d
WHERE
      d.[date] >= '2018-01-01'
  AND d.[date] <= CURRENT_TIMESTAMP
GROUP BY
    CAST(d.[date] AS date)
ORDER BY
    CAST(d.[date] AS date) ;
2
  • this works, but the issue now is with the union bit, I need the all dates filter to work for my app, please help
    – pete
    Commented Feb 6, 2019 at 15:19
  • 1
    @pete: '0' is not a valid date, use NULL instead, e.g. select null, 'All Dates'
    – user1822
    Commented Feb 12, 2019 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.