SQLite convert date - sqlite

I have a column of data type TEXT:
date
----
DD/MM/YYYY
but I want to convert all rows in the column to:
date
----
YYYY-MM-DD 00:00:00
(Yes, 00:00:00 for all rows)
Is there any way to do it in SQLite?

Use strftime.
strftime('%Y-%m-%d %H:%M:%S', date_str);
EDIT:
Yes, my first quess do not work. This one does, though:
SELECT
date,
substr(date,7,4)||'-'||substr(date,4,2)||'-'||substr(date,1,2)||' 00:00:00' as text_repr,
datetime(substr(date,7,4)||'-'||substr(date,4,2)||'-'||substr(date,1,2)||' 00:00:00') as datetime_repr
FROM
t
Simply put - You have to parse it on Your own, as stated here or here...

Related

SQLite Adding a Calculated Column

I have a table named "Combined_Bike_Ride" with the below sample data in SQLite
started_at
ended_at
27/05/2020 10:03
27/05/2020 10:16
25/05/2020 10:47
25/05/2020 11:05
I want to add a new calculated column "ride_length" where ride_length = (ended_at) - (started_at) in that table.
I used the below sql code but getting a syntax error near AS.
ALTER TABLE Combined_Bike_Ride ADD COLUMN ride_length AS (ended_at) - (started_at)
Am I missing a date calculation? Any help is much appreciated.
First you must change the format of your dates to YYYY-MM-DD hh:mm because this is the only text date format that you can use with SQLite's datetime functions if you want to perform operations such as calculation of time difference.
For this purpose you can use the function strftime().
With strftime('%s', datetimecolumn) you get the number of seconds between 1970-01-01 00:00:00 and the value of datetimecolumn
The correct syntax to add the new column is this:
ALTER TABLE Combined_Bike_Ride
ADD COLUMN ride_length
GENERATED ALWAYS AS ((strftime('%s', ended_at) - strftime('%s', started_at)) / 60)

SQlite Query select string date today or less than

Im having problem in query
i cannot get the proper result dates i want
I need to get all the date from today or less than date today
can you help me or give me right query to use
for example this is mytable
id(number) datestring(string)
1 02/06/2019
2 02/06/2019
3 02/14/2019
4 02/04/2019
5 03/17/2019
query i use: SELECT * FROM mytable WHERE datestring <= date('now')
expected result is
id(number) datestring(string)
1 02/06/2019
2 02/06/2019
4 02/04/2019
Thank you for helping
The issue you are encountering is that date('now') will return 2019-02-06 , as such as all rows in the table start with 0 and that this is less than 2.
You either need to convert on of the dates to be in the same format as the other or use the same format when storing the data.
SQLite itself has various date format that can be recognised and used and it is advisable to utilise one of these as reduces necessary complexity.
Time Strings A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
SQL As Understood By SQLite - Date And Time Functions
The following is a solution that could be used to convert the datestring column within the query :-
SELECT * FROM mytable WHERE substr(datestring,7,4)||'-'||substr(datestring,1,2)||'-'||substr(datestring,4,2) <= date('now');
The following could be used to convert the existing data to a recognised format :-
UPDATE mytable SET datestring = substr(datestring,7,4)||'-'||substr(datestring,1,2)||'-'||substr(datestring,4,2);
This could be followed by your original query
SELECT * FROM mytable WHERE datestring <= date('now');

Invalid date supplied in teradata--varchar date to timestamp(0)

I have a date column in source which is defined as varchar(255). I want to convert that to timestamp(0). I am using the below query:
SEL CAST(CAST( SERIND AS CHAR(10)) || ' 00:00:00' AS TIMESTAMP(0))
FROM DP_BOX.SOC1
WHERE SERIND ='20130518'
I am getting invalid timestamp here. Sample SERIND are
20170509
00000000
Can anyone please help me on this?
You can try CAST with correct format of TIMESTAMP(0)
SEL CAST(SERIND as TIMESTAMP(0) FORMAT 'YYYYMMDDHHMISS')
FROM DP_BOX.SOC1;
You're going to have to CAST() your way to a date format that is acceptable as a component of a timestamp in Teradata. (I think there may be a shortcut with math to get to a numeric format as well but I have to noodle on that one.)
Assuming SERIND is CHAR(8) to begin with. The first CAST() gets you to a DATE value with a compatible format. The second CAST() gets you a DATE with 10 character format so that when you concatenate it to the TIME portion of the timestamp the resulting character conversion of the DATE value is in the correct 10 character format to be explicitly CAST() into a TIMESTAMP value:
SELECT CAST(CAST(CAST(SERIND AS DATE FORMAT 'YYYYMMDD')
AS DATE FORMAT 'YYYY-MM-DD') || ' 00:00:00' AS TIMESTAMP(0))
FROM DP_BOX.SOC1
WHERE SERIND > '00000000'; -- Invalid Date value

How to deal with date in SQLite?

I have a table in Sqlite DB having two fields Id and Date (Date is of type Text).
I have stored a few dates in the table from c#. now i want to get the records matching specific day, month and year.
The query i have tried is:
select strftime('%m', Date) from testTbl Where id = 3;
also:
select Date(substr(Date, 0, 10)) as daa from testTbl Where id = 3;
but the result of these two quires is always null.. can anyone help me to sort this out?
Proposed (immediate) fix
Use the following select
select substr(Date, 0, 10) as daa from testTbl Where id = 3;
Cause of the issue
The problem (if you surround the above substr with a Date function) is that you're using a Text type that is not in the expected format
Time Strings
A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
Alternative (better) approach
Anyway IMHO, it would be better to create the column with a Date type and to insert values in the following way
insert into testTbl values (DateTime("2015-12-31"),3);
so that you'll be able to do
SELECT strftime('%m/%d/%Y',Date) from testTbl where id = 3;
or also
SELECT Date from testTbl where Date > DateTime('2016-01-01');
from C# the parameterized command would be similar to
"insert into testTbl values (DateTime(?),?);"
with a parameter value myDate.ToString("yyyy-MM-dd")
Anyway you can actually get the month string with substr(Date,0,2) and the year with substr(Date,5,4) with your current format. I'm simply suggesting an alternative that I would find more standard (using the built-in Date format)

Sort date in sqlite

I want to select the dates in ascending order. Dates are stored in dd-MMM-yy(02-Mar-12) format. Here is my query:
SELECT EventDate,Event,ID from EventCalenderTable Order By EventDate ASC
output is:
10-03-12
12-02-12
15-01-12
18-07-12
But the output should like:
15-01-12
12-02-12
10-03-12
18-07-12
Event Date is date datatype.
I have seen number of post about storing date in sql. I noticed that Convert function done the tricks in sql server. But how can I do this in Sqlite??
Thanks in advance.
SQLite only knows three date formats:
Text ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")
Real Julian day numbers since November 24, 4714 B.C
Integer number of seconds since 1970-01-01 00:00:00 UTC
SQLite does have five date/time functions for converting between formats.

Resources