Converting Varchar to Date and date Comparison










0















I am converting two Date columns to find the most recent one



SELECT ISNULL(CONVERT(varchar(10),REPLACE('10-07-2015','/','-'), 103),'01-01-1900'),
ISNULL(CONVERT(varchar(10),REPLACE('10/7/2015','/','-'), 103),'01-01-1900'),
CASE
WHEN ISNULL(CONVERT(varchar,REPLACE('10-07-2015','/','-'), 103),'01-01-1900') = ISNULL(CONVERT(varchar,REPLACE('10/7/2015','/','-'), 103),'01-01-1900')
THEN '10-07-2015'
END


My issue is some dates missing leading Zero in Day or Month and comparison is giving false results. Is there a better way to handle this? Other issue is one column has date with '/' and other have with '-'



Currently case is only checking on '=' but will add more to get the most recent










share|improve this question



















  • 4





    Why on earth are you storing DATE values in a VARCHAR column. That is a really, really bad idea

    – a_horse_with_no_name
    Nov 14 '18 at 11:12






  • 1





    Types exist for a reason, you should use them. In this case the Date or DateTime2 type would be the most appropriate choice, not varchar. See also When to use VARCHAR and DATE/DATETIME

    – Igor
    Nov 14 '18 at 11:16












  • That's legacy data store and it's been like this always

    – InTheWorldOfCodingApplications
    Nov 14 '18 at 11:20







  • 1





    yes it's DDMMYYYY

    – InTheWorldOfCodingApplications
    Nov 14 '18 at 11:32















0















I am converting two Date columns to find the most recent one



SELECT ISNULL(CONVERT(varchar(10),REPLACE('10-07-2015','/','-'), 103),'01-01-1900'),
ISNULL(CONVERT(varchar(10),REPLACE('10/7/2015','/','-'), 103),'01-01-1900'),
CASE
WHEN ISNULL(CONVERT(varchar,REPLACE('10-07-2015','/','-'), 103),'01-01-1900') = ISNULL(CONVERT(varchar,REPLACE('10/7/2015','/','-'), 103),'01-01-1900')
THEN '10-07-2015'
END


My issue is some dates missing leading Zero in Day or Month and comparison is giving false results. Is there a better way to handle this? Other issue is one column has date with '/' and other have with '-'



Currently case is only checking on '=' but will add more to get the most recent










share|improve this question



















  • 4





    Why on earth are you storing DATE values in a VARCHAR column. That is a really, really bad idea

    – a_horse_with_no_name
    Nov 14 '18 at 11:12






  • 1





    Types exist for a reason, you should use them. In this case the Date or DateTime2 type would be the most appropriate choice, not varchar. See also When to use VARCHAR and DATE/DATETIME

    – Igor
    Nov 14 '18 at 11:16












  • That's legacy data store and it's been like this always

    – InTheWorldOfCodingApplications
    Nov 14 '18 at 11:20







  • 1





    yes it's DDMMYYYY

    – InTheWorldOfCodingApplications
    Nov 14 '18 at 11:32













0












0








0








I am converting two Date columns to find the most recent one



SELECT ISNULL(CONVERT(varchar(10),REPLACE('10-07-2015','/','-'), 103),'01-01-1900'),
ISNULL(CONVERT(varchar(10),REPLACE('10/7/2015','/','-'), 103),'01-01-1900'),
CASE
WHEN ISNULL(CONVERT(varchar,REPLACE('10-07-2015','/','-'), 103),'01-01-1900') = ISNULL(CONVERT(varchar,REPLACE('10/7/2015','/','-'), 103),'01-01-1900')
THEN '10-07-2015'
END


My issue is some dates missing leading Zero in Day or Month and comparison is giving false results. Is there a better way to handle this? Other issue is one column has date with '/' and other have with '-'



Currently case is only checking on '=' but will add more to get the most recent










share|improve this question
















I am converting two Date columns to find the most recent one



SELECT ISNULL(CONVERT(varchar(10),REPLACE('10-07-2015','/','-'), 103),'01-01-1900'),
ISNULL(CONVERT(varchar(10),REPLACE('10/7/2015','/','-'), 103),'01-01-1900'),
CASE
WHEN ISNULL(CONVERT(varchar,REPLACE('10-07-2015','/','-'), 103),'01-01-1900') = ISNULL(CONVERT(varchar,REPLACE('10/7/2015','/','-'), 103),'01-01-1900')
THEN '10-07-2015'
END


My issue is some dates missing leading Zero in Day or Month and comparison is giving false results. Is there a better way to handle this? Other issue is one column has date with '/' and other have with '-'



Currently case is only checking on '=' but will add more to get the most recent







sql sql-server sql-server-2012






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 11:25









Barbaros Özhan

14k71634




14k71634










asked Nov 14 '18 at 11:11









InTheWorldOfCodingApplicationsInTheWorldOfCodingApplications

89651952




89651952







  • 4





    Why on earth are you storing DATE values in a VARCHAR column. That is a really, really bad idea

    – a_horse_with_no_name
    Nov 14 '18 at 11:12






  • 1





    Types exist for a reason, you should use them. In this case the Date or DateTime2 type would be the most appropriate choice, not varchar. See also When to use VARCHAR and DATE/DATETIME

    – Igor
    Nov 14 '18 at 11:16












  • That's legacy data store and it's been like this always

    – InTheWorldOfCodingApplications
    Nov 14 '18 at 11:20







  • 1





    yes it's DDMMYYYY

    – InTheWorldOfCodingApplications
    Nov 14 '18 at 11:32












  • 4





    Why on earth are you storing DATE values in a VARCHAR column. That is a really, really bad idea

    – a_horse_with_no_name
    Nov 14 '18 at 11:12






  • 1





    Types exist for a reason, you should use them. In this case the Date or DateTime2 type would be the most appropriate choice, not varchar. See also When to use VARCHAR and DATE/DATETIME

    – Igor
    Nov 14 '18 at 11:16












  • That's legacy data store and it's been like this always

    – InTheWorldOfCodingApplications
    Nov 14 '18 at 11:20







  • 1





    yes it's DDMMYYYY

    – InTheWorldOfCodingApplications
    Nov 14 '18 at 11:32







4




4





Why on earth are you storing DATE values in a VARCHAR column. That is a really, really bad idea

– a_horse_with_no_name
Nov 14 '18 at 11:12





Why on earth are you storing DATE values in a VARCHAR column. That is a really, really bad idea

– a_horse_with_no_name
Nov 14 '18 at 11:12




1




1





Types exist for a reason, you should use them. In this case the Date or DateTime2 type would be the most appropriate choice, not varchar. See also When to use VARCHAR and DATE/DATETIME

– Igor
Nov 14 '18 at 11:16






Types exist for a reason, you should use them. In this case the Date or DateTime2 type would be the most appropriate choice, not varchar. See also When to use VARCHAR and DATE/DATETIME

– Igor
Nov 14 '18 at 11:16














That's legacy data store and it's been like this always

– InTheWorldOfCodingApplications
Nov 14 '18 at 11:20






That's legacy data store and it's been like this always

– InTheWorldOfCodingApplications
Nov 14 '18 at 11:20





1




1





yes it's DDMMYYYY

– InTheWorldOfCodingApplications
Nov 14 '18 at 11:32





yes it's DDMMYYYY

– InTheWorldOfCodingApplications
Nov 14 '18 at 11:32












1 Answer
1






active

oldest

votes


















3














You can just convert those 2 varchars to the DATE type, then compare them.



You can find the date/datetime styles here



For those DD/MM/YYYY datestamps the 103 style would fit.



And to calculate the most recent between them, just wrap it in a CASE.



Example snippet:



declare @T table (
id int identity(1,1) primary key,
datestamp1 varchar(10),
datestamp2 varchar(10)
);

insert into @T (datestamp1, datestamp2) values
('5/9/2018','17/9/2018')
,('9-10-2018','16-10-2018')
,('15-10-2018','13-10-2018')
;

SELECT *,
TRY_CONVERT(DATE, datestamp1, 103) as date1,
TRY_CONVERT(DATE, datestamp2, 103) as date2,
CASE
WHEN TRY_CONVERT(DATE, datestamp1, 103) >= TRY_CONVERT(DATE, datestamp2, 103) THEN datestamp1
WHEN TRY_CONVERT(DATE, datestamp2, 103) IS NULL THEN datestamp1
ELSE datestamp2
END AS MostRecentDatestamp
FROM @T;


Returns:



id datestamp1 datestamp2 date1 date2 MostRecentDatestamp
1 5/9/2018 17/9/2018 2018-09-05 2018-09-17 17/9/2018
2 9-10-2018 16-10-2018 2018-10-09 2018-10-16 16-10-2018
3 15-10-2018 13-10-2018 2018-10-15 2018-10-13 15-10-2018





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298859%2fconverting-varchar-to-date-and-date-comparison%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You can just convert those 2 varchars to the DATE type, then compare them.



    You can find the date/datetime styles here



    For those DD/MM/YYYY datestamps the 103 style would fit.



    And to calculate the most recent between them, just wrap it in a CASE.



    Example snippet:



    declare @T table (
    id int identity(1,1) primary key,
    datestamp1 varchar(10),
    datestamp2 varchar(10)
    );

    insert into @T (datestamp1, datestamp2) values
    ('5/9/2018','17/9/2018')
    ,('9-10-2018','16-10-2018')
    ,('15-10-2018','13-10-2018')
    ;

    SELECT *,
    TRY_CONVERT(DATE, datestamp1, 103) as date1,
    TRY_CONVERT(DATE, datestamp2, 103) as date2,
    CASE
    WHEN TRY_CONVERT(DATE, datestamp1, 103) >= TRY_CONVERT(DATE, datestamp2, 103) THEN datestamp1
    WHEN TRY_CONVERT(DATE, datestamp2, 103) IS NULL THEN datestamp1
    ELSE datestamp2
    END AS MostRecentDatestamp
    FROM @T;


    Returns:



    id datestamp1 datestamp2 date1 date2 MostRecentDatestamp
    1 5/9/2018 17/9/2018 2018-09-05 2018-09-17 17/9/2018
    2 9-10-2018 16-10-2018 2018-10-09 2018-10-16 16-10-2018
    3 15-10-2018 13-10-2018 2018-10-15 2018-10-13 15-10-2018





    share|improve this answer





























      3














      You can just convert those 2 varchars to the DATE type, then compare them.



      You can find the date/datetime styles here



      For those DD/MM/YYYY datestamps the 103 style would fit.



      And to calculate the most recent between them, just wrap it in a CASE.



      Example snippet:



      declare @T table (
      id int identity(1,1) primary key,
      datestamp1 varchar(10),
      datestamp2 varchar(10)
      );

      insert into @T (datestamp1, datestamp2) values
      ('5/9/2018','17/9/2018')
      ,('9-10-2018','16-10-2018')
      ,('15-10-2018','13-10-2018')
      ;

      SELECT *,
      TRY_CONVERT(DATE, datestamp1, 103) as date1,
      TRY_CONVERT(DATE, datestamp2, 103) as date2,
      CASE
      WHEN TRY_CONVERT(DATE, datestamp1, 103) >= TRY_CONVERT(DATE, datestamp2, 103) THEN datestamp1
      WHEN TRY_CONVERT(DATE, datestamp2, 103) IS NULL THEN datestamp1
      ELSE datestamp2
      END AS MostRecentDatestamp
      FROM @T;


      Returns:



      id datestamp1 datestamp2 date1 date2 MostRecentDatestamp
      1 5/9/2018 17/9/2018 2018-09-05 2018-09-17 17/9/2018
      2 9-10-2018 16-10-2018 2018-10-09 2018-10-16 16-10-2018
      3 15-10-2018 13-10-2018 2018-10-15 2018-10-13 15-10-2018





      share|improve this answer



























        3












        3








        3







        You can just convert those 2 varchars to the DATE type, then compare them.



        You can find the date/datetime styles here



        For those DD/MM/YYYY datestamps the 103 style would fit.



        And to calculate the most recent between them, just wrap it in a CASE.



        Example snippet:



        declare @T table (
        id int identity(1,1) primary key,
        datestamp1 varchar(10),
        datestamp2 varchar(10)
        );

        insert into @T (datestamp1, datestamp2) values
        ('5/9/2018','17/9/2018')
        ,('9-10-2018','16-10-2018')
        ,('15-10-2018','13-10-2018')
        ;

        SELECT *,
        TRY_CONVERT(DATE, datestamp1, 103) as date1,
        TRY_CONVERT(DATE, datestamp2, 103) as date2,
        CASE
        WHEN TRY_CONVERT(DATE, datestamp1, 103) >= TRY_CONVERT(DATE, datestamp2, 103) THEN datestamp1
        WHEN TRY_CONVERT(DATE, datestamp2, 103) IS NULL THEN datestamp1
        ELSE datestamp2
        END AS MostRecentDatestamp
        FROM @T;


        Returns:



        id datestamp1 datestamp2 date1 date2 MostRecentDatestamp
        1 5/9/2018 17/9/2018 2018-09-05 2018-09-17 17/9/2018
        2 9-10-2018 16-10-2018 2018-10-09 2018-10-16 16-10-2018
        3 15-10-2018 13-10-2018 2018-10-15 2018-10-13 15-10-2018





        share|improve this answer















        You can just convert those 2 varchars to the DATE type, then compare them.



        You can find the date/datetime styles here



        For those DD/MM/YYYY datestamps the 103 style would fit.



        And to calculate the most recent between them, just wrap it in a CASE.



        Example snippet:



        declare @T table (
        id int identity(1,1) primary key,
        datestamp1 varchar(10),
        datestamp2 varchar(10)
        );

        insert into @T (datestamp1, datestamp2) values
        ('5/9/2018','17/9/2018')
        ,('9-10-2018','16-10-2018')
        ,('15-10-2018','13-10-2018')
        ;

        SELECT *,
        TRY_CONVERT(DATE, datestamp1, 103) as date1,
        TRY_CONVERT(DATE, datestamp2, 103) as date2,
        CASE
        WHEN TRY_CONVERT(DATE, datestamp1, 103) >= TRY_CONVERT(DATE, datestamp2, 103) THEN datestamp1
        WHEN TRY_CONVERT(DATE, datestamp2, 103) IS NULL THEN datestamp1
        ELSE datestamp2
        END AS MostRecentDatestamp
        FROM @T;


        Returns:



        id datestamp1 datestamp2 date1 date2 MostRecentDatestamp
        1 5/9/2018 17/9/2018 2018-09-05 2018-09-17 17/9/2018
        2 9-10-2018 16-10-2018 2018-10-09 2018-10-16 16-10-2018
        3 15-10-2018 13-10-2018 2018-10-15 2018-10-13 15-10-2018






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 13:14

























        answered Nov 14 '18 at 11:28









        LukStormsLukStorms

        13.6k31734




        13.6k31734





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298859%2fconverting-varchar-to-date-and-date-comparison%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo