Date Conversion

  • I am getting a data feed with date column of type integer.Format is mmddyyyy.

    Values are like : 01092015

    Now i want to convert this into yyyy-mm-dd format ie 2015-01-09

    Pls suggest the way. Thanks in advance

    Rahul Sahay

  • This is a naive way of doing it, but it works.

    declare @iDate int = 01092015

    select cast(substring(cast(@iDate+100000000 as char(11)), 6, 4) + substring(cast(@iDate+100000000 as char(11)), 2, 4) as date)

    __________________________________________________________________________________________________________
    How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks for the prompt reply!!

  • rahulsahay123 (1/8/2016)


    I am getting a data feed with date column of type integer.Format is mmddyyyy.

    Values are like : 01092015

    Now i want to convert this into yyyy-mm-dd format ie 2015-01-09

    Pls suggest the way. Thanks in advance

    Rahul Sahay

    If the date looks like this... 01092015 it's not an integer. An integer would drop the leading 0. Most likely just text.

    In which case, this is all you need...

    DECLARE @Date CHAR(8) = '01092015';

    SELECT CAST(SUBSTRING(@Date, 5, 4) + SUBSTRING(@Date, 1, 2) + SUBSTRING(@Date, 3, 2) AS DATE);

  • Jason A. Long (1/8/2016)


    rahulsahay123 (1/8/2016)


    I am getting a data feed with date column of type integer.Format is mmddyyyy.

    Values are like : 01092015

    Now i want to convert this into yyyy-mm-dd format ie 2015-01-09

    Pls suggest the way. Thanks in advance

    Rahul Sahay

    If the date looks like this... 01092015 it's not an integer. An integer would drop the leading 0. Most likely just text.

    In which case, this is all you need...

    D E C L A R E @Date CHAR(8) = '01092015'; -- can't post the declaration

    SELECT CAST(SUBSTRING(@Date, 5, 4) + SUBSTRING(@Date, 1, 2) + SUBSTRING(@Date, 3, 2) AS DATE);

    Or this:

    with sampledata as (

    select '01092015' as SampleDate

    )

    select SampleDate, cast(right(SampleDate,4) + left(SampleDate,4) as date) ConvertedDate from sampledata;

  • SELECT convert(varchar, getdate(), 102)

  • rahulsahay123 (1/8/2016)


    I am getting a data feed with date column of type integer.Format is mmddyyyy.

    Values are like : 01092015

    Now i want to convert this into yyyy-mm-dd format ie 2015-01-09

    Pls suggest the way. Thanks in advance

    Rahul Sahay

    Few things wrong here, numerical data types do not have leading zeros and there is no such thing as a date format for the date and datetime type data types. If you are importing the data then you would probably like to convert the integer to the data type date which is very simple, no need to do any kind of string conversion or manipulation.

    😎

    DECLARE @DATEINT INT = 9012016;

    SELECT

    DATEFROMPARTS(

    (@DATEINT % 10000 )

    ,(@DATEINT / 10000) % 100

    ,(@DATEINT / 1000000));

  • Eirikur Eiriksson (1/10/2016)


    rahulsahay123 (1/8/2016)


    I am getting a data feed with date column of type integer.Format is mmddyyyy.

    Values are like : 01092015

    Now i want to convert this into yyyy-mm-dd format ie 2015-01-09

    Pls suggest the way. Thanks in advance

    Rahul Sahay

    Few things wrong here, numerical data types do not have leading zeros and there is no such thing as a date format for the date and datetime type data types. If you are importing the data then you would probably like to convert the integer to the data type date which is very simple, no need to do any kind of string conversion or manipulation.

    😎

    D E C L A R E @DATEINT INT = 9012016; -- can't post declarations from work.

    SELECT

    DATEFROMPARTS(

    (@DATEINT % 10000 )

    ,(@DATEINT / 10000) % 100

    ,(@DATEINT / 1000000));

    Won't work in SQL Server 2008.

  • Perhaps you can use DATEPART?

  • johnwalker10 (1/8/2016)


    SELECT convert(varchar, getdate(), 102)

    How can this work? Reread the OP's post.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Jason A. Long (1/8/2016)


    rahulsahay123 (1/8/2016)


    I am getting a data feed with date column of type integer.Format is mmddyyyy.

    Values are like : 01092015

    Now i want to convert this into yyyy-mm-dd format ie 2015-01-09

    Pls suggest the way. Thanks in advance

    Rahul Sahay

    If the date looks like this... 01092015 it's not an integer. An integer would drop the leading 0. Most likely just text.

    In which case, this is all you need...

    DECLARE @Date CHAR(8) = '01092015';

    SELECT CAST(SUBSTRING(@Date, 5, 4) + SUBSTRING(@Date, 1, 2) + SUBSTRING(@Date, 3, 2) AS DATE);

    Looks like something I would do.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Lynn Pettis (1/8/2016)


    Jason A. Long (1/8/2016)


    rahulsahay123 (1/8/2016)


    I am getting a data feed with date column of type integer.Format is mmddyyyy.

    Values are like : 01092015

    Now i want to convert this into yyyy-mm-dd format ie 2015-01-09

    Pls suggest the way. Thanks in advance

    Rahul Sahay

    If the date looks like this... 01092015 it's not an integer. An integer would drop the leading 0. Most likely just text.

    In which case, this is all you need...

    D E C L A R E @Date CHAR(8) = '01092015'; -- can't post the declaration

    SELECT CAST(SUBSTRING(@Date, 5, 4) + SUBSTRING(@Date, 1, 2) + SUBSTRING(@Date, 3, 2) AS DATE);

    Or this:

    with sampledata as (

    select '01092015' as SampleDate

    )

    select SampleDate, cast(right(SampleDate,4) + left(SampleDate,4) as date) ConvertedDate from sampledata;

    Looks like something else I'd do.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • rahulsahay123 (1/8/2016)


    I am getting a data feed with date column of type integer.Format is mmddyyyy.

    Values are like : 01092015

    Now i want to convert this into yyyy-mm-dd format ie 2015-01-09

    Pls suggest the way. Thanks in advance

    Rahul Sahay

    Rather than MMDDYYYY, confirm if the data is instead using the pattern YYYYMMDD. That's the typical pattern for integer based date keys.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • Eric M Russell (1/11/2016)


    rahulsahay123 (1/8/2016)


    I am getting a data feed with date column of type integer.Format is mmddyyyy.

    Values are like : 01092015

    Now i want to convert this into yyyy-mm-dd format ie 2015-01-09

    Pls suggest the way. Thanks in advance

    Rahul Sahay

    Rather than MMDDYYYY, confirm if the data is instead using the pattern YYYYMMDD. That's the typical pattern for integer based date keys.

    I certainly hope the sample data given (01092015) is not meant to be YYYYMMDD.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

Viewing 15 posts - 1 through 15 (of 20 total)

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