Expressions and Functions: CASE, CAST, Strings, Dates
SELECT, WHERE, and ORDER BY accept expressions in place of columns. Arithmetic, CASE, and CAST follow the standard and are the same in all three DBs; many string and date functions have different names.
CASE: picking a value by condition
CASE WHEN condition THEN value … ELSE value END returns the value of the first true condition. If there is no ELSE and no condition matches, it is NULL.
Splitting into amount bands
SELECT ord_id, amt,
CASE
WHEN amt >= 400000 THEN 'HIGH'
WHEN amt >= 100000 THEN 'MID'
ELSE 'LOW'
END AS amt_band
FROM orders
WHERE cust_id = 101
ORDER BY ord_id
To compare one value against several values, use the short form CASE grade WHEN 'VIP' THEN … END.
Arithmetic and integer division
Dividing integers by integers returns a truncated integer in PostgreSQL and SQL Server, and a decimal in Oracle. If you need a decimal, convert one side to a decimal.
7 / 2
SELECT 7 / 2 AS int_div,
7 / 2.0 AS dec_div,
CAST(7 AS DECIMAL(10, 2)) / 2 AS cast_div
NOTE
SELECTwithoutFROMworks in PostgreSQL, SQL Server, and Oracle 23ai+. Oracle 21c and earlier needFROM dual.
CAST: converting types
CAST(expr AS type) is standard and the same in all three DBs. When you compare a string with a number, the DB silently converts one side (implicit conversion), a common reason an index cannot be used (tuning scenario 1).
String order number to number
SELECT ord_id, ord_no,
CAST(ord_no AS DECIMAL(20)) + 1 AS ord_no_plus_1
FROM orders
WHERE ord_id <= 3
ORDER BY ord_id
String functions
| Function | Oracle | PostgreSQL | SQL Server |
|---|---|---|---|
| Concatenate | a || b, CONCAT(a, b) | a || b, CONCAT(a, b, …) | a + b, CONCAT(a, b, …) |
| Substring | SUBSTR(s, start, length) | SUBSTRING(s FROM start FOR length) | SUBSTRING(s, start, length) |
| Length | LENGTH(s) | LENGTH(s) | LEN(s) |
| Case | UPPER, LOWER | UPPER, LOWER | UPPER, LOWER |
| Trim leading/trailing spaces | TRIM(s) | TRIM(s) | TRIM(s) (2017+) |
Oracle · String functions
SELECT cust_nm,
region || '-' || grade AS label,
SUBSTR(cust_nm, 5) AS num_part,
LENGTH(cust_nm) AS len,
LOWER(region) AS region_lower
FROM customers
WHERE cust_id IN (1, 22, 333)
ORDER BY cust_id
PostgreSQL · String functions
SELECT cust_nm,
region || '-' || grade AS label,
SUBSTRING(cust_nm FROM 5) AS num_part,
LENGTH(cust_nm) AS len,
LOWER(region) AS region_lower
FROM customers
WHERE cust_id IN (1, 22, 333)
ORDER BY cust_id
SQL Server · String functions
SELECT cust_nm,
CONCAT(region, '-', grade) AS label,
SUBSTRING(cust_nm, 5, LEN(cust_nm)) AS num_part,
LEN(cust_nm) AS len,
LOWER(region) AS region_lower
FROM customers
WHERE cust_id IN (1, 22, 333)
ORDER BY cust_id
Korean string literals
Korean inside quotes
SELECT '한글' AS plain_literal, N'한글' AS national_literal
In SQL Server, '한글' is interpreted in the code page of the database's default collation, and characters missing from that code page become ?. Write Unicode strings as N'…' (nvarchar). In Oracle and PostgreSQL, the two literals are the same when the database character set is UTF-8. All three DBs accept the N'…' syntax.
Date functions
The standard date literal is DATE '2025-09-01', and the function that extracts the year or month from a date is EXTRACT(YEAR FROM d). Oracle and PostgreSQL support both; SQL Server supports neither.
| Function | Oracle | PostgreSQL | SQL Server |
|---|---|---|---|
| Date literal | DATE '2025-09-01' | DATE '2025-09-01' | CAST('2025-09-01' AS date) |
| Extract year, month | EXTRACT(YEAR FROM d) | EXTRACT(YEAR FROM d) | YEAR(d), MONTH(d) |
| Add one day | d + INTERVAL '1' DAY, d + 1 | d + INTERVAL '1' DAY | DATEADD(day, 1, d) |
| Truncate to month start | TRUNC(d, 'MM') | date_trunc('month', d) | DATETRUNC(month, d) (2022+) |
Oracle · Date functions
SELECT ord_id, ord_dt,
EXTRACT(YEAR FROM ord_dt) AS y,
EXTRACT(MONTH FROM ord_dt) AS mon,
TRUNC(ord_dt, 'MM') AS month_start,
ord_dt + INTERVAL '7' DAY AS plus_7d
FROM orders
WHERE cust_id = 101
AND ord_dt >= DATE '2025-01-01'
ORDER BY ord_dt
PostgreSQL · Date functions
SELECT ord_id, ord_dt,
EXTRACT(YEAR FROM ord_dt) AS y,
EXTRACT(MONTH FROM ord_dt) AS mon,
date_trunc('month', ord_dt) AS month_start,
ord_dt + INTERVAL '7' DAY AS plus_7d
FROM orders
WHERE cust_id = 101
AND ord_dt >= DATE '2025-01-01'
ORDER BY ord_dt
SQL Server · Date functions
SELECT ord_id, ord_dt,
YEAR(ord_dt) AS y,
MONTH(ord_dt) AS mon,
DATETRUNC(month, ord_dt) AS month_start,
DATEADD(day, 7, ord_dt) AS plus_7d
FROM orders
WHERE cust_id = 101
AND ord_dt >= CAST('2025-01-01' AS date)
ORDER BY ord_dt
TIP Write date ranges as half-open intervals, such as
ord_dt >= start AND ord_dt < next day. Wrapping the column in a function, as inEXTRACT(YEAR FROM ord_dt) = 2025, gives the same result but prevents a range read of theord_dtindex.