This question already has answers here:
When converting string to datetime the milliseconds precision is changing
(2 answers)
Closed 12 days ago.
I encounter situation like this where I try to add Millisecond into my DateTime.
In some cases, it didn't give me the exact number and it got rounded to something else.
Is it normal ?
CONVERT(varchar(125), dateadd(millisecond,ms,datetime), 21) as ZDateTimeMs
From datetime:
datetime values are rounded to increments of .000, .003, or .007 seconds, as shown in the following table.
User-specified value System stored value
01/01/98 23:59:59.999 1998-01-02 00:00:00.000
01/01/98 23:59:59.995
01/01/98 23:59:59.996
01/01/98 23:59:59.997
01/01/98 23:59:59.998 1998-01-01 23:59:59.997
01/01/98 23:59:59.992
01/01/98 23:59:59.993
01/01/98 23:59:59.994 1998-01-01 23:59:59.993
01/01/98 23:59:59.990
01/01/98 23:59:59.991 1998-01-01 23:59:59.990
Related
Tick to Ohlc 1hr data always start from 00:00:00 like 9:00:00 to 10:00:00. Even my data start from 9:15 it's not convert from 9:15 to 10:15....it just convert from 9:00:00 to 10:000
Solve this 1hr convert
I have row in my table with first column datetime type:
2021-11-01 08:51:56.123 102 296
When I use the select commands below, I get same result (this row):
select * from cmd where timestamp = convert(datetime, '2021-11-01 08:51:56.122')
select * from cmd where timestamp = convert(datetime, '2021-11-01 08:51:56.123')
select * from cmd where timestamp = convert(datetime, '2021-11-01 08:51:56.124')
I request that only the second command picks this line.
How to do it?
SQL Server is version 14
DATETIME has a precision of (about) .003 seconds. This means it can represent only every 3rd thousands of a second and everything else is rounded to increments of .000, .003, or .007 seconds, as shown in the following table.
User-specified value System stored value
01/01/98 23:59:59.999 1998-01-02 00:00:00.000
01/01/98 23:59:59.995
01/01/98 23:59:59.996
01/01/98 23:59:59.997
01/01/98 23:59:59.998 1998-01-01 23:59:59.997
01/01/98 23:59:59.992
01/01/98 23:59:59.993
01/01/98 23:59:59.994 1998-01-01 23:59:59.993
01/01/98 23:59:59.990
01/01/98 23:59:59.991 1998-01-01 23:59:59.990
You can use DATETIME2(3) instead of DATETIME for more precision.
For more details, please read https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql?view=sql-server-ver15
I'm using Pandas to read a .csv file that a 'Timestamp' date column in the format:
31/12/2016 00:00
I use the following line to convert it to a datetime64 dtype:
time = pd.to_datetime(df['Timestamp'])
The column has an entry corresponding to every 15mins for almost a year, and I've run into a problem when I want to plot more than 1 months worth.
Pandas seems to change the format from ISO to US upon reading (so YYYY:MM:DD to YYYY:DD:MM), so my plots have 30 day gaps whenever the datetime represents a new day. A plot of the first 5 days looks like:
This is the raw data in the file either side of the jump:
01/01/2017 23:45
02/01/2017 00:00
If I print the values being plotted (after reading) around the 1st jump, I get:
2017-01-01 23:45:00
2017-02-01 00:00:00
So is there a way to get pandas to read the dates properly?
Thanks!
You can specify a format parameter in pd.to_datetime to tell pandas how to parse the date exactly, which I suppose is what you need:
time = pd.to_datetime(df['Timestamp'], format='%d/%m/%Y %H:%M')
pd.to_datetime('02/01/2017 00:00')
#Timestamp('2017-02-01 00:00:00')
pd.to_datetime('02/01/2017 00:00', format='%d/%m/%Y %H:%M')
#Timestamp('2017-01-02 00:00:00')
Using Cloudera's Impala SQL, is there a way to truncate a timestamp by day?
i.e. go from:
2015-05-01 01:23:45 -> 2015-05-01 00:00:00
2015-05-01 12:34:56 -> 2015-05-01 00:00:00
2015-05-01 23:45:59 -> 2015-05-01 00:00:00
The default TRUNC options only seem to enable Week or Hour, not Day...
By my read of the tests, you should be able to pass DDD, DD, or J to TRUNC to get this behavior. I agree that the docs are not clear on this point though--I've filed an issue to get the documentation cleaned up.
how to convert string to datetime using sql in sqlite?
In my sqlite db, the puttime column is nvarchar type, and store data as null, empty string, 2013-10-23, 2013-10-23 13:30:25, 2013-10-24 9:30:22
I use the query below to convert , but the '2013-10-24 9:30:22' can't convert success, how to do it:
select puttime, datetime(puttime) from tb_news
result:
PutTime datetime(puttime)
2013-05-06 2013-05-06 00:00:00
2013-10-23 2013-10-23 00:00:00
2013-10-23 13:30:25 2013-10-23 13:30:25
2013-10-23 18:00:00 2013-10-23 18:00:00
2013-10-24 17:32:33 2013-10-24 17:32:33
2013-10-24 22:49:43 2013-10-24 22:49:43
2013-10-24 9:30:22
2013-10-25 00:01:33 2013-10-25 00:01:33
thanks.
Closest date/time format SQLite expects is YYYY-MM-DD HH:MM:SS.
Looking at your data, it seams that only deviation is YYYY-MM-DD H:MM:SS.
So, lets just add a 0 when needed:
SELECT puttime, DATETIME(
CASE SUBSTR(puttime, 14, 1) WHEN ':' THEN puttime -- Found ':' after HH
ELSE SUBSTR(puttime, 1, 11)||'0'||SUBSTR(puttime, 12) END
) FROM tb_news
SimpleDateFormat format = new SimpleDateFormat(""dd-MM-yyyy");
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
date = df2.format(format.parse(str));
try this one sir