DateTime condition in where clause doesnt work correctly - sql

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

Related

date time conversion seems off [duplicate]

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

Date format data Base mm/dd/yyyy

I have a csv file which I inserted into a database using SSIS, this file contains dates in this format MM / DD / YYYY hh: ss: mmm Am / Pm which I inserted under in varchar because if I do a transformation in date it deforms them. here is my data:
ARRIVAL_DATE_TIME
9/25/2021 11:40:32 AM
9/25/2021 11:41:46 AM
9/25/2021 11:55:35 AM
9/25/2021 11:56:15 AM
9/25/2021 11:56:37 AM
9/25/2021 11:56:48 AM
9/25/2021 12:12:25 PM
10/8/2021 8:05:12 AM
10/8/2021 8:11:05 AM
I would like to display my dates in order, my dates are between 09/25/2021 and 10/19/2021,
I am writing this sql code under sql server:
SELECT
convert(varchar, [ARRIVAL_DATE_TIME], 9) a
FROM [MAKS].[dbo].[Masks] order by a
but the results displayed are not good it confuses between days and months. this is what i get
a
10/1/2021 1:00:00 AM
10/1/2021 1:00:06 PM
10/1/2021 1:00:31 AM
10/1/2021 1:00:52 AM
10/1/2021 1:01:06 PM
9/26/2021 9:16:41 AM
9/26/2021 9:19:43 AM
9/26/2021 9:18:28 AM
9/26/2021 9:16:57 AM
anyone have an idea please
Style 101 is the correct datetime style to use here and it should be datetime to get a correct ordering on datetime:
Select convert(datetime, Arrival_date_time, 101) myDateTime
from myTable
order by myDateTime;
DBFiddle demo

Splitting unrecognized timestamp column into separate date and time columns

I have a problem splitting column name timedate and I want to split it into time and column date.
TimeDate
00:00:00 (01/01/2018)
01:00:00 (01/01/2018)
02:00:00 (01/01/2018)
I tried using pandas datetime method but it won't work
pd.to_datetime(df["Time / Date."]).dt.date
Got this error
('Unknown string format:', '00:00:00 (01/01/2018)')
Any idea how should I approach this problem?
Looks like you can just pass the format:
pd.to_datetime(df['TimeDate'], format='%H:%M:%S (%m/%d/%Y)').dt.date
Output:
0 2018-01-01
1 2018-01-01
2 2018-01-01
Name: TimeDate, dtype: object

Date and Time Format Conversion in SQL Server 2012

I am using SQL Server 2012 sp1 .I have a table column with the following Date Time values.
BLDCHKDT
-----------------------
2013-06-19 00:00:00.000
2013-07-22 00:00:00.000
2013-08-21 00:00:00.000
2013-09-20 00:00:00.000
2013-11-18 00:00:00.000
I would like to retrieve the date and Time in the following formats:
Date: 19062013
Time: 00000000
Is it possible? I have reviewed the SQL Server help documentation for the FORMAT, CAST and CONVERT functions and I can’t seem to get any headway.
So far I have attempted the following conversions:
N.B Please note that I am converting to Date time to string to facilitate a flat file export.
--Retrieving Date
SELECT [DATE-BLDCHKD] = CONVERT (VARCHAR (20), BLDCHKDT, 112)
FROM TABLEA
DATE-BLDCHKD
--------------------
20130619
20130722
20130821
20130920
20131118
--Retrieving Time
SELECT [TIME-BLDCHKD] = CONVERT (VARCHAR (20), BLDCHKDT, 24)
FROM TABLEA
TIME-BLDCHKD
--------------------
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
I appreciate your insights. Thank you in advance.
Since you're using SQL Server 2012 you can use the FORMAT() function:
SELECT FORMAT(BLDCHKDT,'ddMMyyyy')
, FORMAT(BLDCHKDT,'hhmmssfff')
You can do it this way:
declare #value datetime = getdate()
select
replace(convert(varchar(20), #value, 104), '.', '') date_part,
left(replace(convert(varchar(20), #value, 114), ':', ''), 8) time_part
returns 23032014, 17174466 for 2014-03-23 17:17:44.660
Use 104th format for date part:
select replace (convert (varchar(10), #value, 104), '.', '') as date_part

how to convert string to datetime using sql in sqlite

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