SQlite Query select string date today or less than - sqlite

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

Related

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.

PARSE_DATETIME from string returns letter T in between date and time

I want to convert the string to DateTime.
My strings look like this:
19.03.2020 08:14:13
09.07.2020 07:32:39
I used these queries:
PARSE_DATETIME('%d.%m.%Y %H:%M:%S', Date) AS NF_spent_date,
and
PARSE_DATETIME('%d.%m.%Y %T', Date) AS NF_spent_date,
but the result in both cases is
2020-03-19T08:14:13
2020-07-09T07:32:39
How can I avoid the T letter in the output??
This is how Google BigQuery displays DATETIME values. You can use PARSE_TIMESTAMP to convert to TIMESTAMP instead which is displayed without T but with UTC at the end:
select PARSE_TIMESTAMP('%d.%m.%Y %H:%M:%S', '19.03.2020 08:14:13') AS NF_spent_date

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)

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

PostgreSQL - How to convert seconds in a numeric field to HH:MM:SS

I'm new to PostgreSQL (I have been using MS SQL for many years) and need to convert a numeric column which contains a time in seconds to HH:MM:SS format.
I have Googled and found that to_char(interval '1000s', 'HH24:MI:SS') works so I am attempting to use this with my field name:
to_char(fieldname, 'HH24:MI:SS') gives an error cannot use "S" and "PL"/"MI"/"SG"/"PR" together
to_char(fieldname::interval, 'HH24:MI:SS') gives an error cannot cast type numeric to interval
Can anyone show me where I am going wrong?
SELECT TO_CHAR('1000 second'::interval, 'HH24:MI:SS')
or, in your case
SELECT TO_CHAR((mycolumn || ' second')::interval, 'HH24:MI:SS')
FROM mytable
To convert seconds to HH:MM:SS
SELECT 120 * interval '1 sec';
You will get 00:02:00
To convert minutes to HH:MM:SS
SELECT 120 * interval '1 min';
You will get 02:00:00

Resources