How to deal with date in SQLite? - 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)

Related

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

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: Select rows within a specific time range, ignoring the date

I have a table called messages that stores messages from a chat with the following columns: username, message, datetime, where the type of datetime is TEXT and it is stored in the following format: "yyyy/MM/dd hh:mm:ss". I want to retrieve the average count of rows within a specific time range, without bothering with the date. For instance:
SELECT avg(count(message))
FROM messages
WHERE datetime < "2016/mm/dd 13:00:00" AND
datetime > "2016/mm/dd 12:00:00"
Is there some operator that allows any character to take the place of "mm" and "dd". Essentially, I am trying to construct a query that retrieves the average amount of messages within a specific time range, not the amount of messages on a specific date.
If I read your question correctly, you want to use your WHERE clause to restrict to any calendar date in 2016 between 12 and 13 hours. In this case, you can use STRFTIME to extract the year and hour in string format from your datetime column.
SELECT COUNT(message)
FROM messages
WHERE STRFTIME('%Y', datetime) = '2016' AND
STRFTIME('%H', datetime) < '13' AND
STRFTIME('%H', datetime) > '12'
Note that the reason while the inequalities should work with strings is because numerical strings still sort based on their lexigraphical order.
Update:
Since your datetime column is in a non standard format, you may be able to workaround this by substringing off the various pieces you need to use in the WHERE clause:
SELECT COUNT(message)
FROM messages
WHERE SUBSTR(datetime, 1, 4) = '2016' AND
SUBSTR(datetime, 12, 2) < '13' AND
SUBSTR(datetime, 12, 2) > '12'

Informix: datetime manipulation

I have 2 fields in a table test1:
onlydate DATE
onlytime DATETIME HOUR TO MINUTE
and 1 field in a table test2:
dateandtime DATETIME YEAR TO SECOND
Now I need to append the value of onlydate and onlytime field and set it to dateandtime field. How do I do that?
The basic issues are:
Converting DATE to DATETIME -- use EXTEND.
Converting DATETIME to INTERVAL -- use subtraction.
Assembling these two concepts and applied to SELECT only:
create temp table td(dateonly date not null, timeonly datetime hour to minute);
insert into td values('2010-05-31', '06:30');
select extend(dateonly, year to second) +
(timeonly - datetime(00:00) hour to minute) from td;
The result is what you want:
DATETIME YEAR TO SECOND
2010-05-31 06:30:00
Subtracting midnight from timeonly converts it into an INTERVAL HOUR TO MINUTE; you can add a DATETIME YEAR TO SECOND and an INTERVAL HOUR TO MINUTE, getting a DATETIME YEAR TO SECOND. You cannot add two DATETIME values.
So, strictly answering your question, you'd write:
INSERT INTO Test2(DateAndTime)
SELECT EXTEND(DateOnly, YEAR TO SECOND) +
(TimeOnly - DATETIME(00:00) HOUR TO MINUTE) AS DateAndTime
FROM Test1;
(I run with DBDATE=Y4MD- so that the date literal shown works as expected. To insert the DATE constant reliably regardless of the setting of DBDATE, use MDY(5,31,2010).)
You can concatenate both values as text, and cast it to datetime like:
update datetime_test
set dateandtime = (dateonly || ' ' || timeonly || ':00')::
datetime year to second

Resources