SQL Commands 06 Dates 2018 PDF
SQL Commands 06 Dates 2018 PDF
Date Functions
• The time string can be followed by zero or more modifiers that alter show the current date date(timestring, modifier,
date and/or time. SELECT date('now') AS Today; modifier, ...)
• Each modifier is a transformation that is applied to the time value to its
left.
• Modifiers are applied from left to right; order is important
• e.g.,
SELECT datetime('now');
datetime(timestring, modifier, modifier, ...) datetime(timestring,
YYYY-MM-DD HH:MM:SS modifier, modifier, ...)
3 4
• NNN days Add (subtract) some amount of time to • start of month Shift the date backwards to the beginning of
• NNN hours • start of year the current month, year or day.
the date and time specified by the
• NNN minutes timestring and modifiers. • start of day
• NNN.NNNN seconds
• NNN months SELECT flno, departure,
• NNN years DATE(departure,'start of month')
FROM Flight;
Select datetime('now') as current_time,
datetime('now','1 days','1 hour','1 minute','1 second','1 year','1 month') as modified_time
SELECT flno, departure, DATE(departure,'start of
month‘, ‘3 days’)
FROM Flight;
5 6
1
8/23/18
• Advances the date forward to the next date where the weekday • Compute the duration of each flight in hours
number is N. Sunday is 0, Monday is 1, and so forth. SELECT flno, departure, arrival,
(julianday (arrival) - julianday (departure))*24 AS duration
SELECT date('now', 'weekday 0') FROM Flight;
7 8