Sort date in sqlite - sqlite

I want to select the dates in ascending order. Dates are stored in dd-MMM-yy(02-Mar-12) format. Here is my query:
SELECT EventDate,Event,ID from EventCalenderTable Order By EventDate ASC
output is:
10-03-12
12-02-12
15-01-12
18-07-12
But the output should like:
15-01-12
12-02-12
10-03-12
18-07-12
Event Date is date datatype.
I have seen number of post about storing date in sql. I noticed that Convert function done the tricks in sql server. But how can I do this in Sqlite??
Thanks in advance.

SQLite only knows three date formats:
Text ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")
Real Julian day numbers since November 24, 4714 B.C
Integer number of seconds since 1970-01-01 00:00:00 UTC
SQLite does have five date/time functions for converting between formats.

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.

How to filter data between date & time in sqlite

I have a table Orders with Order_Date datatype is smalldatetime and my Order_Date Format is 01/10/2018 10:00:00 PM
Now I want to filter data between 01/10/2018 04:00:00 PM AND 02/10/2018 04:00:00 AM
What I tried
SELECT distinct(Order_No),Order_Date from Orders WHERE Order_Date BETWEEN '01/10/2018 04:00:00 PM' and '02/10/2018 04:00:00 AM'
This query is showing only 01/10/2018 Data but I want the data BETWEEN 01/10/2018 04:00:00 PM and 02/10/2018 04:00:00 AM
Is there any way to get the data from today 4PM To Next Day 4AM?
First off, sqlite does not have actual date/time types. It's a simple database with only a few types. Your smalldatetime column actually has NUMERIC affinity (See the affinity rules).
For Sqlite's builtin functions to be able to understand them, date and times can be stored as numbers or text; numbers are either the number of seconds since the Unix epoch, or a Julian day. Text strings can be one of a number of formats; see the list in the docmentation. All these have the additional advantage that, when compared to other timestamps in the same format, they can be properly sorted.
You seem to be using text strings like '01/10/2018 04:00:00 PM'. This is not one of the formats that sqlite date and time functions understand, and it doesn't sort naturally, so you can't use it in comparisons aside from testing equality. Plus it's ambiguous: Is it October 1, or January 10? Depending on where you're from you'll have a different interpretation of it.
If you change your timestamp format to a better one like (Assuming October 1) '2018-10-01 16:00:00', you'll be able to sort and compare ranges, and use it with sqlite functions.

How to convert sqlite dates to a date in R

I am working with a sqlite database table. I have pulled the data into R using the RSQLite package. One of the columns holds a date. Sqlite is storing it as a Real number, the number of days since noon in Greenwich on November 24, 4714 B.C. (e.g.1264896000). Any ideas on how to convert this to a valid date in R? I tried the following
as.POSIXct(1264896000,origin = "-4714-11-24")
However, this doesn't work as the character string in not in a standard form. Any ideas?
I tested my theory that your claim about the origin was unlikely. The theory that these are POSIX date-times (origin= 1970-01-01 and times in seconds) seems supported by experiment.
> as.POSIXct(1264896000,origin = "1970-01-01")
[1] "2010-01-30 16:00:00 PST"

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

Julian Day to ISO 8601 string in SQLite

I have a table that stores date/time values as julian days in SQLite (using the julianday() function). I can't seem to figure out how to convert them back to ISO 8601 style strings (YYYY-mm-ddThh:m:ss.sss) when I read them?
Just feed the Julian day number to the datetime function:
A time string can be in any of the following formats:
[...]
12. DDDDDDDDDD
[...]
Format 12 is the Julian day number expressed as a floating point value.
So datetime(julianday_output) goes in the opposite direction as the julianday function:
sqlite> select datetime(julianday(current_timestamp)) as dt_from_jd, current_timestamp as dt;
dt_from_jd | dt
2011-09-30 14:46:52 | 2011-09-30 14:46:52
Have you tried strftime? http://www.sqlite.org/lang_datefunc.html

Resources