Date Format 101 question

  • I would think that this should be simple, but I must be overlooking it.  I simply what to return a date format of  Jan04 from a date field. 

     

    Thanks.

    fryere

    fryere

  • select substring(convert(varchar(11), getdate(), 106), 4, 3) + substring(convert(varchar(11), getdate(), 106), 10, 2)

  • Thanks Allen,

    I had thought about converting it and then chopping it up, but I thought that there maybe something built in that I was overlooking.  At Least I won't have to look up how to convert and reassemble.  Thanks, it has saved me a lot of time.

     

    fryer 

    fryere

  • I would have done it this way just to cut down on typing - select left(datename(month,getdate()),3) + right(datepart(year,getdate()),2) . Does anyone know which performs better? my execution plan tests prove inconclusive.

  • As you can see there are many ways to do this, and here is another:

    select replace(convert(varchar(6), getdate(), 107), ' ', '')

  • Paul,

    Your Code actually returns the Month and Day instead of Month and Year.

    Thanks.

    fryere

    fryere

  • My mistake (I was reading your post date....4th of the month), try this instead then:

    select replace(right(convert(varchar, getdate(), 6), 6), ' ', '')

  • Another possible way to do this would be:

    select convert(char(3), getdate(), 107)+convert(char(2),getdate(),11)

    Any format that uses the two-year date as the first couple of numbers would work (2,11 or 12)

     

  • Don't forget that BOL (Books On Line) is your best friend!

    No shame in looking things up. =;o)

    Search for CONVERT to see all available styles 'out of the box' and which to use when building custom displays.

    =;o)

    /Kenneth

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

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