how to convert string to datetime using sql in sqlite - sql

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

Related

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

DateTime condition in where clause doesnt work correctly

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

How to cast 12/01/2019 12:00:00 a.m. to DATETIME in BigQuery

I'm trying to convert a String (12/01/2019 12:00:00 a.m.) to DATETIME.
I have tried with:
PARSE_DATE('%e/%m/%Y %k:%M:%S %P', Fecha_Desc)
no result ... any help?
Two things. First, you need parse_datetime(). Second a.m. is not recognized. So, remove the spaces:
SELECT PARSE_DATETIME('%e/%m/%Y %k:%M:%S %p', replace('12/01/2019 12:00:00 a.m.', '.', ''))
If you really want a date, convert to a date after converting to a datetime -- or just convert the first 10 characters.

How can I convert to UTC from VARCHAR that's in AM/PM format?

I have a T-SQL VARCHAR column that looks like this:
APR-10-2018 11:33:39 AM
Dec-04-2017 11:14:24 AM
DEC-01-2017 07:24:54 PM
DEC-01-2017 07:20:28 PM
DEC-01-2017 07:19:52 PM
I want to convert it to UTC. I realize it will likely require using the CONVERT function. However, I have been unsuccessful in my attempts to do this. How?
Converting to a DateTime would be small matter. However, we have no idea what your timezone is, or more importantly, the timezone of the data
Example
Declare #YourTable table (SomeCol varchar(50))
Insert Into #YourTable values
('APR-10-2018 11:33:39 AM')
,('Dec-04-2017 11:14:24 AM')
,('DEC-01-2017 07:24:54 PM')
,('DEC-01-2017 07:20:28 PM')
,('DEC-01-2017 07:19:52 PM')
Select AsDateTime = try_convert(datetime,replace(SomeCol,'-',' '))
From #YourTable
Returns
AsDateTime
2018-04-10 11:33:39.000
2017-12-04 11:14:24.000
2017-12-01 19:24:54.000
2017-12-01 19:20:28.000
2017-12-01 19:19:52.000
You need to read up on cast() and convert(): https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
[Hint: you have to specify the desired date/time style).

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