Change dates to a right format in a query of SQLite - sqlite

I have stored dates in a Sqlite database with the following format:
21-03-2014, 09:56
How I can convert this date a right sqlite format inside a select query? (for ordering them, ORDER BY *formatted_date*)
Thanks in advance.

Ok, I've converted dd-mm-yyyy, hh:mm into yyyymmddhhmm and I've used it in order clause
substr(date,7,4)||substr(date,4,2)||substr(date,1,2)||substr(date,13,2)||substr(date,16,2)

Related

OUTPUT <NULL> WHEN SELECTING DATE FROM DATETIME - SQLITE

I'm new to the SQL world and im going crazy trying to figure out how to SELECT date from a datetime field in SQLITE.
Example: value <11/11/2005 14:56>, i just want to select <11/11/2005> for EVERY ROW.
I tried strftime(), date(), CAST() and other functions but the output its always NULL.
For example i tried querying SELECT strftime('%d/%m/%Y' , columnname) AS date FROM tablename;
OUTPUT: "NULL" in every row
Can someone help me understand what im doing wrong and how can i fix it? Thank you!!!
It always returns NULL because MM/DD/YYYY is not a valid sqlite date format. Treat the column as a string and use substr and instr to drop off the time portion. Something like (no guarantees, check the doc!)
SELECT substr(columname,0,instr(columnname,' '))
Re comment "how to order by the date in descending order"
This problem is a good argument (the best argument?) for storing the date in a sqlite date/time format. There is a strategy in this post for converting MM/DD/YYYY to YYYY-MM-DD (which sorts dates correctly).
If it's not too late, it would be advisable to change the date storage to a valid sqlite date format. strftime can be used to present the date as desired, and sorting will be accurate.

Snowflake GMT String to Date Conversion

We receive a string '2019-11-30T18:00:00GMT-06:00' in the JSON file and this need to be converted to timestamp to load into the timestamp column in the snowflake. I tried multiple options convert_timezone,to_timestamp etc, however in vain, Can you please let me know how i represent this string (2019-11-30T18:00:00GMT-06:00) in data format for comversion.
Thanks !
Leveraging a more Snowflake way to do this, you'd want to run something like this:
SELECT TO_TIMESTAMP('2019-11-30T18:00:00GMT-06:00','YYYY-MM-DDTHH:MI:SSGMT-TZH:TZM');
The output of this will be the date/time of your account default along with the timezone offset that goes along with that.
Please try the below mentioned approach
if your table is create as below
CREATE TABLE TIMESTAMP_TEST(DATE TIMESTAMP);
Insert the value as
INSERT INTO TIMESTAMP_TEST SELECT REPLACE(REPLACE('2019-11-30T18:00:00GMT-06:00','GMT'),'T',' ') FROM DUAL
Thanks

Convert integer column to date column using sqlite query

I am using SQLite.
In my database, I have a integer column value like 20050819. I would like to convert this as date column as like 2005-08-19.
I can achieve my requirement when using the function CONVERT(columnName, DATETIME) in MySQL server. But I need the same result in SQLite.
Is this possible in SQLite query?
You basically have to break apart the date:
select printf('%4d-%02.2d-%02.2d',
columnname/10000, columnname%10000/100, columnname%100)
from tablename;
P.S.
If you have the choice, you will be far better off storing the date in ISO standard format ('YYYY-MM-DD') in the database in the first place.

SQLite Database - Compare DateTime

I am working on a SQLite Database which contains a column which stores value in format yyyy-MM-dd HH:mm:ss. Now I need to create a filter to select rows with filter as this datetime column.
Query:
Select * from tbl_locations where datetime >= '2013-09-11 00:00:00' and datetime <='2013-09-13 00:00:00'
Above query is returning null set despite containing values in this slot(which I verified using select statement without filter.)
Any suggestion how can i get the required data set?
Perhaps this excerpt from the SQLite documentation will help you:
1.2 Date and Time Datatype
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
The date and time functions that you can use in your SQL to build your query are documented at http://www.sqlite.org/lang_datefunc.html
Ok, I tested this out in MySQL but hopefully it will work. I had a table that used timestamps, and changed the column to be of type text. Then I tried the following SQL query and got the same results that I normally would (besides trailing decimals)
SELECT timestamp(stock_quote_timestamp)
FROM stock.stock_quote
WHERE stock_quote_timestamp < timestamp('2013-10-07 11:05:30')##high_date
AND stock_quote_timestamp > timestamp('2013-10-03 14:09:03');##low_date;
So basically, just convert your text statements to timestamps so that they compare correctly. Oh, and you'll also need to state what else you're SELECTing, or you could do a compound select statement: SELECT *, timestamp(stock_quote_timestamp)...

datetime() in SQLite Manager

I cannot seem to figure out why datetime does not work for me on some data I imported from CSV. I have a column, TIMESTAMP, which is of type datetime.
Select TIMESTAMP from GPS limit 1 <-This gives me a time, "6/29/2009 00:00:00"
Select datetime(TIMESTAMP) from GPS limit 1 <- This gives me a pink field in SQLite manager, which seems empty.
Select datetime('now') from GPS limit 1 <- This gives me the current date and time. ("2012-12-19 20:45:17") It is formatted differently than my other data - is there a datatype issue?
What is going on? Did my "Timestamp" data not actually get converted into a DATETIME object? Why is it stored as text? Is there a way to fix this?
SQLite does not have a native date/time type; dates are stored either as numbers or as strings.
To be understood by SQLite's built-in date functions, date strings must have a format like yyyy-mm-dd hh:mm:ss.

Resources