SQLite How can I select data between 2 date with time included - sql

I have seperate date and time columns in my table. Date as mm/dd/yyyy, time as hh:mm but i can change the format. I want to list data between 2 date/time. How can I do that?
select * from testtable where date >= '01/10/2022' AND date <= '01/10/2023' AND time >= '13:45' AND time <= '15:50'
I wrote it but of course it doesn't work like what i expected.

The best fix and really the only one you want here would be to start storing your timestamps in a sortable ISO format yyyy-mm-dd hh:ii:ss. Then, use this query:
SELECT *
FROM testtable
WHERE date BETWEEN '2022-01-10 13:45:00' AND '2023-01-10 15:50:00';
The thing to realize here is that SQLite does not actually have a date column type. Rather, you always store your dates/timestamps as text, and therefore it is crucial to use an ISO sortable format as shown above.

If your target is sqlite, it lacks complex timestamp types. But you have another option here. You can store that as unix timestamp, it is an integer representing the number of seconds offset from the epoch which is 1970-01-01 00:00:00 UTC. The table format would then be:
CREATE TABLE testtable (
date INTEGER
);
You can the use the unixepoch function to translate a string representation to that unix timestamp format. To insert a new date, you would use:
INSERT INTO testtable (date) VALUES (unixepoch('2023-01-11T11:30:00+01:00'))
Finding a matching row is now as easy to compare integers together. You can convert the datetime representation to unix timestamp at the application level, most programming environments provide such time utility functions/library. Or can still use the unixepoch function from sqlite for your where clause.
SELECT * FROM testtable
WHERE date >= unixepoch('2022-10-01T13:45:00Z')
AND date <= unixepoch('2023-10-01T15:50:00Z')
The final Z indicates an UTC time zone but you can adjust that with another +HH:00 extenstion instead which reflect the offset from utc of your datetime representation.

Related

how to do a SELECT in SQLite with a range of dates from datetime string to a UNIX timestamp?

Hi I have a database SQLite with a table "data" with the column "time#timestamp" that is a REAL for example 1669729394.792
So I have to select a range of data using 2 dates (start date and end date) written by the operator in human datetime format (ex. 2022-11-29) and extract all my data
somehow I should convert my date from standard format to UNIX timestamp
I tried like this but it doesn't work for me:
SELECT * FROM data WHERE date([time#timestamp]) BETWEEN CAST(strftime('%s', '2022-11-29') AS REAL) AND CAST(strftime('%s', '2022-11-30') AS REAL)
The function date() with a numeric parameter and no modifiers considers the numeric value as a Julian day and returns that date in the text format YYYY-MM-DD.
But your datetime values are not Julian days, they are unix timestamps and you can transform them to a readable date format YYYY-MM-DD with the modifier 'unixepoch':
date([time#timestamp], 'unixepoch')
After that you can directly compare the result to any date in the format YYYY-MM-DD and no casting is needed:
SELECT *
FROM data
WHERE date([time#timestamp], 'unixepoch') BETWEEN '2022-11-29' AND '2022-11-30'
Or, keep the value of [time#timestamp] as it is and transform the 2 date boundaries to unix timestamps:
SELECT *
FROM data
WHERE [time#timestamp] BETWEEN strftime('%s', '2022-11-29') AND strftime('%s', '2022-11-30')
Or, if your version of SQLite is 3.38.0+:
SELECT *
FROM data
WHERE [time#timestamp] BETWEEN unixepoch('2022-11-29') AND unixepoch('2022-11-30')

How to query timestamp columns with T and Z in Oracle 11?

If I do the following query,
select dt from table where dt <= timestamp '2021-06-01T10:45:00Z'
I get the ORA-01861: literal does not match format string error. However, if I remove T and Z from the string, it works fine.
So, my question is, how can I query a timestamp with T and Z here. I need to be able to do that, because the timestamp is returned by a tool in this format (so, I can't remove T and Z myself) and another tool directly uses that in a query. I can however change the query myself. The timestamp there is referred as a variable.
If your dt column is a timestamp with no time zone or date and you are ignoring the time zone from the fixed string then you can do:
where dt <= to_timestamp('2021-06-01T10:45:00Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z")
or
where dt <= to_date('2021-06-01T10:45:00Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z")
If you want to honour the time zone, because e.g. dt is a time stamp with timezone, then you can declare the value as UTC:
where dt <= from_tz(to_timestamp('2021-06-01T10:45:00Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"), 'UTC')
Be careful with the table column data type though.
And if have numbers after seconds, such as 10:38:10.11956, what would be the format?
Add .FF for fractional seconds:
to_timestamp('2021-06-01T10:38:10.11956Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF"Z")
Not that this won't work with to_date() as that doesn't have fraction-second precision. If necessary you can cast a timestamp to a date, or round/trunc to do that implicitly.
The format model elements are in the documentation; that also shows character literals - like the T and Z in this format.
A brute force method would use to_timestamp_tz() and to replace the timezone with an hour offset:
to_timestamp_tz(replace('2021-06-01T10:45:00Z', 'Z', ' +00'), 'YYYY-MM-DD"T"HH:MI:SS TZH')

Oracle SQL Select Current Timestamp without Timezone and 24hr Format

I have a Oracle SQL statement where I have to get the current timestamp as one of the columns. But I dont require the Timezone which CURRENT_TIMESTAMP gives or the AM/PM given by LOCALTIMESTAMP.
I require the current timestamp in 24hr format without the timezone.
Is it possible to get that in Oracle SQL?
It seems you're mixing 2 concepts here: "datatype" and "date format mask".
data type: LOCALTIMESTAMP returns datatype TIMESTAMP and CURRENT_TIMESTAMP returns datatype TIMESTAMP WITH TIME ZONE. TIMESTAMP is similar to DATE but has a higher precision. As usual... checking the docs is worth it.
date format mask: determines how you display the date information. Americans can't read 24 hour format, the rest of the world is confused by AM/PM. Fortunately, you can decide how you want to display the date as explained in the oracle docs.
If you just want to return the current date in 24 hour format you could do something like:
SELECT
TO_CHAR(SYSDATE,'DD-MON-YYYY HH24:MI:SS') as mydate,
<other columns>
FROM
<table_name>
If you need the date to be more precise and you require fractional seconds then you can use SYSTIMESTAMP instead of DATE with a format mask 'DD-MON-YYYY HH24:MI:SS.FF9'

Oracle - Convert datetime format

I have a temp table.
It has last_update column in 2/10/2018 6:01:50 PM datetime format.
How can I write THE BEST QUERY to display all information that's updated on 02-Oct-2018 day?
You can use trunc function
select *
from tab
where trunc(last_update) = date'2018-10-02'
It is preferable to avoid TRUNC especially if you have an index on the column last_update.
A simple where condition should be better and may be better performant.
WHERE last_update >= date '2018-10-02' AND
last_update < date '2018-10-02' + 1
Use trunc function for getting the same day:
trunc(last_update) = trunc(to_date('02-Oct-2018', 'DD-MONTH-YYYY'))
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day.
You can also use format DD-MON-YYYY

Restrict the date range to 20 days

I have a user form where user selects From_date & To_date to search between those days, there is no way to restrict the user not to enter more than 20 days, this must be done in the store procedure.
How can I return the results after adding 20 days to the From_date. The date format is '2015-05-29-06.44.03.956380'
Your date contains fractional seconds, so it's a TIMESTAMP not a DATE datatype. Dont worry about the TIMESTAMP format, because Oracle store it internally always as a number then it is formatted depending to your session settigs, you can use + operator to add days to any datetime datatypes (DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, and TIMESTAMP WITH LOCAL TIME ZONE):
select * from my_table where date_field between From_date and From_date+20