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.
sql sql-server tsql sql-server-2016
|
show 1 more comment
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.
sql sql-server tsql sql-server-2016
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
|
show 1 more comment
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.
sql sql-server tsql sql-server-2016
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
sql sql-server tsql sql-server-2016
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
|
show 1 more comment
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
|
show 1 more comment
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);
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 8 at 14:29
Shnugo
47.2k72465
47.2k72465
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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