Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite Query Questions

Tags:

sql

sqlite

2 questions:

First, in MSSQL, I can convert nvarchar to datetime by doing

cast('5/31/2011 12:00:00 AM' as datetime) as convertedtodate

Result: 2011-05-31 00:00:00.000

How do I do it in SQLite?

Second, what is the equivalent of MS SQL's datediff function on sqlite? E.g.:

datediff(Day,'5/30/2011 12:00:00 AM','5/31/2011 12:00:00 AM') as DateAge  

Result: 1

like image 899
BizApps Avatar asked May 12 '11 00:05

BizApps


People also ask

How many queries can SQLite handle?

The SQLite website (https://www.sqlite.org/) uses SQLite itself, of course, and as of this writing (2015) it handles about 400K to 500K HTTP requests per day, about 15-20% of which are dynamic pages touching the database. Dynamic content uses about 200 SQL statements per webpage.

Is SQLite file based MCQ?

SQL support stored procedures while SQLite does not support stored procedures. SQL is server based while SQLite is file based.

When Can U Get an SQLite schema error?

16) When can you get an SQLITE_SCHEMA error? The SQLITE_SCHEMA error is returned when a prepared SQL statement is not valid and cannot be executed. Such type occurs only when using the sqlite3 prepare() and sqlite3 step() interfaces to run SQL.


1 Answers

To answer your first question (convert string to date), the answer is "it depends". Since SQLite does not have a specific date field, you may not need to convert it. You could just store it in a string field (the options are sting, real, or int for date storage). If you want to convert the string to an int (which would be the number of seconds since 1970-01-01), you would use the strftime method like so:

strftime('%s','2011-05-12 01:03:00')

As for the second part of your question (difference between two dates), you would use the following code in SQLite:

strftime('%s','2011-05-12 01:03:00') - strftime('%s','2011-05-08 11:54:09')

That will give you the number of seconds between the two dates. You can play around with this information, as well as a lot more, to get exactly what you are looking for from SQLite. Here are a couple resources that will help you out:

http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions

http://www.sqlite.org/datatype3.html

like image 66
IAmTimCorey Avatar answered Sep 19 '22 14:09

IAmTimCorey