Converting varchar(50) to date time

  • I've a table created in a database in SQL Server 2005 .A field having datatype varchar(50) which contains date (ex. 20071015) in yyyymmdd format.I want month and year(Oct 2007).Please any body let me know how to do this ASAP.

    Thanks VSP

  • Read about DATEPART. Or you can use the MONTH and YEAR functions. Try it out, and post again if there's anything in particular that you don't understand.

    John

  • SELECT STUFF(CONVERT(VARCHAR(11),CONVERT(datetime,'20071015',112),113),1,3,'')

    As a side note: "ASAP" is not really among the preferred phrases on this site... It's usually combined with the word "money" 😉

    Edit: two more issues:

    1) it's highly recommended to store values in a format that represents the "true meaning". A date should be stored in a datetime column and not as varchar()

    2) Formatting as you requested should be done at the presentation layer, if possible.



    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]

Viewing 3 posts - 1 through 2 (of 2 total)

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