You are on page 1of 6

MySQL Date Functions

The following table lists the most important built-in date functions in MySQL: Function NOW() CURDATE() CURTIME() DATE() EXTRACT() DATE_ADD() DATE_SUB() DATEDIFF() DATE_FORMAT() Description Returns the current date and time Returns the current date Returns the current time Extracts the date part of a date or date/time expression Returns a single part of a date/time Adds a specified time interval to a date Subtracts a specified time interval from a date Returns the number of days between two dates 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 GETDATE() DATEPART() DATEADD() DATEDIFF() CONVERT() Description Returns the current date and time Returns a single part of a date/time Adds or subtracts a specified time interval from a date Returns the time between two dates Displays date/time data in different formats

SQL Functions
The AVG() Function
The AVG() function returns the average value of a numeric column.

SQL AVG() Syntax


SELECT AVG(column_name) FROM table_name

The COUNT() Function


SQL COUNT(column_name) Syntax
The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column:

SELECT COUNT(column_name) FROM table_name

The FIRST() Function


The FIRST() function returns the first value of the selected column.

SQL FIRST() Syntax


SELECT FIRST(column_name) FROM table_name

The LAST() Function


The LAST() function returns the last value of the selected column.

SQL LAST() Syntax


SELECT LAST(column_name) FROM table_name

The MAX() Function


The MAX() function returns the largest value of the selected column.

SQL MAX() Syntax


SELECT MAX(column_name) FROM table_name

The MIN() Function


The MIN() function returns the smallest value of the selected column.

SQL MIN() Syntax


SELECT MIN(column_name) FROM table_name

The SUM() Function


The SUM() function returns the total sum of a numeric column.

SQL SUM() Syntax


SELECT SUM(column_name) FROM table_name

The GROUP BY Statement


The GROUP BY statement is used in conjunction with the aggregate functions to group the resultset 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

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

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

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

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

The LEN() Function


The LEN() function returns the length of the value in a text field.

SQL LEN() Syntax


SELECT LEN(column_name) FROM table_name

The ROUND() Function


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

SQL ROUND() Syntax


SELECT ROUND(column_name,decimals) FROM table_name

The NOW() Function


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

SQL NOW() Syntax


SELECT NOW() FROM table_name

1) What does SQL stand for? 2) Which SQL statement is used to extract data from a database?

3) Which SQL statement is used to update data in a database? 4) Which SQL statement is used to delete data from a database?

5) Which SQL statement is used to insert new data in a database? 6) With SQL, how do you select a column named "FirstName" from a table named "Persons"?

7) With SQL, how do you select all the columns from a table named "Persons"? 8) With SQL, how do you select all the records from a table named "Persons" where the value of the column "FirstName" is "Peter"?

9) With SQL, how do you select all the records from a table named "Persons" where the value of the column "FirstName" starts with an "a"? 10) The OR operator displays a record if ANY conditions listed are true. The AND operator displays a record if ALL of the conditions listed are true

11) With SQL, how do you select all the records from a table named "Persons" where the "FirstName" is "Peter" and the "LastName" is "Jackson"? 12) With SQL, how do you select all the records from a table named "Persons" where the "LastName" is alphabetically between (and including) "Hansen" and "Pettersen"?

13) Which SQL statement is used to return only different values? 14) Which SQL keyword is used to sort the result-set?

15) With SQL, how can you return all the records from a table named "Persons" sorted descending by "FirstName"? 16) With SQL, how can you insert a new record into the "Persons" table?

17) With SQL, how can you insert "Olsen" as the "LastName" in the "Persons" table? 18) How can you change "Hansen" into "Nilsen" in the "LastName" column in the Persons table?

You might also like