Date/Time and String Functions
| Table | Columns used | One row represents |
|---|---|---|
Customer |
CustomerId, FirstName, LastName, Company, Email, Phone, Country |
One customer |
Invoice |
InvoiceId, CustomerId, InvoiceDate, Total |
One purchase |
Track |
TrackId, Name, Composer, Milliseconds |
One track |
Employee |
EmployeeId, FirstName, LastName, BirthDate, HireDate |
One employee |
Join Invoice.CustomerId to Customer.CustomerId.
Classroom InvoiceDate values use UTC, with format YYYY-MM-DD HH:MM:SS.
| Table | Columns | Purpose |
|---|---|---|
practice_contact |
contact_id, first_name, last_name, email, phone |
Spaces, blanks, NULLs, mixed case, malformed email |
practice_event |
event_id, label, start_text, end_text |
Offsets, missing endpoints, invalid input, reversed intervals |
A timestamp without an offset in these exercises is assumed UTC.
Keep the raw values. Derive cleaned columns with SELECT so you can compare before and after.
Strings
Dates and times
Activity Solutions
NULL, '', and 'NULL' are three different values.
|| joins text expressions in the order written.
joined |
spaced |
|---|---|
| AnaRivera | Ana Rivera |
concat_ws() places the separator between retained values.''. A NULL separator gives NULL.lower() and upper() handle ASCII case conversion by default. They do not provide full Unicode case folding or accent removal.
length(TEXT) counts Unicode code points before the first NUL character.SQL.
ite.
ite.
substring() is an alias in SQLite 3.34+.
instr(text, search) returns the position of the first match. It returns 0 when there is no match, and NULL if an argument is NULL.
Table: practice_contact
Return one row per contact with:
contact_iddisplay_name: trim both names, join with one space, and avoid a trailing space when the last name is NULL.email_clean: lowercase and trim the emailDefault LIKE ignores ASCII letter case, but does not fully fold non-ASCII case.
| Pattern | Meaning |
|---|---|
'Love%' |
Starts with Love |
'%Love%' |
Contains Love |
'%Love' |
Ends with Love |
!% means a literal percent sign. The outer % symbols remain wildcards.
!_ means a literal underscore.
Write separate queries for tracks whose names contain love fromt the Track table. Display the TrackID and Name, the length of the name in a new column, and the duration of the track in minutes from Milliseconds. Order by descending Milliseconds and TrackID.
Without an explicit input order, the order inside the string is not guaranteed.
group_concat(DISTINCT Country) removes duplicates with the default comma separator. Deduplicate in a CTE when you also need a custom separator.
For each country from the table Customer, return:
Sort the countries alphabetically. On SQLite 3.44+, also order names by last name, first name, and customer ID inside the list.
Strings
Dates and times
Activity Solutions
| Concept | Example | Interpretation |
|---|---|---|
| Calendar date | 2024-02-29 |
A day on the calendar |
| Local date and time | 2024-03-01 09:00:00 |
Needs a time-zone convention |
| Timestamp with offset | 2024-03-01T09:00:00-08:00 |
An instant with an explicit offset |
| Duration | 5400 seconds |
Elapsed time between instants |
| Representation | Example | Useful for |
|---|---|---|
TEXT |
'2024-01-01 00:00:00' |
Readable timestamps in a fixed format |
INTEGER |
1704067200 |
Unix seconds since 1970-01-01 UTC |
REAL |
2460310.5 |
Julian day number and fractional days |
Declaring a column DATE or DATETIME does not automatically parse or validate every value inserted into it.
Choose one representation, precision, and time-zone policy per column.
The first two give 2024-03-01 09:30:00.
The third gives 2024-03-01 17:30:00.
Use supported, zero-padded year-month-day forms. SQLite does not parse every ISO 8601 variant or arbitrary locale format such as 03/01/2024.
This produces 2024-03-25 only because the source contract specifies MM/DD/YYYY with fixed widths.
| Function | Result type | Example use |
|---|---|---|
date() |
TEXT | Calendar date |
time() |
TEXT | Time of day |
datetime() |
TEXT | Date and time |
strftime() |
TEXT | Custom format or date component |
julianday() |
REAL | Fractional-day arithmetic |
unixepoch() |
INTEGER, or REAL with subsec |
Elapsed seconds |
timediff() |
TEXT | Calendar-aware difference, 3.43+ |
The first six accept modifiers. timediff(A, B) takes exactly two time values.
YYYY-MM-DD date.
HH:MM:SS time of day.
These expressions return derived values. They do not change InvoiceDate.
'now' uses UTC and depends on when the query runs.'now' calls within the same SQLite step use the same instant.1704067200.
Use 2024-01-01 00:00:00.
datetime().0 and Saturday = 6.
%m means month. %M means minute.
| Code | Meaning | Example |
|---|---|---|
%Y, %m, %d |
Year, month, day | 2024, 02, 29 |
%H, %M, %S |
Hour, minute, second | 09, 05, 07 |
%f |
Seconds including fraction | 07.125 |
%j |
Day of year | 060 |
%w |
Weekday, Sunday = 0 | 4 |
%W |
Week number, Monday-based, 00–53 | 09 |
%s, %J |
Unix seconds, Julian day as text | Numeric-looking text |
%% |
Literal percent sign | % |
An unsupported format code makes the result NULL. Month and weekday names need a mapping such as CASE or a lookup table.
calendar_year |
iso_week |
|---|---|
| 2021 | 2020-W53 |
Use %G with %V for an ISO week-year key. Combining %Y with %V can mislabel weeks near New Year.
%W and %U are different week-numbering systems and can include week 00.
Useful modifiers include signed days, hours, minutes, seconds, months, and years, plus start of day, start of month, and start of year.
Multiple modifiers can be added per function. Modifiers apply from left to right.
For the portable weekday N pattern, use 0 = Sunday through 6 = Saturday. It advances to that weekday or stays on the date if it already matches.
It does not automatically skip holidays. Business-day schedules need a calendar table with explicit holiday rules.
This includes every February timestamp, including leap day. It avoids guessing the final second or fractional second of the month.
due_date, exactly 14 calendar days after the invoice date.invoice_month_end, the final date of the invoice’s month.0.0625 days.
5400 seconds, or 90 minutes.
Subtract end minus start. Divide seconds by 60.0, 3600.0, or 86400.0 to preserve fractional units.
Using the Invoice table, find the time elapse from a customers first InvoiceDate to the last InvoiceDate customer 1 and 2.
Strings
Dates and times
Activity Solutions
SELECT InvoiceId, InvoiceDate, Total,
date(InvoiceDate, '+14 day') as due_date,
date(InvoiceDate, 'start of month', '+1 month', '-1 day') as invoice_month_end,
time(InvoiceDate) as timestamp
FROM Invoice
WHERE InvoiceDate >= '2024-03-01 00:00:00'
AND InvoiceDate < '2024-04-01 00:00:00'
ORDER BY InvoiceDate, InvoiceId;