sql format()

SQL Functions

homeBack

SQL FORMAT()

  • The FORMAT() Function

The FORMAT() function is used to format how a field is to be displayed.

SQL FORMAT() Syntax

SELECT FORMAT(column_name,format) FROM table_name

Parameter Description
column_name Required. The field to be formatted.
format Required. Specifies the format.

  • SQL FORMAT() Example

Table Name ” Distance”

Point distance(KM)
AtoB 5.4
XtoZ 15.7
BtoE 4.9

Now we want to display the products and prices per today’s date (with today’s date displayed in the following format “YYYY-MM-DD”).

We use the following SELECT statement:

SELECT Point, Unitdistance, FORMAT(Now(),’YYYY-MM-DD’) as Date FROM emp

The result-set will look like this:

Point Unitdistance Date
AtoB 5.4 2012-08-16
XtoZ 15.7 2012-08-16
BtoE 4.9 2012-08-16

sql now()

SQL Functions

homeBack

Next

SQL NOW()

  • The NOW() Function

    The NOW() function returns the current system date and time.

    SQL NOW() Syntax

    SELECT NOW() FROM table_name

  • SQL NOW() Example

    Table Name ” Distance”

    Point distance(KM)
    AtoB 5.4
    XtoZ 15.7
    BtoE 4.9

    Now we want to display the products and prices per today’s date.

    We use the following SELECT statement:

    SELECT Point, Unitdistance, Now() as Date FROM emp

    The result-set will look like this:

    Point Unitdistance Date
    AtoB 5.4 16/8/2012 12:54:59 AM
    XtoZ 15.7 26/8/2013 1:54:29 PM
    BtoE 4.9 17/1/2012 10:44:09 AM

sql round()

SQL Functions

homeBack

Next

SQL ROUND()

  • The ROUND() Function

    The ROUND() function is used to round a numeric field to the number of decimals specified.

    SQL ROUND() Syntax

    SELECT LEN(column_name) FROM table_name

    Parameter Description
    column_name Required. The field to round.
    decimals Required. Specifies the number of decimals to be returned.

  • SQL ROUND() Example

    Table Name ” Distance”

    Point distance(KM)
    AtoB 5.4
    XtoZ 15.7
    BtoE 4.9

    Now we want to display the Point and the Distance rounded to the nearest integer.

    We use the following SELECT statement:

    SELECT Point, ROUND(UnitPrice,0) as Unitdistance FROM Dictance

    The result-set will look like this:

    Point Unitdistance
    AtoB 5
    XtoZ 16
    BtoE 5

sql len()

SQL Functions

homeBack

Next

SQL LEN()

  • The LEN() Function

    The LEN() function is used to extract lenght from a text field.

    SQL LEN() Syntax

    SELECT LEN(column_name) FROM table_name

  • SQL LEN() Example

    Table Name ” emp”

    S.no Name1 Name2 City
    4857 Faran Khan Hyderabad
    2458 Peter Jhon Hyderabad
    8578 Sameer Ali Mumbai
    5874 Raj Kiran London

    Now we want to select the length of the values in the “City” column above.

    We use the following SELECT statement:

    SELECT LEN(City) as LengthOfCity FROM emp

    The result-set will look like this:

    LengthOfCity
    9
    9
    6
    6

sql mid()

SQL Functions

homeBack

Next

SQL MID()

  • The MID() Function

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

SQL MID() Syntax

SELECT MID(column_name,start[,length]) FROM table_name

Parameter Description
column_name Required. The field to extract characters from
start Required. Specifies the starting position (starts at 1)
length Optional. The number of characters to return. If omitted, the MID() function returns the rest of the text

  • SQL MID() Example

Table Name ” emp”

S.no Name1 Name2 City
4857 Faran Khan Hyderabad
2458 Peter Jhon Hyderabad
8578 Sameer Ali Mumbai
5874 Raj Kiran London

Now we want to extract the first four characters of the “City” column above.

We use the following SELECT statement:

SELECT MID(City,1,) as ShortCity FROM Persons

The result-set will look like this:

ShortCity
Hyd
Hyd
Mum
Lon

sql lcase()

SQL Functions

homeBack

Next

SQL LCASE()

  • The LCASE() Function

    The LCASE() function converts the value of a field to lowercase. 
    SQL LCASE() Syntax

    SELECT LCASE(column_name) FROM table_name

    Syntax for SQL Server

    SELECT LOWER(column_name) FROM table_name

  • SQL UCASE() Example

    Table Name ” emp”

    S.no Name1 Name2 City
    4857 Faran Khan Hyderabad
    2458 Peter Jhon Hyderabad
    8578 Sameer Ali Mumbai
    5874 Raj Kiran London

    Now we want to select the content of the “Name2” and “Name1” columns above, and convert the “Name1” column to lowercase.

    We use the following SELECT statement:

    SELECT LCASE(Name1) as Name1,Name2 FROM emp

    The result-set will look like this:

    Table Name ” emp”

    Name1 Name2
    faran Khan
    peter Jhon
    sameer Ali
    raj Kiran

sql ucase()

SQL Functions

homeBack

Next

SQL UCASE()

  • The UCASE() Function

    The UCASE() function converts the value of a field to uppercase.

    SQL UCASE() Syntax

    SELECT UCASE(column_name) FROM table_name

    Syntax for SQL Server

    SELECT UPPER(column_name) FROM table_name

  • SQL UCASE() Example

    Table Name ” emp”

    S.no Name1 Name2 City
    4857 Faran Khan Hyderabad
    2458 Peter Jhon Hyderabad
    8578 Sameer Ali Mumbai
    5874 Raj Kiran London

    Now we want to select the content of the “Name2” and “Name1” columns above, and convert the “Name1” column to uppercase.

    We use the following SELECT statement:

    SELECT UCASE(Name1) as Name1,Name2 FROM emp

    The result-set will look like this:

    Table Name ” emp”

    Name1 Name2
    FARAN Khan
    PETER Jhon
    SAMEER Ali
    RAJ Kiran

sql having

SQL Functions

homeBack

Next

SQL HAVING

  • The HAVING Clause

    The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

    SQL HAVING Syntax

    SELECT column_name, aggregate_function(column_name)
    FROM table_name
    WHERE column_name operator value
    GROUP BY column_name
    HAVING aggregate_function(column_name) operator value

  • SQL GROUP BY Example

    We have the following “Orders” table:

    OrderID Date Price Customer
    1 2012/12/12 1000 Joy
    2 2012/12/16 1600 Sam
    3 2012/12/22 1700 Arjun
    4 2012/12/23 1300 Joy
    5 2012/12/30 2300 John
    6 2012/12/31 1400 Joy

    Now we want to find if any of the customers have a total order of more than 3000.

    We use the following SQL statement:

    SELECT Customer,SUM(Price) FROM Orders
    GROUP BY Customer
    HAVING SUM(Price)>3000

    The result-set will look like this:

    Customer SUM(Price)
    Joy 3700

    Now we want to find if the customers “Hansen” or “Jensen” have a total order of more than 1500.

    We add an ordinary WHERE clause to the SQL statement:

    SELECT Customer,SUM(Price) FROM Orders
    WHERE Customer=’Arjun’ OR Customer=’Joy’
    GROUP BY Customer
    HAVING SUM(Price)>1400

    The result-set will look like this:

    Customer SUM(Price)
    Joy 3700
    Arjun 1700

sql group by statement

SQL Functions

homeBack

Next

The GROUP BY Statement

  • The AVG() Function

    The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

    SQL GROUP BY Syntax

    SELECT column_name, aggregate_function(column_name)
    FROM table_name
    WHERE column_name operator value
    GROUP BY column_name

  • SQL GROUP BY Example

    We have the following “Orders” table:

    OrderID Date Price Customer
    1 2012/12/12 1000 Joy
    2 2012/12/16 1600 Sam
    3 2012/12/22 1700 Arjun
    4 2012/12/23 1300 Joy
    5 2012/12/30 2300 John
    6 2012/12/31 1400 Joy

    Now we want to find the total sum (total order) of each customer.

    We will have to use the GROUP BY statement to group the customers.

    We use the following SQL statement:

    SELECT Customer,SUM(Price) FROM Orders
    GROUP BY Customer

    The result-set will look like this:

    Customer SUM(Price)
    Joy 3700
    Sam 1600
    Arjun 1700
    John 2300

    Let’s see what happens if we omit the GROUP BY statement:

    SELECT Customer,SUM(Price) FROM Orders

    The result-set will look like this:

    Customer SUM(Price)
    Joy 9300
    Sam 9300
    Arjun 9300
    Joy 9300
    John 9300
    Joy 9300

    The result-set above is not what we wanted.

    Explanation of why the above SELECT statement cannot be used: The SELECT statement above has two columns specified (Customer and SUM(Price). The “SUM(Price)” returns a single value (that is the total sum of the “Price” column), while “Customer” returns 6 values (one value for each row in the “Orders” table). This will therefore not give us the correct result. However, you have seen that the GROUP BY statement solves this problem.

  • GROUP BY More Than One Column

    We can also use the GROUP BY statement on more than one column, like this:

    SELECT Customer,Date,SUM(Price) FROM Orders
    GROUP BY Customer,Date