SQL – Dates and Times
How many columns should be returned for the following query? SELECT *, students.*, name FROM students; 3 4 7 Syntax error students name grade enthusiasm Josh 3.5 8.7
SELECT * and Qualified Names SELECT * is shorthand for all the columns (in order) in the table. * can be qualified (i.e. students.*) to make it unambiguous which table's columns are being included.
Problems with representations of time Dates and Times are messy to represent. Sometimes we only care about one or the other ("March 3rd" or 10:35PM) Sometimes we want different precision (12:35.9995AM) And don't even get started on time zones. https://www.youtube.com/watch?v=-5wpm-gesOY Many competing standards https://xkcd.com/927/
ISO 8601 date: YYYY-MM-DD time: HH:MM:SS.SSS datetime: YYYY-MM-DD HH:MM:SS.SSS Big benefit: lexicographic sort is chronological sort! You can store this format as text, and convert to a date object on export. https://xkcd.com/1179/
How to add timestamps to SQLite CREATE TABLE birthdays (name TEXT, birthday TEXT); INSERT INTO birthdays VALUES ('Josh', '1988-11-01'), ('Tom', '1986-12-31 12:31:56.7'), ('Dick', '12:31:56.7'); SELECT * FROM birthdays; But without a common format you can't compare them
SQLite date and time functions Input is of the form: 'YYYY-MM-DD' 'HH:MM:SS.SSS' (seconds are optional) 'YYYY-MM-DD HH:MM:SS.SSS' 'now' You can modify the timestamp by adding / removing time: '99 days' '-2 hours' Details here: https://www.sqlite.org/lang_datefunc.html
SQLite date and time functions The functions date, time, and datetime will take an input, apply any modifiers and return the resulting string. SELECT date('now'); -- 2016-02-08 SELECT time('12:53', '1 hours'); -- 13:53:00 SELECT datetime('2014-04-15', '-3 days'); -- 2014-04-12 00:00:00
What should this return? SELECT date('12:34'); 0000-01-01 1970-01-01 2000-01-01 Error (no date specified)