Scalar Functions - SQL Query

Scalar Functions

Scalar functions are used to return single value that is based on input value.

Consider the following table 'Students'

Stud_IDNamePhoneCityCountry
1Alex654124PerthAustralia
2Martin654125SydneyAustralia
3Shruti910001DelhiIndia
4Jaya910002MumbaiIndia
5Paul450525LondonEngland
6Andrew450526LondonEngland

i) Display the column 'Name' in uppercase from table 'Students'.

SELECT UCASE(Name) AS Student_Name FROM Students

The UCASE() function converts a particular field into uppercase.

Output:

Student_Name
ALEX
MARTIN
SHRUTI
JAYA
PAUL
ANDREW

ii) Display the column 'Name' in lowercase from table 'Students'.

SELECT LCASE(Name) AS Student_Name FROM Students

The Function LCASE() converts a selected field into lowercase.

Output:

Student_Name
alex
martin
shruti
saya
paul
andrew

iii) Display the first three characters of Column 'City', from table 'Students'.

SELECT MID(City,1,3) AS City3 FROM Students

The function MID() is used to extract the characters from a text field.

Output:

City3
Per
Syd
Del
Mum
Lon
Lon

iv) Display the total length value of field 'City' from table Students.

SELECT Name, LEN(City) AS Length_City FROM Students

The LEN() function computes the total length of the characters present in the selected field. Here the Name is selected to display the name of student and LEN(City) will return the total length of city in all rows.

Output:

NameLength_City
Alex5
Martin6
Shruti5
Jaya6
Paul6
Andrew6