A Gentle Introduction to

SQL

GISQ: How to use functions

You may need to "Tidy up" before running these examples.

Most SQL implementations offer a large number of functions to manipulate strings and perform complex calculations. Unfortunately each implementation has its own suite with different names.


Concatenate strings.
Concatenation means "stick strings together".
In this example we concatenate three strings, name and region are string attributes of the table, ' is in ' is a string literal.
oracle variations:
None

postgres
oracle
access
mysql
sqlserver

Substring: Extracting part of a string.
Here we take two characters starting from position 4.
 123456789
'Afghanistan' -> 'ha'
'China'       -> 'na'
'Sri Lanka'   -> ' L'
oracle variations:
None

postgres
oracle
access
mysql
sqlserver

lower case
UPPER CASE is similar.
oracle variations:
None

postgres
oracle
access
mysql
sqlserver

Finding a substring in a string
Here we extract the first word of a country name. INSTR gives this position of one string within another, we use this and substring to pick out the first few characters.
oracle variations:
None

postgres
oracle
access
mysql
sqlserver

Formatting numbers to two decimal places.
This rounds up or down
oracle variations:
A preceding 9 in the format string may be dropped, 0 is not

postgres
oracle
access
mysql
sqlserver

Replace a NULL with a specific value
oracle variations:
None

postgres
oracle
access
mysql
sqlserver

Conditional values
oracle variations:
The DECODE function allows a list of possible values

postgres
oracle
access
mysql
sqlserver

Get the date and time right now.
oracle variations:
None

postgres
oracle
access
mysql
sqlserver

Format dates.
oracle variations:
Other format codes: AM, D (day of week), DDD (day of year), DY (abrv. day name), HH, MON (month in words)

postgres
oracle
access
mysql
sqlserver