How can I fix wrong date format in SQLite - sqlite

I'm working on app where I use SQLite to store data.
I created column Date. Since I'm beginner I made a mistake by inputing date as %m/%d/%Y (for example: 2/20/2020)
Now I've got a problem while taking out rows between selected dates.
I tried using this code:
SELECT * FROM Table WHERE Date BETWEEN strftime('%m/%d/%Y','2/5/2019') AND strftime('%m/%d/%Y','2/20/2020')
But that's not working.
Example table:
ID | Date
01 | 9/2/2019
02 | 2/20/2020
Thank you in advance for your help.

Update your dates to the only valid for SQLite date format which is YYYY-MM-DD:
update tablename
set date = substr(date, -4) || '-' ||
substr('00' || (date + 0), -2, 2) || '-' ||
substr('00' || (substr(date, instr(date, '/') + 1) + 0), -2, 2);
See the demo.
Results:
| ID | Date |
| --- | ---------- |
| 1 | 2019-09-02 |
| 2 | 2020-02-20 |
Now you can set the conditions like:
Date BETWEEN '2019-02-05' AND '2020-02-20'
If you do this change then you can use the function strftime() in select statements to return the dates in any format that you want:
SELECT strftime('%m/%d/%Y', date) date FROM Table
If you don't change the format of date column then every time you need to compare dates you will have to transform the value with the expression used in the UPDATE statement, and this is the worst choice that you could make.

Related

I need a conversion function, from integer to date-time

Is there a conversion to go from an integer (ex: 54600) to date-time format? I am seeing the integers in a flowsheet in healthcare, used to record a time
CONVERT(varchar, DATEADD(ms, b22.meas_value * 1000, 0), 114) AS 'START TIME' : Is the code that worked in MS SQL, but SNOWFLAKE does not recognize the function.
You can try to use TO_DATE. The data type of the returned value is DATE.
Syntax
TO_DATE( '<integer>' )
where
<integer>
An expression that evaluates to a string containing an integer, for example ‘15000000’. Depending upon the magnitude of the string, it can be interpreted as seconds, milliseconds, microseconds, or nanoseconds.
For details or other usage, see the above link.
There are a few different ways:
select 1637804567::varchar::date as date;
+------------+
| DATE |
|------------|
| 2021-11-25 |
+------------+
select to_date(1637804567::varchar) as date;
+------------+
| DATE |
|------------|
| 2021-11-25 |
+------------+
TO_DATE or CASTING will require the value to be string, so you need to convert the integer to string first before converting to date.
For your example, it will be like below in Snowflake:
select dateadd(ms, 54600*1000, to_date(1637804567::varchar)) as date;
+-------------------------------+
| DATE |
|-------------------------------|
| 2021-11-25 15:10:00.000000000 |
+-------------------------------+
TRY_TO_TIME() (there's a joke in there somewhere)
select try_to_time('54600') AS "START TIME"

SQLite - add days to a certain date in insert

I am using SQLite.
Let's say I have a table like this one:
CREATE TABLE dates (
date1 DATE NOT NULL PRIMARY KEY,
date2 DATE NOT NULL
);
Now, I want date1 to be a certain date and date2 to be date1 + 10 days.
How can I insert values to the table by using only date1 to produce both of them?
only thing i could find on the internet was something like that, but it's obviously not working, except for the case that I replace date('date1',+10days)) with date('now',+10days), but this is not what I want:
insert into dates values('2012-01-01', date('date1','+10 days'))
Any ideas?
Raise a trigger to automatically insert date2 every time you insert a date1 into the table.
CREATE TRIGGER date2_trigger AFTER INSERT ON dates
BEGIN
UPDATE dates SET date2 = DATE(NEW.date1, '+10 days') WHERE date1 = NEW.date1;
END;
-- insert date1 like so; date2 will be set automatically.
INSERT INTO dates(date1) VALUES('2012-01-01');
Instead of INSERT...VALUES use INSERT...SELECT like this:
insert into dates (date1, date2)
select t.date1, date(t.date1, '+10 days')
from (
select '2012-01-01' as date1
union all
select '2012-01-02'
union all
....................
) t
See the demo.
Results:
| date1 | date2 |
| ---------- | ---------- |
| 2012-01-01 | 2012-01-11 |
| 2012-01-02 | 2012-01-12 |

hive time_stamp convert into UTC with time_offset in UTC

I have 2 columns: time_stamp and time_offset. Both are STRING data type.
How can we convert one column values into UTC with the help of second column which is in UTC? Is their any hive or from unix solution to convert time_stamp column into UTC?
hive> select time_stamp from table1 limit 2;
OK
20170717-22:31:57.348
20170719-21:10:15.393
[yyyymmdd-hh:mm:ss.msc] this column is in local time
hive> select time_offset from table1 limit 2;
OK
-05:00
+05:00
[‘+hh:mm’ or ‘-hh:mm’ ] this column is in UTC
You can use the Hive Date Functions unix_timestamp and from_unixtime to perform the conversion.
Code
WITH table1 AS (
SELECT '20170717-22:31:57.348' AS time_stamp, '-05:00' AS time_offset UNION ALL
SELECT '20170719-21:10:15.393' AS time_stamp, '+05:00' AS time_offset
)
SELECT
time_stamp,
time_offset,
unix_timestamp(concat(time_stamp, ' ', time_offset), 'yyyyMMdd-HH:mm:ss.SSS X') AS unix_timestamp_with_offset,
from_unixtime(unix_timestamp(concat(time_stamp, ' ', time_offset), 'yyyyMMdd-HH:mm:ss.SSS X'), 'yyyyMMdd-HH:mm:ss.SSS') AS string_timestamp_with_offset
FROM table1
;
Result Set
+------------------------+--------------+-----------------------------+-------------------------------+--+
| time_stamp | time_offset | unix_timestamp_with_offset | string_timestamp_with_offset |
+------------------------+--------------+-----------------------------+-------------------------------+--+
| 20170717-22:31:57.348 | -05:00 | 1500348717 | 20170717-20:31:57.000 |
| 20170719-21:10:15.393 | +05:00 | 1500480615 | 20170719-09:10:15.000 |
+------------------------+--------------+-----------------------------+-------------------------------+--+
Explanation
unix_timestamp can accept an optional format string in the same syntax as Java SimpleDateFormat. I am guessing that your offsets are using the ISO 8601 syntax, so let's use the X format specifier. Then, we can use the concat String Operator to combine time_stamp and time_offset before passing to unix_timestamp.
The unix_timestamp function results in a numeric timestamp specified as seconds since epoch. To convert that back to a string representation, we can pass the result obtained from unix_timestamp through from_unixtime, this time specifying our original format specifier.
(Please do test thoroughly to make sure the results are making sense in your environment. Time zone math can be tricky.)

Sqlite convert month/year columns of text datatype to date

I have a month and year columns in my table. I would like to create a date from them. This does not work:
select date(year || '-' || month || '-' || 01) from utilities
My data looks like this:
------------
|month|year|
| 12|2013|
| 1|2010|
------------
SQLite accepts YYYY-MM-DD format, so only must care with months 1 to 9:
SELECT DATE(year||'-'||CASE WHEN month<10 THEN '0'||month ELSE month END||'-0
1');
This worked for me:
select max(date(year || '-' || SUBSTR('0' || month, -2, 2) || '-' || '01')) as maxdate, month, year from utilities

PostgreSQL - What is the easiest way to convert seconds in a numeric field to MM:SS

I have field with an integer value which means number of seconds. I would want to display it as MM:SS, without hours, so for example 6000 seconds should be shown as 100:00.
What's the easiest way to do this? As far as I see, to_char cannot be used for this.
This is a kind of workaround, as I haven't found better solution:
You could use a simple select:
test=# select (6022/60)::TEXT || ':' || (6022%60)::TEXT;
?column?
----------
100:22
(1 row)
This could be wrapped in a nice function:
CREATE FUNCTION format(integer) RETURNS TEXT AS $$
select ($1/60)::TEXT || ':' || ($1%60)::TEXT;
$$ LANGUAGE SQL STABLE;
Example of usage:
test=# SELECT format(6);
format
--------
0:6
(1 row)
test=# SELECT format(60);
format
--------
1:0
(1 row)
test=# SELECT format(600);
format
--------
10:0
(1 row)
test=# SELECT format(6000);
format
--------
100:0
(1 row)

Resources