Date Formatting

  • I want to get a date in the following format ex- 14-09-2009 should be returned in the format 14th September 2009. Can nay one tell me how?

  • Where do you want to convert the date format - SSRS or Database?

    If the latter I strongly recommend against it. Date formatting should be done at the front end side (e.g. SSRS), especially, if the target format cannot be achieved using the standard CONVERT function.

    In the scenario you described a concatenation of various CONVERT functions together with DATENAME function would be required.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • In the front end can u tell me the logic how to do it

  • jsheikabdullah (9/14/2009)


    In the front end can u tell me the logic how to do it

    For what type of front end?



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • In vb.net

  • Hi,

    try this

    Declare @RESULT datetime

    set @RESULT = getdate()

    --

    select (cast(day(@RESULT) as varchar)+

    (case when (right(day(@RESULT),1) >= 4)

    or(right(day(@RESULT),1)= 0)

    or((left(day(@RESULT),1) = 1)and len(day(@RESULT))= 2) then 'th'

    when right(day(@RESULT),1) = 1 then 'st'

    when right(day(@RESULT),1) = 2 then 'nd'

    when right(day(@RESULT),1) = 3 then 'rd'

    end)+

    space(1)+

    datename(mm,@RESULT)+

    space(1)+

    cast(year(@RESULT) as varchar))

  • thanks it worked but shocking to know that the date format which is almost used in all forums for displaying is not supported by default in SQL.

  • Well the point is that SQL Server is the back-end and nicely formatting things is the job of the front-end. It's just not what SQL was designed for.

    It's also beneficial in other ways to pass the data to the front end as the proper type (a date) rather than a string so you can re-sort it efficiently from the client end.

    If you're using VB.net, it's pretty straight forward, example below:

    Dim idate As Date

    idate = "2009-01-01"

    MsgBox(idate.ToString("dd MMMM yyyy"))

    Adding the "th", "rd" etc isn't natively supported as far as I'm aware, but there are plenty of examples for building a vb.net function to do this on the web

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply