SQL Server : varchar to Date Conversion/Casting Error










2














If I use this



SELECT CONVERT(DATE, '26/03/2014', 101)


I get an error:




Conversion failed when converting date and/or time from character string.




But if I use this



SELECT CONVERT(DATE, '26/03/2014', 103)


There's no error and this is the result returned:



2014-03-26


I don't understand why the first code is not working, as far as I searched and understand is that 101 is for US date and 103 is for UK/French Date.










share|improve this question























  • What version of SQL Server are you using?
    – Radu Gheorghiu
    Apr 4 '14 at 10:35










  • @RaduGheorghiu .. Im using MS SQL 2012
    – maitiest
    Apr 8 '14 at 4:52















2














If I use this



SELECT CONVERT(DATE, '26/03/2014', 101)


I get an error:




Conversion failed when converting date and/or time from character string.




But if I use this



SELECT CONVERT(DATE, '26/03/2014', 103)


There's no error and this is the result returned:



2014-03-26


I don't understand why the first code is not working, as far as I searched and understand is that 101 is for US date and 103 is for UK/French Date.










share|improve this question























  • What version of SQL Server are you using?
    – Radu Gheorghiu
    Apr 4 '14 at 10:35










  • @RaduGheorghiu .. Im using MS SQL 2012
    – maitiest
    Apr 8 '14 at 4:52













2












2








2







If I use this



SELECT CONVERT(DATE, '26/03/2014', 101)


I get an error:




Conversion failed when converting date and/or time from character string.




But if I use this



SELECT CONVERT(DATE, '26/03/2014', 103)


There's no error and this is the result returned:



2014-03-26


I don't understand why the first code is not working, as far as I searched and understand is that 101 is for US date and 103 is for UK/French Date.










share|improve this question















If I use this



SELECT CONVERT(DATE, '26/03/2014', 101)


I get an error:




Conversion failed when converting date and/or time from character string.




But if I use this



SELECT CONVERT(DATE, '26/03/2014', 103)


There's no error and this is the result returned:



2014-03-26


I don't understand why the first code is not working, as far as I searched and understand is that 101 is for US date and 103 is for UK/French Date.







sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 4 '14 at 10:33









marc_s

571k12811031251




571k12811031251










asked Apr 4 '14 at 10:28









maitiest

74311




74311











  • What version of SQL Server are you using?
    – Radu Gheorghiu
    Apr 4 '14 at 10:35










  • @RaduGheorghiu .. Im using MS SQL 2012
    – maitiest
    Apr 8 '14 at 4:52
















  • What version of SQL Server are you using?
    – Radu Gheorghiu
    Apr 4 '14 at 10:35










  • @RaduGheorghiu .. Im using MS SQL 2012
    – maitiest
    Apr 8 '14 at 4:52















What version of SQL Server are you using?
– Radu Gheorghiu
Apr 4 '14 at 10:35




What version of SQL Server are you using?
– Radu Gheorghiu
Apr 4 '14 at 10:35












@RaduGheorghiu .. Im using MS SQL 2012
– maitiest
Apr 8 '14 at 4:52




@RaduGheorghiu .. Im using MS SQL 2012
– maitiest
Apr 8 '14 at 4:52












4 Answers
4






active

oldest

votes


















4














This:



SELECT CONVERT(DATE, '26/03/2014', 101)


will be interpreted in the US way (mm/dd/yyyy) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).



This however:



SELECT CONVERT(DATE, '26/03/2014', 103)


will be interpreted the European way (dd/mm/yyyy): the 26th day of the 3rd month (March) of 2014.



You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT here



If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD or in your case:



SELECT CAST('20140326' AS DATE)


will always work, no matter what language/regional settings you have






share|improve this answer




















  • so would it always assume the format 'YYYYMMDD'?
    – Tauseef
    Apr 4 '14 at 10:39






  • 1




    @Tauseef: yes - that's what the ISO-8601 format defines
    – marc_s
    Apr 4 '14 at 10:40


















3














Thats becuase of the format specifier(101) which you are using.



101 is mm/dd/yyyy


So 26 cannot be a month. Hence resulting in error.



103 is dd/mm/yy


And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.






share|improve this answer






























    1














    The convert signature is as follows
    CONVERT(data_type(length),expression,style)



    for the date conversion, the styles are as follows



    101 mm/dd/yy USA

    103 dd/mm/yy British/French


    more formats here http://www.w3schools.com/sql/func_convert.asp






    share|improve this answer




























      0














      Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.



      Here's a nice link with more info from MVP Tibor Karaszi:



      http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes






      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%2f22860229%2fsql-server-varchar-to-date-conversion-casting-error%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        This:



        SELECT CONVERT(DATE, '26/03/2014', 101)


        will be interpreted in the US way (mm/dd/yyyy) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).



        This however:



        SELECT CONVERT(DATE, '26/03/2014', 103)


        will be interpreted the European way (dd/mm/yyyy): the 26th day of the 3rd month (March) of 2014.



        You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT here



        If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD or in your case:



        SELECT CAST('20140326' AS DATE)


        will always work, no matter what language/regional settings you have






        share|improve this answer




















        • so would it always assume the format 'YYYYMMDD'?
          – Tauseef
          Apr 4 '14 at 10:39






        • 1




          @Tauseef: yes - that's what the ISO-8601 format defines
          – marc_s
          Apr 4 '14 at 10:40















        4














        This:



        SELECT CONVERT(DATE, '26/03/2014', 101)


        will be interpreted in the US way (mm/dd/yyyy) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).



        This however:



        SELECT CONVERT(DATE, '26/03/2014', 103)


        will be interpreted the European way (dd/mm/yyyy): the 26th day of the 3rd month (March) of 2014.



        You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT here



        If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD or in your case:



        SELECT CAST('20140326' AS DATE)


        will always work, no matter what language/regional settings you have






        share|improve this answer




















        • so would it always assume the format 'YYYYMMDD'?
          – Tauseef
          Apr 4 '14 at 10:39






        • 1




          @Tauseef: yes - that's what the ISO-8601 format defines
          – marc_s
          Apr 4 '14 at 10:40













        4












        4








        4






        This:



        SELECT CONVERT(DATE, '26/03/2014', 101)


        will be interpreted in the US way (mm/dd/yyyy) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).



        This however:



        SELECT CONVERT(DATE, '26/03/2014', 103)


        will be interpreted the European way (dd/mm/yyyy): the 26th day of the 3rd month (March) of 2014.



        You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT here



        If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD or in your case:



        SELECT CAST('20140326' AS DATE)


        will always work, no matter what language/regional settings you have






        share|improve this answer












        This:



        SELECT CONVERT(DATE, '26/03/2014', 101)


        will be interpreted in the US way (mm/dd/yyyy) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).



        This however:



        SELECT CONVERT(DATE, '26/03/2014', 103)


        will be interpreted the European way (dd/mm/yyyy): the 26th day of the 3rd month (March) of 2014.



        You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT here



        If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD or in your case:



        SELECT CAST('20140326' AS DATE)


        will always work, no matter what language/regional settings you have







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 4 '14 at 10:35









        marc_s

        571k12811031251




        571k12811031251











        • so would it always assume the format 'YYYYMMDD'?
          – Tauseef
          Apr 4 '14 at 10:39






        • 1




          @Tauseef: yes - that's what the ISO-8601 format defines
          – marc_s
          Apr 4 '14 at 10:40
















        • so would it always assume the format 'YYYYMMDD'?
          – Tauseef
          Apr 4 '14 at 10:39






        • 1




          @Tauseef: yes - that's what the ISO-8601 format defines
          – marc_s
          Apr 4 '14 at 10:40















        so would it always assume the format 'YYYYMMDD'?
        – Tauseef
        Apr 4 '14 at 10:39




        so would it always assume the format 'YYYYMMDD'?
        – Tauseef
        Apr 4 '14 at 10:39




        1




        1




        @Tauseef: yes - that's what the ISO-8601 format defines
        – marc_s
        Apr 4 '14 at 10:40




        @Tauseef: yes - that's what the ISO-8601 format defines
        – marc_s
        Apr 4 '14 at 10:40













        3














        Thats becuase of the format specifier(101) which you are using.



        101 is mm/dd/yyyy


        So 26 cannot be a month. Hence resulting in error.



        103 is dd/mm/yy


        And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.






        share|improve this answer



























          3














          Thats becuase of the format specifier(101) which you are using.



          101 is mm/dd/yyyy


          So 26 cannot be a month. Hence resulting in error.



          103 is dd/mm/yy


          And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.






          share|improve this answer

























            3












            3








            3






            Thats becuase of the format specifier(101) which you are using.



            101 is mm/dd/yyyy


            So 26 cannot be a month. Hence resulting in error.



            103 is dd/mm/yy


            And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.






            share|improve this answer














            Thats becuase of the format specifier(101) which you are using.



            101 is mm/dd/yyyy


            So 26 cannot be a month. Hence resulting in error.



            103 is dd/mm/yy


            And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 4 '14 at 10:57









            Tauseef

            1,5331215




            1,5331215










            answered Apr 4 '14 at 10:34









            Rahul Tripathi

            126k21162233




            126k21162233





















                1














                The convert signature is as follows
                CONVERT(data_type(length),expression,style)



                for the date conversion, the styles are as follows



                101 mm/dd/yy USA

                103 dd/mm/yy British/French


                more formats here http://www.w3schools.com/sql/func_convert.asp






                share|improve this answer

























                  1














                  The convert signature is as follows
                  CONVERT(data_type(length),expression,style)



                  for the date conversion, the styles are as follows



                  101 mm/dd/yy USA

                  103 dd/mm/yy British/French


                  more formats here http://www.w3schools.com/sql/func_convert.asp






                  share|improve this answer























                    1












                    1








                    1






                    The convert signature is as follows
                    CONVERT(data_type(length),expression,style)



                    for the date conversion, the styles are as follows



                    101 mm/dd/yy USA

                    103 dd/mm/yy British/French


                    more formats here http://www.w3schools.com/sql/func_convert.asp






                    share|improve this answer












                    The convert signature is as follows
                    CONVERT(data_type(length),expression,style)



                    for the date conversion, the styles are as follows



                    101 mm/dd/yy USA

                    103 dd/mm/yy British/French


                    more formats here http://www.w3schools.com/sql/func_convert.asp







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 4 '14 at 10:36









                    Tauseef

                    1,5331215




                    1,5331215





















                        0














                        Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.



                        Here's a nice link with more info from MVP Tibor Karaszi:



                        http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes






                        share|improve this answer



























                          0














                          Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.



                          Here's a nice link with more info from MVP Tibor Karaszi:



                          http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes






                          share|improve this answer

























                            0












                            0








                            0






                            Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.



                            Here's a nice link with more info from MVP Tibor Karaszi:



                            http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes






                            share|improve this answer














                            Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.



                            Here's a nice link with more info from MVP Tibor Karaszi:



                            http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 11 '18 at 21:21









                            Aaron Bertrand

                            207k27362404




                            207k27362404










                            answered Apr 4 '14 at 10:39









                            dean

                            8,26911319




                            8,26911319



























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f22860229%2fsql-server-varchar-to-date-conversion-casting-error%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