convert a varchar yyyymmdd to yyyy-mm-dd

  • I have a stored proc which accepts the parameter.

    parameter is in varchar type.

    e.g.

    sd varchar(8) and the value enterd by user is like 20101011.

    I need to change the input into 2010-10-11

  • The major question is: WHY?

    If you need to deal with date values, use the DATETIME instead of a varchar. It makes it a lot easier to deal with.

    But to answer your original question:

    SELECT STUFF(STUFF('20101011',7,0,'-'),5,0,'-') should do it...



    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]

  • If you are using the parameter to insert into a datetime column, there is ABSOLUTELY no need to convert the date value.

    Keep the date value in ISO YYYYMMDD date format and the insert will be unambigous.


    N 56°04'39.16"
    E 12°55'05.25"

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

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