SQLite, how to use expression in datetime modifier - sqlite

In short: I need to calculate the ending datetime given starting time and length in minutes.
I have a table with columns StartTime (type datetime) and LengthMinutes (type int). To calculate the ending time I would need some sql like this:
select datetime(StartTime, '+LengthMinutes minutes') from my_table;
How do I refer to the column LengthMinutes from within the modifier?
Edited: solved using dan04's suggestion. Thanks!

SELECT datetime(StartTime, '+' || LengthMinutes || ' minutes') FROM my_table;

select datetime(strftime('%s',StartTime)+lengthMinutes*60,'unixepoch') from my_table;

Related

how to select datetime in sqlite? please help me

I have value of column
datecolumn -> 2021-04-22T00:00:00.000
I want select using condition where like below:
SELECT * FROM 'tblDOISOAT' where datecolumn = strftime('%Y-%m-%d %H:%M:%S','2021-04-22 00:00:00')
I not get được value and i me met error 'Error: near "=": syntax error'. please help me
You have a typo, but your code works.
--------
dt
--------
2021-04-22T00:00:00.000
2021-04-23T01:00:00.000
--------
select strftime('%Y-%m-%dT%H:%M:%S.000', '2021-04-22 00:00:00') as 'datecode'
returns 2021-04-22T00:00:00.000
The query:
select dt from 't' where dt= strftime('%Y-%m-%dT%H:%M:%S.000', '2021-04-22 00:00:00')
returns the expected result: 2021-04-22T00:00:00.000
You forgot to include the correct formatting including T and .000 at the end.
remember that SQLite does not have a datetime data structure by default. You can read about SQLite data types here
If you are storing date/times as ISO-8601 text, you don't need to call strftime() to make comparisons. Just compare the 2 values:
select * from t1 where datecol = '2021-04-22T00:00:00.000';
The whole point of ISO-8601 strings is to make sure that date/time values compare lexicographically the same as temporally.

DateAdd function in Sqlite server with column name as input

select DATE(Orders.OrderDate,'10 DAY') from Orders. it will give the following result "1996-07-14"
When using column name directly instead of value in numeric it will give the empty result
select DATE(Orders.OrderDate,'Orders.OrderId DAY') from Orders.
What wrong with the above select query?
Try concatenating a string using the OrderId column:
SELECT DATE(Orders.OrderDate, '+' || Orders.OrderId || ' DAYS')
FROM Orders
You want a query of the form
SELECT DATE(Orders.OrderDate, '+7 DAYS') FROM Orders
and string concatenation will let you achieve that.

I want to combine date and time and compare with utc date

When I write this query
SELECT Convert(datetime,Convert(varchar,CAST(GETUTCDATE() AS DATE))+' '+
CONVERT(varchar, cast(meas_pain.datetime AS time))) FROM meas_pain
it works for me but when I use the same part in WHERE clause it gives error 'Conversion failed when converting date and/or time from character string.'
SELECT schedules.id
FROM meas_pain LEFT JOIN schedules ON schedules.id=meas_pain.schd_id
WHERE meas_pain.schd_id=9150 AND
Convert(datetime,(Convert(varchar,CAST(GETUTCDATE() AS DATE))+' '+
CONVERT(varchar, cast(meas_pain.datetime AS time)))) <
CONVERT(datetime,DATEADD(Minute,0,getutcdate()))
can anybody explain??
I am not sure why this error does not appear in your select statement, since I can reproduce the error using just
SET DATEFORMAT DMY;
SELECT CONVERT(DATETIME, CONVERT(VARCHAR,CAST(GETUTCDATE() AS DATE)))
Example of Error
You are relying on localised conversion settings, you should use explicit conversion, e.g.
SET DATEFORMAT DMY;
SELECT CONVERT(DATETIME, CONVERT(VARCHAR, CAST(GETUTCDATE() AS DATE), 111), 111)
By explicitly defining the date format to convert both to varchar and from varchar (111) you can avoid any implied conversions.
However, If your dates/times are stored as such there should be no need for all the conversion to and from varchar, this is just more chance for things to go wrong, and unnecessary work, you can simply add a time to a datetime. e.g.
DECLARE #Date1 DATETIME = DATEADD(HOUR, 1, GETUTCDATE()),
#Date2 DATETIME = DATEADD(DAY, 1, GETUTCDATE());
SELECT [Date1] = #Date1,
[Date2] = #Date2,
[Date1/Time2] = CAST(CAST(#Date1 AS DATE) AS DATETIME) +
CAST(#Date2 AS TIME);
From what I can gather from your query you are just trying to get results where the time of meas_pain.datetime is less that the current UTC time, regardless of date. So you should be able to simplify your query to just:
SELECT schedules.id
FROM meas_pain
LEFT JOIN schedules
ON schedules.id = meas_pain.schd_id
WHERE meas_pain.schd_id = 9150
AND CAST(meas_pain.[DateTime] AS TIME) < CAST(GETUTCDATE() AS TIME);
And remove further redundant conversions.
Simplified example on SQL Fiddle
ADENDUM
Apparently this time comparison is not what you are after (although it is what the query you have posted is doing), so I am assuming GETUTCDATE() is just for demonstration.
The conversion you are trying to perform is equivalent to this:
CAST(CAST(GETUTCDATE() AS DATE) AS DATETIME) + CAST(meas_pain.[DateTime] AS TIME)
Another example on SQL Fiddle using the above conversion

How to concat two column with '-' seperator in PL/SQL

I just want to concat two columns with seperator '-'.
These are the two columns, want to concat.
I am using this query to concat them
select concat(amt,endamt)as amount from mstcatrule
and it is giving me result this
But I Want that data of 2 columns should be sepearted by '-'
RESULT I WANT IS :
AMOUNT
0-0
100-99999999999
100-500
Alternative:
select amt || '-' || endamt as amount from mstcatrule;
Do it with two concats:
select concat(concat(amt, '-'), endamt) as amount from mstcatrule;
concat(amt,'-') concatenates the amt with the dash and the resulting string is concatenated with endamt.
Another way is to use double pipe.
select amt || '-' || endamt as amount from mstcatrule;
You may have to convert amt and endamt to varchar
In oracle this works for me! :D
select amt||'-'||endamt as amount from mstcatrule
Alternative you can use under query
select concat(amt,'-',endamt) as amount from mstcatrule;
A generic format for the query
Select concat(column1,'-',column2) as concatedCols from table_Name
For Postgresql only

SQLite: Combine Date Column & Time Column

i simply have two fields. dtStartTime and dtStartDate.
I want to do a query now which returns one combined field dtStart using SQLite
I have tried
SELECT (dtStartDate+dtStartTime) as dtStart1, from ...
but it returns wrong values...
Thank you, shorty
PS: Dates are stored as unixepoch
SELECT datetime(d, t)
FROM (
SELECT date('now') as d, time('now') as t) as dt;
probably:
SELECT DATETIME(DATE(dtStartDate) || ' ' || TIME(dtStartTime)) FROM YourTable;

Resources