Extracting substrings

From SQLZOO
Jump to navigation Jump to search

Substring: Extracting part of a string.

schema:gisq

We from position 1 (the beginning) we take two characters. 4.

'Afghanistan' -> 'Af'
'China'       -> 'Ch'
'Sri Lanka'   -> 'Sr'

The SQL standard insists on a horrible syntax:

SUBSTRING(name FROM 1 FOR 2) 
SELECT name, SUBSTRING(name FROM 1 FOR 2)
 FROM bbc
 WHERE region = 'Asia'
SELECT name, SUBSTR(name,1,2) FROM bbc
SELECT name, SUBSTRING(name FROM 1 FOR 2)
 FROM bbc
 WHERE region = 'Asia'
SELECT name, MID(name,1,2) FROM bbc
 WHERE region = 'Asia'
SELECT name, SUBSTRING(name FROM 1 FOR 2)
 FROM bbc
 WHERE region = 'Asia'
SELECT name, SUBSTR(name,1,2) FROM bbc
SELECT name, SUBSTRING(name ,1 ,2)
 FROM cia
 WHERE region = 'Asia'
SELECT name, SUBSTRING(name FROM 1 FOR 2)
 FROM bbc
SELECT name, SUBSTRING(name FROM 1 FOR 2)
 FROM bbc
 WHERE region = 'Asia'
SELECT name, SUBSTRING(name ,1 ,2)
 FROM bbc
 WHERE region = 'Asia'
DataWars, Data Science Practice Projects - LogoDataWars: Practice Data Science/Analysis with +100 Real Life Projects