MS SQL Server: storing mm-dd--year hh-mm-ss am/pm kills the time of datetime











up vote
1
down vote

favorite












I receiving over API a json with datetimes formatting like:
December 4, 2018 11:10:00 AM (so it is like: mm-dd--year hh-mm-ss am/pm)
I added a datetime row to the table in my MS SQL Server 2016 and import the data. I expected that the datetime displayed like 2018-12-04 11:10:00.000 but on Import process the SQL Server kills the time part entries. So only 2018-12-04 00:00:00.000 is stored. I checked Google and stackoverflow but found the reason for that.



Any tips?





Edit



I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table.










share|improve this question




















  • 7




    ... and import the data <= You need to include how you are importing the data.
    – Igor
    Nov 8 at 11:24








  • 3




    What's the type of the column you've inserted the data into? What code are you using for the import? What code are you using when extracting/displaying the data?
    – JohnLBevan
    Nov 8 at 11:27










  • Hi Guys, sorry to miss that Import point. I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table. The column type is datetime as written.
    – TimoC
    Nov 8 at 12:00












  • In the future please edit your question to add the additional details instead of putting them in a comment. Then it becomes more visible to others who might not read the comments.
    – Igor
    Nov 8 at 13:12










  • Have you looked at this previous answer yet? stackoverflow.com/a/1135756/1260204
    – Igor
    Nov 8 at 13:15















up vote
1
down vote

favorite












I receiving over API a json with datetimes formatting like:
December 4, 2018 11:10:00 AM (so it is like: mm-dd--year hh-mm-ss am/pm)
I added a datetime row to the table in my MS SQL Server 2016 and import the data. I expected that the datetime displayed like 2018-12-04 11:10:00.000 but on Import process the SQL Server kills the time part entries. So only 2018-12-04 00:00:00.000 is stored. I checked Google and stackoverflow but found the reason for that.



Any tips?





Edit



I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table.










share|improve this question




















  • 7




    ... and import the data <= You need to include how you are importing the data.
    – Igor
    Nov 8 at 11:24








  • 3




    What's the type of the column you've inserted the data into? What code are you using for the import? What code are you using when extracting/displaying the data?
    – JohnLBevan
    Nov 8 at 11:27










  • Hi Guys, sorry to miss that Import point. I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table. The column type is datetime as written.
    – TimoC
    Nov 8 at 12:00












  • In the future please edit your question to add the additional details instead of putting them in a comment. Then it becomes more visible to others who might not read the comments.
    – Igor
    Nov 8 at 13:12










  • Have you looked at this previous answer yet? stackoverflow.com/a/1135756/1260204
    – Igor
    Nov 8 at 13:15













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I receiving over API a json with datetimes formatting like:
December 4, 2018 11:10:00 AM (so it is like: mm-dd--year hh-mm-ss am/pm)
I added a datetime row to the table in my MS SQL Server 2016 and import the data. I expected that the datetime displayed like 2018-12-04 11:10:00.000 but on Import process the SQL Server kills the time part entries. So only 2018-12-04 00:00:00.000 is stored. I checked Google and stackoverflow but found the reason for that.



Any tips?





Edit



I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table.










share|improve this question















I receiving over API a json with datetimes formatting like:
December 4, 2018 11:10:00 AM (so it is like: mm-dd--year hh-mm-ss am/pm)
I added a datetime row to the table in my MS SQL Server 2016 and import the data. I expected that the datetime displayed like 2018-12-04 11:10:00.000 but on Import process the SQL Server kills the time part entries. So only 2018-12-04 00:00:00.000 is stored. I checked Google and stackoverflow but found the reason for that.



Any tips?





Edit



I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table.







sql sql-server tsql sql-server-2016






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 13:54









Rahul Neekhra

427423




427423










asked Nov 8 at 11:22









TimoC

9319




9319








  • 7




    ... and import the data <= You need to include how you are importing the data.
    – Igor
    Nov 8 at 11:24








  • 3




    What's the type of the column you've inserted the data into? What code are you using for the import? What code are you using when extracting/displaying the data?
    – JohnLBevan
    Nov 8 at 11:27










  • Hi Guys, sorry to miss that Import point. I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table. The column type is datetime as written.
    – TimoC
    Nov 8 at 12:00












  • In the future please edit your question to add the additional details instead of putting them in a comment. Then it becomes more visible to others who might not read the comments.
    – Igor
    Nov 8 at 13:12










  • Have you looked at this previous answer yet? stackoverflow.com/a/1135756/1260204
    – Igor
    Nov 8 at 13:15














  • 7




    ... and import the data <= You need to include how you are importing the data.
    – Igor
    Nov 8 at 11:24








  • 3




    What's the type of the column you've inserted the data into? What code are you using for the import? What code are you using when extracting/displaying the data?
    – JohnLBevan
    Nov 8 at 11:27










  • Hi Guys, sorry to miss that Import point. I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table. The column type is datetime as written.
    – TimoC
    Nov 8 at 12:00












  • In the future please edit your question to add the additional details instead of putting them in a comment. Then it becomes more visible to others who might not read the comments.
    – Igor
    Nov 8 at 13:12










  • Have you looked at this previous answer yet? stackoverflow.com/a/1135756/1260204
    – Igor
    Nov 8 at 13:15








7




7




... and import the data <= You need to include how you are importing the data.
– Igor
Nov 8 at 11:24






... and import the data <= You need to include how you are importing the data.
– Igor
Nov 8 at 11:24






3




3




What's the type of the column you've inserted the data into? What code are you using for the import? What code are you using when extracting/displaying the data?
– JohnLBevan
Nov 8 at 11:27




What's the type of the column you've inserted the data into? What code are you using for the import? What code are you using when extracting/displaying the data?
– JohnLBevan
Nov 8 at 11:27












Hi Guys, sorry to miss that Import point. I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table. The column type is datetime as written.
– TimoC
Nov 8 at 12:00






Hi Guys, sorry to miss that Import point. I stored the json Infos in a variable and Import the data via @json/OPENJSON and merge it via tsql "merge" into the table. The column type is datetime as written.
– TimoC
Nov 8 at 12:00














In the future please edit your question to add the additional details instead of putting them in a comment. Then it becomes more visible to others who might not read the comments.
– Igor
Nov 8 at 13:12




In the future please edit your question to add the additional details instead of putting them in a comment. Then it becomes more visible to others who might not read the comments.
– Igor
Nov 8 at 13:12












Have you looked at this previous answer yet? stackoverflow.com/a/1135756/1260204
– Igor
Nov 8 at 13:15




Have you looked at this previous answer yet? stackoverflow.com/a/1135756/1260204
– Igor
Nov 8 at 13:15












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The datetime format in use is a cummulation of all the never-do-this in one package... It is language specific, culture specific and non-standard.



But - lucky guy - you are very close to an existing standard format in SQL-Server. Read about CAST() and CONVERT() and the third parameter..



Try this:



SET LANGUAGE ENGLISH;
DECLARE @YourDateTimeString VARCHAR(100)='December 4 2018, 11:10:00 AM';

SELECT CONVERT(DATETIME,REPLACE(@YourDateTimeString,',',''),109);


Make sure to use a language which knows "December". In my culture (German) this was "Dezember" and the script would fail...



It seems to be enough to replace the comma in order to get into format 109...



If there is any chance to change the input, try to convince the sending side to use a standard format, best was ISO8601 (yyyyMMddTHH:mm:ss).



UPDATE



While testing this I had the comma in the wrong spot. With the string given by you this works even without any replacements:



SET LANGUAGE ENGLISH;
DECLARE @YourDateTimeString VARCHAR(100)='December 4, 2018 11:10:00 AM';

SELECT CONVERT(DATETIME,@YourDateTimeString,109);





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',
    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%2f53206749%2fms-sql-server-storing-mm-dd-year-hh-mm-ss-am-pm-kills-the-time-of-datetime%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    The datetime format in use is a cummulation of all the never-do-this in one package... It is language specific, culture specific and non-standard.



    But - lucky guy - you are very close to an existing standard format in SQL-Server. Read about CAST() and CONVERT() and the third parameter..



    Try this:



    SET LANGUAGE ENGLISH;
    DECLARE @YourDateTimeString VARCHAR(100)='December 4 2018, 11:10:00 AM';

    SELECT CONVERT(DATETIME,REPLACE(@YourDateTimeString,',',''),109);


    Make sure to use a language which knows "December". In my culture (German) this was "Dezember" and the script would fail...



    It seems to be enough to replace the comma in order to get into format 109...



    If there is any chance to change the input, try to convince the sending side to use a standard format, best was ISO8601 (yyyyMMddTHH:mm:ss).



    UPDATE



    While testing this I had the comma in the wrong spot. With the string given by you this works even without any replacements:



    SET LANGUAGE ENGLISH;
    DECLARE @YourDateTimeString VARCHAR(100)='December 4, 2018 11:10:00 AM';

    SELECT CONVERT(DATETIME,@YourDateTimeString,109);





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      The datetime format in use is a cummulation of all the never-do-this in one package... It is language specific, culture specific and non-standard.



      But - lucky guy - you are very close to an existing standard format in SQL-Server. Read about CAST() and CONVERT() and the third parameter..



      Try this:



      SET LANGUAGE ENGLISH;
      DECLARE @YourDateTimeString VARCHAR(100)='December 4 2018, 11:10:00 AM';

      SELECT CONVERT(DATETIME,REPLACE(@YourDateTimeString,',',''),109);


      Make sure to use a language which knows "December". In my culture (German) this was "Dezember" and the script would fail...



      It seems to be enough to replace the comma in order to get into format 109...



      If there is any chance to change the input, try to convince the sending side to use a standard format, best was ISO8601 (yyyyMMddTHH:mm:ss).



      UPDATE



      While testing this I had the comma in the wrong spot. With the string given by you this works even without any replacements:



      SET LANGUAGE ENGLISH;
      DECLARE @YourDateTimeString VARCHAR(100)='December 4, 2018 11:10:00 AM';

      SELECT CONVERT(DATETIME,@YourDateTimeString,109);





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        The datetime format in use is a cummulation of all the never-do-this in one package... It is language specific, culture specific and non-standard.



        But - lucky guy - you are very close to an existing standard format in SQL-Server. Read about CAST() and CONVERT() and the third parameter..



        Try this:



        SET LANGUAGE ENGLISH;
        DECLARE @YourDateTimeString VARCHAR(100)='December 4 2018, 11:10:00 AM';

        SELECT CONVERT(DATETIME,REPLACE(@YourDateTimeString,',',''),109);


        Make sure to use a language which knows "December". In my culture (German) this was "Dezember" and the script would fail...



        It seems to be enough to replace the comma in order to get into format 109...



        If there is any chance to change the input, try to convince the sending side to use a standard format, best was ISO8601 (yyyyMMddTHH:mm:ss).



        UPDATE



        While testing this I had the comma in the wrong spot. With the string given by you this works even without any replacements:



        SET LANGUAGE ENGLISH;
        DECLARE @YourDateTimeString VARCHAR(100)='December 4, 2018 11:10:00 AM';

        SELECT CONVERT(DATETIME,@YourDateTimeString,109);





        share|improve this answer












        The datetime format in use is a cummulation of all the never-do-this in one package... It is language specific, culture specific and non-standard.



        But - lucky guy - you are very close to an existing standard format in SQL-Server. Read about CAST() and CONVERT() and the third parameter..



        Try this:



        SET LANGUAGE ENGLISH;
        DECLARE @YourDateTimeString VARCHAR(100)='December 4 2018, 11:10:00 AM';

        SELECT CONVERT(DATETIME,REPLACE(@YourDateTimeString,',',''),109);


        Make sure to use a language which knows "December". In my culture (German) this was "Dezember" and the script would fail...



        It seems to be enough to replace the comma in order to get into format 109...



        If there is any chance to change the input, try to convince the sending side to use a standard format, best was ISO8601 (yyyyMMddTHH:mm:ss).



        UPDATE



        While testing this I had the comma in the wrong spot. With the string given by you this works even without any replacements:



        SET LANGUAGE ENGLISH;
        DECLARE @YourDateTimeString VARCHAR(100)='December 4, 2018 11:10:00 AM';

        SELECT CONVERT(DATETIME,@YourDateTimeString,109);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 14:29









        Shnugo

        47.2k72465




        47.2k72465






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206749%2fms-sql-server-storing-mm-dd-year-hh-mm-ss-am-pm-kills-the-time-of-datetime%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)