Download presentation
Presentation is loading. Please wait.
1
SQL – Dates and Times
2
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
3
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.
4
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: AM) And don't even get started on time zones. Many competing standards
5
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.
6
How to add timestamps to SQLite
CREATE TABLE birthdays (name TEXT, birthday TEXT); INSERT INTO birthdays VALUES ('Josh', ' '), ('Tom', ' :31:56.7'), ('Dick', '12:31:56.7'); SELECT * FROM birthdays; But without a common format you can't compare them
7
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:
8
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'); SELECT time('12:53', '1 hours'); -- 13:53:00 SELECT datetime(' ', '-3 days'); :00:00
9
What should this return? SELECT date('12:34');
Error (no date specified)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.