The UCASE() Function
The UCASE() function converts the value of a field to uppercase.
Syntax
SELECT UCASE(column_name) FROM table_name;
The LCASE() Function
The LCASE() function converts the value of a field to lowercase.
Syntax
SELECT LCASE(column_name) FROM table_name;
MID() Example
The following SQL statement selects the first four characters from the "City" column from the
"Customers" table:
Example
SELECT MID(City,1,4) AS ShortCity
FROM Customers;
The LEN() Function
The LEN() function returns the length of the value in a text field.
Syntax
SELECT LEN(column_name) FROM table_name;
SELECT CustomerName,LEN(Address) as LengthOfAddress
FROM Customers;
The ROUND() Function
The ROUND() function is used to round a numeric field to the number of decimals specified.
SELECT ROUND(column_name,decimals) FROM table_name;
SELECT ProductName, ROUND(Price,0) AS RoundedPrice
FROM Products;
ABS(X)
The ABS() function returns the absolute value of X. Consider the following
example:
mysql> SELECT ABS(2);
+---------------------------------------------------------+
| ABS(2) |
+---------------------------------------------------------+
| 2 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT ABS(-2);
+---------------------------------------------------------+
| ABS(2) |
+---------------------------------------------------------+
| 2 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
TRUNCATE(X,D)
This function is used to return the value of X truncated to D number of
decimal places. If D is 0, then the decimal point is removed. If D is negative,
then D number of values in the integer part of the value is truncated.
Consider the following example:
mysql>SELECT TRUNCATE(7.536432,2);
+---------------------------------------------------------+
| TRUNCATE(7.536432,2) |
+---------------------------------------------------------+
| 7.53 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
SQRT(X)
This function returns the non-negative square root of X. Consider the
following example:
mysql>SELECT SQRT(49);
+---------------------------------------------------------+
| SQRT(49) |
+---------------------------------------------------------+
| 7 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
FLOOR(X)
This function returns the largest integer value that is not greater than X.
mysql>SELECT FLOOR(7.55);
+---------------------------------------------------------+
| FLOOR(7.55) |
+---------------------------------------------------------+
| 7 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
POW(X,Y)
POWER(X,Y)
These two functions return the value of X raised to the power of Y.
mysql> SELECT POWER(3,3);
+---------------------------------------------------------+
| POWER(3,3) |
+---------------------------------------------------------+
| 27 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
MOD(N,M)
This function returns the remainder of N divided by M. Consider the
following example:
mysql>SELECT MOD(29,3);
+---------------------------------------------------------+
| MOD(29,3) |
+---------------------------------------------------------+
| 2 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
OCT(N)
The OCT() function returns the string representation of the octal number N.
This is equivalent to using CONV(N,10,8).
mysql>SELECT OCT(12);
+---------------------------------------------------------+
| OCT(12) |
+---------------------------------------------------------+
| 14 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
CEIL(X)
CEILING(X)
This function returns the smallest integer value that is not smaller than X.
Consider the following example:
mysql> SELECT CEILING(3.46);
+---------------------------------------------------------+
| CEILING(3.46) |
+---------------------------------------------------------+
| 4 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT CEIL(-6.43);
+---------------------------------------------------------+
| CEIL(-6.43) |
+---------------------------------------------------------+
| -6 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
Date Function:
SQL Dates
The most difficult part when working with dates is to be sure that the format of
the date you are trying to insert, matches the format of the date column in the
database.
As long as your data contains only the date portion, your queries will work as
expected. However, if a time portion is involved, it gets complicated.
Before talking about the complications of querying for dates, we will look at the
most important built-in functions for working with dates.
MySQL Date Functions
The following table lists the most important built-in date functions in MySQL:
Function Description
NOW() Returns the current date and time
CURDATE() Returns the current date
CURTIME() Returns the current time
DATE() Extracts the date part of a date or date/time expression
EXTRACT() Returns a single part of a date/time
DATE_ADD() Adds a specified time interval to a date
DATE_SUB() Subtracts a specified time interval from a date
DATEDIFF() Returns the number of days between two dates
DATE_FORMAT() Displays date/time data in different formats
SQL Server Date Functions
The following table lists the most important built-in date functions in SQL Server:
Function Description
GETDATE() Returns the current date and time
DATEPART() Returns a single part of a date/time
DATEADD() Adds or subtracts a specified time interval from a date
DATEDIFF() Returns the time between two dates
CONVERT() Displays date/time data in different formats
SQL Date Data Types
MySQL comes with the following data types for storing a date or a date/time
value in the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
YEAR - format YYYY or YY
SQL Server comes with the following data types for storing a date or a
date/time value in the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS
TIMESTAMP - format: a unique number
Note: The date types are chosen for a column when you create a new table in
your database!
For an overview of all data types available, go to our complete Data Types
reference.
SQL Working with Dates
You can compare two dates easily if there is no time component involved!
Assume we have the following "Orders" table:
OrderId ProductName OrderDate
1 Geitost 2008-11-11
2 Camembert Pierrot 2008-11-09
3 Mozzarella di Giovanni 2008-11-11
4 Mascarpone Fabioli 2008-10-29
Now we want to select the records with an OrderDate of "2008-11-11" from the
table above.
We use the following SELECT statement:
SELECT * FROM Orders WHERE OrderDate='2008-11-11'
The result-set will look like this:
OrderId ProductName OrderDate
1 Geitost 2008-11-11
3 Mozzarella di Giovanni 2008-11-11
Now, assume that the "Orders" table looks like this (notice the time component
in the "OrderDate" column):
OrderId ProductName OrderDate
1 Geitost 2008-11-11 13:23:44
2 Camembert Pierrot 2008-11-09 15:45:21
3 Mozzarella di Giovanni 2008-11-11 11:12:01
4 Mascarpone Fabioli 2008-10-29 14:56:59
If we use the same SELECT statement as above:
SELECT * FROM Orders WHERE OrderDate='2008-11-11'