SQLite Adding a Calculated Column - sqlite

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)

Related

Calculating difference between datetime stamp in sqlite

I want to calculate the difference between two columns containing datetime stamps in db browser SQLite, I want the answers in minutes, and it keeps returning "Null". Please what could be the reason and how can I solve it?
I tried using this;
SELECT
started_at,
ended_at,
(strftime('%M','ended_at') - strftime('%M','started_at'))as duration
FROM citi1;
You have 'started_at' and 'ended_at' which are string literals and not identifiers and SQLite returns null when you use them in strftime().
But, even if you remove the single quotes you will not get the timestamp difference, because subtracting only the minutes parts of 2 timestamps does not return their difference.
For example, the difference that you would get for started_at = '2022-03-31 13:15:00' and ended_at = '2022-03-31 14:00:00' would be -15 (= 0 - 15).
Use strftime('%s', some_date) which returns the number of seconds since 1970-01-01 00:00:00 for both timestamps, subtract and divide by 60 to get the correct difference in minutes:
SELECT started_at, ended_at,
(strftime('%s', ended_at) - strftime('%s', started_at)) / 60 AS duration
FROM citi1;
See the demo.

Negative dates in sqllite database

I am working locally with an sqllite DB. I have imported some records from teradata where there was a date field in the format of 'YYYY-MM-DD'. When i imported the records the date switched from a date to a number. I know this is a feature of sqllite and that one can access it via date(sqllite_date) when selecting it in a where clause.
My problem is that the dates now appear to be a bit odd. For example the year appears to be negative.
Is there anyway to recover this to the correct format?
Below is an example of converting a number in the database into a date
SELECT date(18386)
# -4662-03-28
SELECT datetime('now')
# 2021-02-11 10:41:52
SELECT date(sqllite_date) FROM mydb
# Returns -4662-03-28
# Should return 2020-05-04
I am very new to this area so apologies if this is a basic question. Thank you very much for your time
In SQLite you can store dates as TEXT, REAL or INTEGER.
It seems that you stored the dates in a column with INTEGER or REAL affinity.
In this case, if you use the function date(), it considers a value like 18386 as a Julian day, meaning the number of days since noon in Greenwich on November 24, 4714 B.C.
This is why date(18386) returns 4662-03-28B.C.
But I suspect that the date values that you have are the number of days since '1970-01-01'.
In this case, 18386 days after '1970-01-01' is '2020-05-04'.
So you can get the dates in the format YYYY-MM-DD if you add the value of your column as days to '1970-01-01':
SELECT date('1970-01-01', datecolumn || ' day') FROM tablename
Or by transforming your date values to seconds and treat them as UNIX time (the number of seconds since '1970-01-01 00:00:00 UTC'):
SELECT date(datecolumn * 24 * 3600, 'unixepoch') FROM tablename
Replace datecolumn with the name of your column.

Creating datetime data on hour frequency by using date and hour column in integer type in Hive

I have a table including date column and hour column which is an integer type column varying from 0 to 24. I need to combine these two fields and create an hourly composite datetime field.
However, I was able to create that kind of variable by using || and cast. But I am unable to transform this code to Hive editor syntax. Can you help me with this problem
SQL Code:
CAST(CAST(CAST(DATE_OF_TRANSACTION AS FORMAT 'yyyy-mm-dd') AS VARCHAR(11))||' '||CAST(CAST( BasketHour AS FORMAT '99:') AS VARCHAR(10))||'00:00' AS TIMESTAMP(0)) Date_Time
Thank you very much
For example like this:
cast(concat(DATE_OF_TRANSACTION, ' ', lpad(BasketHour ,2,0),':00:00.0' ) as timestamp)

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');

SQLite convert date

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...

Resources