Loading lesson path
SQL
SQL References focused on SQL Data Types for MySQL, SQL Server, and MS Access and related concepts.
SQL Data Types for MySQL, SQL Server, and MS Access
This SQL keywords reference contains the reserved words in SQL. SQL Keywords Keyword Description ADD Adds a column in an existing table ADD CONSTRAINT Adds a constraint after a table is already creat…
ADD Keyword ❮ SQL Keywords Next ADD The ADD command is used to add a column in an existing table. Example Add an "Email" column to the "Customers" table: ALTER TABLE Customers ADD Email varchar(255);…
ADD CONSTRAINT Keyword Previous ❮ SQL Keywords Next ADD CONSTRAINT The ADD CONSTRAINT command is used to create a constraint after a table is already created. The following SQL adds a constraint name…
ALL Keyword Previous ❮ SQL Keywords Next ALL The ALL command returns true if all of the subquery values meet the condition. The following SQL statement returns TRUE and lists the productnames if ALL…
ALTER Keyword Previous ❮ SQL Keywords Next ALTER TABLE The ALTER TABLE command adds, deletes, or modifies columns in a table. The ALTER TABLE command also adds and deletes various constraints in a ta…
ALTER COLUMN Keyword Previous ❮ SQL Keywords Next ALTER COLUMN The ALTER COLUMN command is used to change the data type of a column in a table. The following SQL changes the data type of the column n…
ALTER TABLE Keyword Previous ❮ SQL Keywords Next ALTER TABLE The ALTER TABLE command adds, deletes, or modifies columns in a table. The ALTER TABLE command also adds and deletes various constraints i…
ALTER VIEW Keyword Previous ❮ SQL Keywords Next ALTER VIEW In SQL Server, a view can be updated with the ALTER VIEW command. The following SQL adds the "City" column to the "Brazil Customers" view: E…
AND Keyword Previous ❮ SQL Keywords Next AND The AND command is used with WHERE to only include rows where both conditions is true. The following SQL statement selects all fields from "Customers" whe…
ANY Keyword Previous ❮ SQL Keywords Next ANY The ANY command returns true if any of the subquery values meet the condition. The following SQL statement returns TRUE and lists the productnames if it f…
AS Keyword Previous ❮ SQL Keywords Next AS The AS command is used to rename a column or table with an alias. An alias only exists for the duration of the query. Alias for Columns The following SQL st…
ASC Keyword Previous ❮ SQL Keywords Next ASC The ASC command is used to sort the data returned in ascending order. The following SQL statement selects all the columns from the "Customers" table, sort…
SQL BACKUP DATABASE Keyword
BETWEEN Keyword Previous ❮ SQL Keywords Next BETWEEN The BETWEEN command is used to select values within a given range. The values can be numbers, text, or dates. The BETWEEN command is inclusive: be…
CASE Keyword Previous ❮ SQL Keywords Next CASE The CASE command is used is to create different output based on conditions. The following SQL goes through several conditions and returns a value when t…
CHECK Keyword Previous ❮ SQL Keywords Next CHECK The CHECK constraint limits the value that can be placed in a column. SQL CHECK on CREATE TABLE The following SQL creates a CHECK constraint on the "A…
COLUMN Keyword Previous ❮ SQL Keywords Next ALTER COLUMN The ALTER COLUMN command is used to change the data type of a column in a table. The following SQL changes the data type of the column named "…
CONSTRAINT Keyword Previous ❮ SQL Keywords Next ADD CONSTRAINT The ADD CONSTRAINT command is used to create a constraint after a table is already created. The following SQL adds a constraint named "P…
SQL CREATE Keyword
SQL CREATE DATABASE Keyword
CREATE INDEX Keyword Previous ❮ SQL Keywords Next CREATE INDEX The CREATE INDEX command is used to create indexes in tables (allows duplicate values). Indexes are used to retrieve data from the datab…
CREATE OR REPLACE VIEW Keyword Previous ❮ SQL Keywords Next CREATE OR REPLACE VIEW In MySQL and Oracle, a view can be updated with the CREATE OR REPLACE VIEW command. The following SQL adds the "City…
CREATE TABLE Keyword Previous ❮ SQL Keywords Next CREATE TABLE The CREATE TABLE command creates a new table in the database. The following SQL creates a table called "Persons" that contains five colu…
CREATE PROCEDURE Keyword Previous ❮ SQL Keywords Next CREATE PROCEDURE The CREATE PROCEDURE command is used to create a stored procedure. A stored procedure is a prepared SQL code that you can save,…
CREATE UNIQUE INDEX Keyword Previous ❮ SQL Keywords Next CREATE UNIQUE INDEX The CREATE UNIQUE INDEX command creates a unique index on a table (no duplicate values allowed) Indexes are used to retrie…
CREATE VIEW Keyword Previous ❮ SQL Keywords Next CREATE VIEW The CREATE VIEW command creates a view. A view is a virtual table based on the result set of an SQL statement. The following SQL creates a…
SQL DATABASE Keyword
DEFAULT Keyword Previous ❮ SQL Keywords Next DEFAULT The DEFAULT constraint provides a default value for a column. The default value will be added to all new records if no other value is specified. S…
DELETE Keyword Previous ❮ SQL Keywords Next DELETE The DELETE command is used to delete existing records in a table. The following SQL statement deletes the customer "Alfreds Futterkiste" from the "C…
DESC Keyword Previous ❮ SQL Keywords Next DESC The DESC command is used to sort the data returned in descending order. The following SQL statement selects all the columns from the "Customers" table,…
SELECT DISTINCT Keyword Previous ❮ SQL Keywords Next SELECT DISTINCT The SELECT DISTINCT command returns only distinct (different) values in the result set. The following SQL statement selects only t…
DROP Keyword Previous ❮ SQL Keywords Next DROP COLUMN The DROP COLUMN command is used to delete a column in an existing table. The following SQL deletes the "ContactName" column from the "Customers"…
DROP COLUMN Keyword Previous ❮ SQL Keywords Next DROP COLUMN The DROP COLUMN command is used to delete a column in an existing table. The following SQL deletes the "ContactName" column from the "Cust…
DROP CONSTRAINT Keyword Previous ❮ SQL Keywords Next DROP CONSTRAINT The DROP CONSTRAINT command is used to delete a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK constraint. DROP a UNIQUE Constraint To…
DROP DATABASE Keyword Previous ❮ SQL Keywords Next DROP DATABASE The DROP DATABASE command is used to delete an existing SQL database. The following SQL drops a database named "testDB": Example DROP…
DROP DEFAULT Keyword Previous ❮ SQL Keywords Next DROP DEFAULT The DROP DEFAULT command is used to delete a DEFAULT constraint. To drop a DEFAULT constraint, use the following SQL: SQL Server / Oracl…
DROP INDEX Keyword Previous ❮ SQL Keywords Next DROP INDEX The DROP INDEX command is used to delete an index in a table. MS Access: DROP INDEX index_name ON table_name ; SQL Server: DROP INDEX table_…
DROP TABLE and TRUNCATE TABLE Keywords Previous ❮ SQL Keywords Next DROP TABLE The DROP TABLE command deletes a table in the database. The following SQL deletes the table "Shippers": Example DROP TAB…
DROP VIEW Keyword Previous ❮ SQL Keywords Next DROP VIEW The DROP VIEW command deletes a view. The following SQL drops the "Brazil Customers" view: Example DROP VIEW [Brazil Customers]; Previous ❮ SQ…
EXEC Keyword Previous ❮ SQL Keywords Next EXEC The EXEC command is used to execute a stored procedure. The following SQL executes a stored procedure named "SelectAllCustomers": Example EXEC SelectAll…
EXISTS Keyword Previous ❮ SQL Keywords Next EXISTS The EXISTS command tests for the existence of any record in a subquery, and returns true if the subquery returns one or more records. The following…
FOREIGN KEY Keyword Previous ❮ SQL Keywords Next FOREIGN KEY The FOREIGN KEY constraint is a key used to link two tables together. A FOREIGN KEY is a field (or collection of fields) in one table that…
FROM Keyword Previous ❮ SQL Keywords Next FROM The FROM command is used to specify which table to select or delete data from. The following SQL statement selects the "CustomerName" and "City" columns…
FULL OUTER JOIN Keyword Previous ❮ SQL Keywords Next FULL OUTER JOIN The FULL OUTER JOIN command returns all rows when there is a match in either left table or right table. The following SQL statemen…
GROUP BY Keyword Previous ❮ SQL Keywords Next GROUP BY The GROUP BY command is used to group the result set (used with aggregate functions: COUNT, MAX, MIN, SUM, AVG). The following SQL lists the num…
HAVING Keyword Previous ❮ SQL Keywords Next HAVING The HAVING command is used instead of WHERE with aggregate functions. The following SQL lists the number of customers in each country. Only include…
IN Keyword Previous ❮ SQL Keywords Next IN The IN operator is used in the WHERE clause to check if a specified column's value matches any value within a provided list. The IN operator functions as a…
INDEX Keyword Previous ❮ SQL Keywords Next CREATE INDEX The CREATE INDEX command is used to create indexes in tables (allows duplicate values). Indexes are used to retrieve data from the database ver…
INNER JOIN Keyword Previous ❮ SQL Keywords Next INNER JOIN The INNER JOIN command returns rows that have matching values in both tables. The following SQL selects all orders with customer information…
INSERT INTO Keyword Previous ❮ SQL Keywords Next INSERT INTO The INSERT INTO command is used to insert new rows in a table. The following SQL inserts a new record in the "Customers" table: Example IN…
INSERT INTO SELECT Keyword Previous ❮ SQL Keywords Next INSERT INTO SELECT The INSERT INTO SELECT command copies data from one table and inserts it into another table. The following SQL copies "Suppl…
SQL IS NULL Keyword
IS NOT NULL Keyword Previous ❮ SQL Keywords Next IS NOT NULL The IS NOT NULL command is used to test for non-empty values (NOT NULL values). The following SQL lists all customers with a value in the…
JOIN Keyword Previous ❮ SQL Keywords Next INNER JOIN The INNER JOIN command returns rows that have matching values in both tables. The following SQL selects all orders with customer information: Exam…
LEFT JOIN Keyword Previous ❮ SQL Keywords Next LEFT JOIN The LEFT JOIN command returns all rows from the left table, and the matching rows from the right table. The result is NULL from the right side…
LIKE Keyword Previous ❮ SQL Keywords Next LIKE The LIKE command is used in a WHERE clause to search for a specified pattern in a column. You can use two wildcards with LIKE % - Represents zero, one,…
SELECT TOP, LIMIT and ROWNUM Keywords Previous ❮ SQL Keywords Next SELECT TOP, LIMIT and ROWNUM The LIMIT, SELECT TOP or ROWNUM command is used to specify the number of records to return. Note: SQL S…
NOT Keyword Previous ❮ SQL Keywords Next NOT The NOT command is used with WHERE to only include rows where a condition is not true. The following SQL statement selects all fields from "Customers" whe…
NOT NULL Keyword Previous ❮ SQL Keywords Next NOT NULL The NOT NULL constraint enforces a column to not accept NULL values, which means that you cannot insert or update a record without adding a valu…
OR Keyword Previous ❮ SQL Keywords Next OR The OR command is used with WHERE to include rows where either condition is true. The following SQL statement selects all fields from "Customers" where city…
ORDER BY Keyword Previous ❮ SQL Keywords Next ORDER BY The ORDER BY command is used to sort the result set in ascending or descending order. The ORDER BY command sorts the result set in ascending ord…
FULL OUTER JOIN Keyword Previous ❮ SQL Keywords Next FULL OUTER JOIN The FULL OUTER JOIN command returns all rows when there is a match in either left table or right table. The following SQL statemen…
PRIMARY KEY Keyword Previous ❮ SQL Keywords Next PRIMARY KEY The PRIMARY KEY constraint uniquely identifies each record in a table. A table can have only one primary key, which may consist of one sin…
CREATE PROCEDURE Keyword Previous ❮ SQL Keywords Next CREATE PROCEDURE The CREATE PROCEDURE command is used to create a stored procedure. A stored procedure is a prepared SQL code that you can save,…
RIGHT JOIN Keyword Previous ❮ SQL Keywords Next RIGHT JOIN The RIGHT JOIN command returns all rows from the right table, and the matching records from the left table. The result is NULL from the left…
SELECT TOP, LIMIT and ROWNUM Keywords Previous ❮ SQL Keywords Next SELECT TOP, LIMIT and ROWNUM The LIMIT, SELECT TOP or ROWNUM command is used to specify the number of records to return. Note: SQL S…
SELECT Keyword Previous ❮ SQL Keywords Next SELECT The SELECT command is used to select data from a database. The data returned is stored in a result table, called the result set. The following SQL s…
SELECT DISTINCT Keyword Previous ❮ SQL Keywords Next SELECT DISTINCT The SELECT DISTINCT command returns only distinct (different) values in the result set. The following SQL statement selects only t…
SELECT INTO Keyword Previous ❮ SQL Keywords Next SELECT INTO The SELECT INTO command copies data from one table and inserts it into a new table. The following SQL statement creates a backup copy of C…
SELECT TOP, LIMIT and ROWNUM Keywords Previous ❮ SQL Keywords Next SELECT TOP, LIMIT and ROWNUM The LIMIT, SELECT TOP or ROWNUM command is used to specify the number of records to return. Note: SQL S…
SET Keyword Previous ❮ SQL Keywords Next SET The SET command is used with UPDATE to specify which columns and values that should be updated in a table. The following SQL updates the first customer (C…
TABLE Keyword Previous ❮ SQL Keywords Next CREATE TABLE The CREATE TABLE command creates a new table in the database. The following SQL creates a table called "Persons" that contains five columns: Pe…
SELECT TOP, LIMIT and ROWNUM Keywords Previous ❮ SQL Keywords Next SELECT TOP, LIMIT and ROWNUM The LIMIT, SELECT TOP or ROWNUM command is used to specify the number of records to return. Note: SQL S…
DROP TABLE and TRUNCATE TABLE Keywords Previous ❮ SQL Keywords Next DROP TABLE The DROP TABLE command deletes a table in the database. The following SQL deletes the table "Shippers": Example DROP TAB…
UNION Keyword Previous ❮ SQL Keywords Next UNION The UNION command combines the result set of two or more SELECT statements (only distinct values) The following SQL statement returns the cities (only…
UNION ALL Keyword Previous ❮ SQL Keywords Next UNION ALL The UNION ALL command combines the result set of two or more SELECT statements (allows duplicate values). The following SQL statement returns…
UNIQUE Keyword Previous ❮ SQL Keywords Next UNIQUE The UNIQUE constraint ensures that all values in a column are unique. SQL UNIQUE Constraint on CREATE TABLE The following SQL creates a UNIQUE const…
UPDATE Keyword Previous ❮ SQL Keywords Next UPDATE The UPDATE command is used to update existing rows in a table. The following SQL statement updates the first customer (CustomerID = 1) with a new co…
VALUES Keyword Previous ❮ SQL Keywords Next VALUES The VALUES command specifies the values of an INSERT INTO statement. The following SQL inserts a new record in the "Customers" table: Example INSERT…
VIEW Keyword Previous ❮ SQL Keywords Next CREATE VIEW In SQL, a view is a virtual table based on the result set of an SQL statement. The CREATE VIEW command creates a view. The following SQL creates…
WHERE Keyword Previous ❮ SQL Keywords Next SELECT The WHERE command filters a result set to include only records that fulfill a specified condition. The following SQL statement selects all the custom…
MySQL has many built-in functions. This reference contains string, numeric, date, and some advanced functions in MySQL. MySQL String Functions Function Description ASCII Returns the ASCII value for t…
Example Return the ASCII value of the first character in "CustomerName": SELECT ASCII(CustomerName) AS NumCodeOfFirstChar FROM Customers; Definition and Usage The ASCII() function returns the ASCII v…
Example Return the length of the string: SELECT CHAR_LENGTH("SQL Tutorial") AS LengthOfString; Definition and Usage The CHAR_LENGTH() function return the length of a string (in characters). Note: Thi…
Example Return the length of the string: SELECT CHARACTER_LENGTH("SQL Tutorial") AS LengthOfString; Definition and Usage The CHARACTER_LENGTH() function return the length of a string (in characters).…
Example Add several strings together: SELECT CONCAT("SQL ", "Tutorial ", "is ", "fun!") AS ConcatenatedString; Definition and Usage The CONCAT() function adds two or more expressions together. Note:…
Example Add several expressions together, and add a "-" separator between them: SELECT CONCAT_WS("-", "SQL", "Tutorial", "is", "fun!") AS ConcatenatedString; Definition and Usage The CONCAT_WS() func…
Example Return the index position of "q" in the string list: SELECT FIELD("q", "s", "q", "l"); Definition and Usage The FIELD() function returns the index position of a value in a list of values. Thi…
Example Search for "q" within the list of strings: SELECT FIND_IN_SET("q", "s,q,l"); Definition and Usage The FIND_IN_SET() function returns the position of a string within a list of strings. Syntax…
Example Format the number as "#,###,###.##" (and round with two decimal places): SELECT FORMAT(250500.5634, 2); Definition and Usage The FORMAT() function formats a number to a format like "#,###,###…
Example Insert the string "Example" into the string "W3Schools.com". Replace the first nine characters: SELECT INSERT("W3Schools.com", 1, 9, "Example"); Definition and Usage The INSERT() function ins…
Example Search for "3" in string "W3Schools.com", and return position: SELECT INSTR("W3Schools.com", "3") AS MatchPosition; Definition and Usage The INSTR() function returns the position of the first…
Example Convert the text to lower-case: SELECT LCASE("SQL Tutorial is FUN!"); Definition and Usage The LCASE() function converts a string to lower-case. Note: The LOWER() function is a synonym for th…
MySQL LEFT() Function
Example Return the length of the string, in bytes: SELECT LENGTH("SQL Tutorial") AS LengthOfString; Definition and Usage The LENGTH() function returns the length of a string (in bytes). Syntax LENGTH…
Example Search for "3" in string "W3Schools.com", and return position: SELECT LOCATE("3", "W3Schools.com") AS MatchPosition; Definition and Usage The LOCATE() function returns the position of the fir…
Example Convert the text to lower-case: SELECT LOWER("SQL Tutorial is FUN!"); Definition and Usage The LOWER() function converts a string to lower-case. Note: The LCASE() function is equal to the LOW…
Example Left-pad the string with "ABC", to a total length of 20: SELECT LPAD("SQL Tutorial", 20, "ABC"); Definition and Usage The LPAD() function left-pads a string with another string, to a certain…
Example Remove leading spaces from a string: SELECT LTRIM(" SQL Tutorial") AS LeftTrimmedString; Definition and Usage The LTRIM() function removes leading spaces from a string. Syntax LTRIM( string )…
Example Extract a substring from a string (start at position 5, extract 3 characters): SELECT MID("SQL Tutorial", 5, 3) AS ExtractString; Definition and Usage The MID() function extracts a substring…
Example Search for "3" in string "W3Schools.com", and return position: SELECT POSITION("3" IN "W3Schools.com") AS MatchPosition; Definition and Usage The POSITION() function returns the position of t…
Example Repeat a string 3 times: SELECT REPEAT("SQL Tutorial", 3); Definition and Usage The REPEAT() function repeats a string as many times as specified. Syntax REPEAT( string, number ) Parameter Va…
Example Replace "SQL" with "HTML": SELECT REPLACE("SQL Tutorial", "SQL", "HTML"); Definition and Usage The REPLACE() function replaces all occurrences of a substring within a string, with a new subst…
Example Reverse a string: SELECT REVERSE("SQL Tutorial"); Definition and Usage The REVERSE() function reverses a string and returns the result. Syntax REVERSE( string ) Parameter Values Parameter Des…
MySQL RIGHT() Function
Example Right-pad the string with "ABC", to a total length of 20: SELECT RPAD("SQL Tutorial", 20, "ABC"); Definition and Usage The RPAD() function right-pads a string with another string, to a certai…
Example Remove trailing spaces from a string: SELECT RTRIM("SQL Tutorial ") AS RightTrimmedString; Definition and Usage The RTRIM() function removes trailing spaces from a string. Syntax RTRIM( strin…
Example Return a string with 10 space characters: SELECT SPACE(10); Definition and Usage The SPACE() function returns a string of the specified number of space characters. Syntax SPACE( number ) Para…
Example Compare two strings: SELECT STRCMP("SQL Tutorial", "SQL Tutorial"); Definition and Usage The STRCMP() function compares two strings. Syntax STRCMP( string1, string2 ) Parameter Values Paramet…
Example Extract a substring from a string (start at position 5, extract 3 characters): SELECT SUBSTR("SQL Tutorial", 5, 3) AS ExtractString; Definition and Usage The SUBSTR() function extracts a subs…
Example Extract a substring from a string (start at position 1, extract 3 characters): SELECT SUBSTRING("SQL Tutorial", 1, 3) AS ExtractString; Definition and Usage The SUBSTRING() function extracts…
Example Return a substring of a string before a specified number of delimiter occurs: SELECT SUBSTRING_INDEX("www.w3schools.com", ".", 1); Definition and Usage The SUBSTRING_INDEX() function returns…
Example Remove leading and trailing spaces from a string: SELECT TRIM(' SQL Tutorial ') AS TrimmedString; Definition and Usage The TRIM() function removes leading and trailing spaces from a string. S…
Example Convert the text to upper-case: SELECT UCASE("SQL Tutorial is FUN!"); Definition and Usage The UCASE() function converts a string to upper-case. Note: This function is equal to the UPPER() fu…
Example Convert the text to upper-case: SELECT UPPER("SQL Tutorial is FUN!"); Definition and Usage The UPPER() function converts a string to upper-case. Note: This function is equal to the UCASE() fu…
Example Return the absolute value of a number: SELECT ABS(-243.5); Definition and Usage The ABS() function returns the absolute (positive) value of a number. Syntax ABS( number ) Parameter Values Par…
Example Return the arc cosine of a number: SELECT ACOS(0.25); Definition and Usage The ACOS() function returns the arc cosine of a number. The specified number must be between -1 to 1, otherwise this…
Example Return the arc sine of a number: SELECT ASIN(0.25); Definition and Usage The ASIN() function returns the arc sine of a number. The specified number must be between -1 to 1, otherwise this fun…
Example Return the arc tangent of a number: SELECT ATAN(2.5); Definition and Usage The ATAN() function returns the arc tangent of one or two numbers. Syntax ATAN( number ) OR: ATAN( a, b ) Parameter…
Example Return the arc tangent of two values: SELECT ATAN2(0.50, 1); Definition and Usage The ATAN2() function returns the arc tangent of two numbers. Syntax ATAN2( a, b ) Parameter Values Parameter…
Example Return the average value for the "Price" column in the "Products" table: SELECT AVG(Price) AS AveragePrice FROM Products; Definition and Usage The AVG() function returns the average value of…
Example Return the smallest integer value that is greater than or equal to 25.75: SELECT CEIL(25.75); Definition and Usage The CEIL() function returns the smallest integer value that is bigger than o…
Example Return the smallest integer value that is greater than or equal to 25.75: SELECT CEILING(25.75); Definition and Usage The CEILING() function returns the smallest integer value that is bigger…
Example Return the cosine of a number: SELECT COS(2); Definition and Usage The COS() function returns the cosine of a number. Syntax COS( number ) Parameter Values Parameter Description number Requir…
Example Return the cotangent of a number: SELECT COT(6); Definition and Usage The COT() function returns the cotangent of a number. Syntax COT( number ) Parameter Values Parameter Description number…
Example Return the number of products in the "Products" table: SELECT COUNT(ProductID) AS NumberOfProducts FROM Products; Definition and Usage The COUNT() function returns the number of records retur…
Example Convert the radian value into degrees: SELECT DEGREES(1.5); Definition and Usage The DEGREES() function converts a value in radians to degrees. Note: See also the RADIANS() and PI() functions…
Example Integer division (10/5): SELECT 10 DIV 5; Definition and Usage The DIV function is used for integer division (x is divided by y). An integer value is returned. Syntax x DIV y Parameter Values…
MySQL EXP() Function
Example Return the largest integer value that is less than or equal to 25.75: SELECT FLOOR(25.75); Definition and Usage The FLOOR() function returns the largest integer value that is smaller than or…
Example Return the greatest value of the list of arguments: SELECT GREATEST(3, 12, 34, 8, 25); Definition and Usage The GREATEST() function returns the greatest value of the list of arguments. Note:…
Example Return the smallest value of the list of arguments: SELECT LEAST(3, 12, 34, 8, 25); Definition and Usage The LEAST() function returns the smallest value of the list of arguments. Note: See al…
Example Return the natural logarithm of 2: SELECT LN(2); Definition and Usage The LN() function returns the natural logarithm of a number. Note: See also the LOG() and EXP() functions. Syntax LN( num…
Example Return the natural logarithm of 2: SELECT LOG(2); Definition and Usage The LOG() function returns the natural logarithm of a specified number, or the logarithm of the number to the specified…
Example Return the base-10 logarithm of 2: SELECT LOG10(2); Definition and Usage The LOG10() function returns the natural logarithm of a number to base-10. Note: See also the LOG() function. Syntax L…
Example Return the base-2 logarithm of 6: SELECT LOG2(6); Definition and Usage The LOG2() function returns the natural logarithm of a number to base-2. Note: See also the LOG() function. Syntax LOG2(…
Example Find the price of the most expensive product in the "Products" table: SELECT MAX(Price) AS LargestPrice FROM Products; Definition and Usage The MAX() function returns the maximum value in a s…
Example Find the price of the cheapest product in the "Products" table: SELECT MIN(Price) AS SmallestPrice FROM Products; Definition and Usage The MIN() function returns the minimum value in a set of…
Example Return the remainder of 18/4: SELECT MOD(18, 4); Definition and Usage The MOD() function returns the remainder of a number divided by another number. Syntax MOD( x, y ) OR: x MOD y OR: x % y…
Example Return the value of PI: SELECT PI(); Definition and Usage The PI() function returns the value of PI. Note: See also the DEGREES() and RADIANS() functions. Syntax PI() Technical Details Works…
Example Return 4 raised to the second power: SELECT POW(4, 2); Definition and Usage The POW() function returns the value of a number raised to the power of another number. Note: This function is equa…
Example Return 4 raised to the second power: SELECT POWER(4, 2); Definition and Usage The POWER() function returns the value of a number raised to the power of another number. Note: This function is…
Example Convert a degree value into radians: SELECT RADIANS(180); Definition and Usage The RADIANS() function converts a degree value into radians. Note: See also the DEGREES() and PI() functions. Sy…
Example Return a random decimal number (no seed value - so it returns a completely random number >= 0 and <1): SELECT RAND(); Definition and Usage The RAND() function returns a random number between…
Example Round the number to 2 decimal places: SELECT ROUND(135.375, 2); Definition and Usage The ROUND() function rounds a number to a specified number of decimal places. Note: See also the FLOOR(),…
Example Return the sign of a number: SELECT SIGN(255.5); Definition and Usage The SIGN() function returns the sign of a number. This function will return one of the following: If number > 0, it retur…
Example Return the sine of a number: SELECT SIN(2); Definition and Usage The SIN() function returns the sine of a number. Syntax SIN( number ) Parameter Values Parameter Description number Required.…
Example Return the square root of a number: SELECT SQRT(64); Definition and Usage The SQRT() function returns the square root of a number. Syntax SQRT( number ) Parameter Values Parameter Description…
Example Return the sum of the "Quantity" field in the "OrderDetails" table: SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails; Definition and Usage The SUM() function calculates the sum of…
Example Return the tangent of a number: SELECT TAN(1.75); Definition and Usage The TAN() function returns the tangent of a number. Syntax TAN( number ) Parameter Values Parameter Description number R…
Example Return a number truncated to 2 decimal places: SELECT TRUNCATE(135.375, 2); Definition and Usage The TRUNCATE() function truncates a number to the specified number of decimal places. Note: Se…
Example Add 10 days to a date and return the date: SELECT ADDDATE("2017-06-15", INTERVAL 10 DAY); Definition and Usage The ADDDATE() function adds a time/date interval to a date and then returns the…
Example Add 2 seconds to a time and return the datetime: SELECT ADDTIME("2017-06-15 09:34:21", "2"); Definition and Usage The ADDTIME() function adds a time interval to a time/datetime and then retur…
Example Return the current date: SELECT CURDATE(); Definition and Usage The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric).…
Example Return the current date: SELECT CURRENT_DATE(); Definition and Usage The CURRENT_DATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (…
Example Return current time: SELECT CURRENT_TIME(); Definition and Usage The CURRENT_TIME() function returns the current time. Note: The time is returned as "HH-MM-SS" (string) or as HHMMSS.uuuuuu (n…
Example Return the current date and time: SELECT CURRENT_TIMESTAMP(); Definition and Usage The CURRENT_TIMESTAMP() function returns the current date and time. Note: The date and time is returned as "…
Example Return current time: SELECT CURTIME(); Definition and Usage The CURTIME() function returns the current time. Note: The time is returned as "HH-MM-SS" (string) or as HHMMSS.uuuuuu (numeric). N…
Example Extract the date part: SELECT DATE("2017-06-15"); Definition and Usage The DATE() function extracts the date part from a datetime expression. Syntax DATE( expression ) Parameter Values Parame…
Example Return the number of days between two date values: SELECT DATEDIFF("2017-06-25", "2017-06-15"); Definition and Usage The DATEDIFF() function returns the number of days between two date values…
Example Add 10 days to a date and return the date: SELECT DATE_ADD("2017-06-15", INTERVAL 10 DAY); Definition and Usage The DATE_ADD() function adds a time/date interval to a date and then returns th…
Example Format a date: SELECT DATE_FORMAT("2017-06-15", "%Y"); Definition and Usage The DATE_FORMAT() function formats a date as specified. Syntax DATE_FORMAT( date, format ) Parameter Values Paramet…
Example Subtract 10 days from a date and return the date: SELECT DATE_SUB("2017-06-15", INTERVAL 10 DAY); Definition and Usage The DATE_SUB() function subtracts a time/date interval from a date and t…
Example Return the day of the month for a date: SELECT DAY("2017-06-15"); Definition and Usage The DAY() function returns the day of the month for a given date (a number from 1 to 31). Note: This fun…
Example Return the weekday name for a date: SELECT DAYNAME("2017-06-15"); Definition and Usage The DAYNAME() function returns the weekday name for a given date. Syntax DAYNAME( date ) Parameter Value…
Example Return the day of the month for a date: SELECT DAYOFMONTH("2017-06-15"); Definition and Usage The DAYOFMONTH() function returns the day of the month for a given date (a number from 1 to 31).…
Example Return the weekday index for a date: SELECT DAYOFWEEK("2017-06-15"); Definition and Usage The DAYOFWEEK() function returns the weekday index for a given date (a number from 1 to 7). Note: 1=S…
Example Return the day of the year for a date: SELECT DAYOFYEAR("2017-06-15"); Definition and Usage The DAYOFYEAR() function returns the day of the year for a given date (a number from 1 to 366). Syn…
Example Extract the month from a date: SELECT EXTRACT(MONTH FROM "2017-06-15"); Definition and Usage The EXTRACT() function extracts a part from a given date. Syntax EXTRACT( part FROM date ) Paramet…
Example Return a date from a numeric representation of the day: SELECT FROM_DAYS(685467); Definition and Usage The FROM_DAYS() function returns a date from a numeric datevalue. The FROM_DAYS() functi…
Example Return the hour part of a datetime: SELECT HOUR("2017-06-20 09:34:00"); Definition and Usage The HOUR() function returns the hour part for a given date (from 0 to 838). Syntax HOUR( datetime…
Example Extract the last day of the month for the given date: SELECT LAST_DAY("2017-06-20"); Definition and Usage The LAST_DAY() function extracts the last day of the month for a given date. Syntax L…
Example Return current date and time: SELECT LOCALTIME(); Definition and Usage The LOCALTIME() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS"…
Example Return current date and time: SELECT LOCALTIMESTAMP(); Definition and Usage The LOCALTIMESTAMP() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD…
Example Create and return a date based on a year and a number of days value: SELECT MAKEDATE(2017, 3); Definition and Usage The MAKEDATE() function creates and returns a date based on a year and a nu…
Example Create and return a time value based on an hour, minute, and second value: SELECT MAKETIME(11, 35, 4); Definition and Usage The MAKETIME() function creates and returns a time based on an hour…
Example Return the microsecond part of a datetime: SELECT MICROSECOND("2017-06-20 09:34:00.000023"); Definition and Usage The MICROSECOND() function returns the microsecond part of a time/datetime (f…
Example Return the minute part of a datetime value: SELECT MINUTE("2017-06-20 09:34:00"); Definition and Usage The MINUTE() function returns the minute part of a time/datetime (from 0 to 59). Syntax…
Example Return the month part of a date: SELECT MONTH("2017-06-15"); Definition and Usage The MONTH() function returns the month part for a given date (a number from 1 to 12). Syntax MONTH( date ) Pa…
Example Return the name of the month for a date: SELECT MONTHNAME("2017-06-15"); Definition and Usage The MONTHNAME() function returns the name of the month for a given date. Syntax MONTHNAME( date )…
Example Return current date and time: SELECT NOW(); Definition and Usage The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or…
Example Add a specified number of months to a period: SELECT PERIOD_ADD(201703, 5); Definition and Usage The PERIOD_ADD() function adds a specified number of months to a period. The PERIOD_ADD() func…
Example Return the difference between two periods: SELECT PERIOD_DIFF(201710, 201703); Definition and Usage The PERIOD_DIFF() function returns the difference between two periods. The result will be i…
Example Return the quarter of the year for the date: SELECT QUARTER("2017-06-15"); Definition and Usage The QUARTER() function returns the quarter of the year for a given date value (a number from 1…
Example Return the seconds part of a datetime value: SELECT SECOND("2017-06-20 09:34:00.000023"); Definition and Usage The SECOND() function returns the seconds part of a time/datetime (from 0 to 59)…
Example Return a time value based on a specified seconds value: SELECT SEC_TO_TIME(1); Definition and Usage The SEC_TO_TIME() function returns a time value (in format HH:MM:SS) based on the specified…
Example Return a date based on a string and a format: SELECT STR_TO_DATE("August 10 2017", "%M %d %Y"); Definition and Usage The STR_TO_DATE() function returns a date based on a string and a format.…
Example Subtract 10 days from a date and return the date: SELECT SUBDATE("2017-06-15", INTERVAL 10 DAY); Definition and Usage The SUBDATE() function subtracts a time/date interval from a date and the…
Example Subtract 5.000001 seconds and return the datetime: SELECT SUBTIME("2017-06-15 10:24:21.000004", "5.000001"); Definition and Usage The SUBTIME() function subtracts time from a time/datetime ex…
Example Return the current date and time: SELECT SYSDATE(); Definition and Usage The SYSDATE() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH:MM:SS"…
Example Extract the time part from a time expression: SELECT TIME("19:30:10"); Definition and Usage The TIME() function extracts the time part from a given time/datetime. Note: This function returns…
Example Format a time: SELECT TIME_FORMAT("19:30:10", "%H %i %s"); Definition and Usage The TIME_FORMAT() function formats a time by a specified format. Syntax TIME_FORMAT( time, format ) Parameter V…
Example Convert a time value into seconds: SELECT TIME_TO_SEC("19:30:10"); Definition and Usage The TIME_TO_SEC() function converts a time value into seconds. Syntax TIME_TO_SEC( time ) Parameter Val…
Example Return the difference between two time expressions: SELECT TIMEDIFF("13:10:11", "13:10:10"); Definition and Usage The TIMEDIFF() function returns the difference between two time/datetime expr…
Example Return a datetime value based on the arguments: SELECT TIMESTAMP("2017-07-23", "13:10:11"); Definition and Usage The TIMESTAMP() function returns a datetime value based on a date or datetime…
Example Return the number of days between the date and year 0: SELECT TO_DAYS("2017-06-20"); Definition and Usage The TO_DAYS() function returns the number of days between a date and year 0 (date "00…
Example Return the week number for a date: SELECT WEEK("2017-06-15"); Definition and Usage The WEEK() function returns the week number for a given date (a number from 0 to 53). Syntax WEEK( date, fir…
Example Return the weekday number for a date: SELECT WEEKDAY("2017-06-15"); Definition and Usage The WEEKDAY() function returns the weekday number for a given date. Note: 0 = Monday, 1 = Tuesday, 2 =…
MySQL WEEKOFYEAR() Function
Example Return the year part of a date: SELECT YEAR("2017-06-15"); Definition and Usage The YEAR() function returns the year part for a given date (a number from 1000 to 9999). Syntax YEAR( date ) Pa…
Example Return the year and week number for a date: SELECT YEARWEEK("2017-06-15"); Definition and Usage The YEARWEEK() function returns the year and week number (a number from 0 to 53) for a given da…
Example Return a binary representation of 15: SELECT BIN(15); Definition and Usage The BIN() function returns a binary representation of a number, as a string value. Syntax BIN( number ) Parameter Va…
Example Convert a value to a binary string: SELECT BINARY "W3Schools.com"; Definition and Usage The BINARY function converts a value to a binary string. This function is equivalent to using CAST(valu…
Example Go through conditions and return a value when the first condition is met: SELECT OrderID, Quantity, CASE WHEN Quantity > 30 THEN "The quantity is greater than 30" WHEN Quantity = 30 THEN "The…
MySQL CAST() Function
Example Return the first non-null value in a list: SELECT COALESCE(NULL, NULL, NULL, 'W3Schools.com', NULL, 'Example.com'); Definition and Usage The COALESCE() function returns the first non-null val…
Example Return the unique connection ID for the current connection: SELECT CONNECTION_ID(); Definition and Usage The CONNECTION_ID() function returns the unique connection ID for the current connecti…
MySQL CONV() Function
MySQL CONVERT() Function
MySQL CURRENT_USER() Function
Example Return the name of the current (default) database: SELECT DATABASE(); Definition and Usage The DATABASE() function returns the name of the current database. If there is no current database, t…
Example Return "YES" if the condition is TRUE, or "NO" if the condition is FALSE: SELECT IF(500<1000, "YES", "NO"); Definition and Usage The IF() function returns a value if a condition is TRUE, or a…
Example Return the specified value IF the expression is NULL, otherwise return the expression: SELECT IFNULL(NULL, "W3Schools.com"); Definition and Usage The IFNULL() function returns a specified val…
Example Test whether an expression is NULL: SELECT ISNULL(NULL); Definition and Usage The ISNULL() function returns 1 or 0 depending on whether an expression is NULL. If expression is NULL, this func…
Example Return the AUTO_INCREMENT id of the last row that has been inserted in a table: SELECT LAST_INSERT_ID(); Definition and Usage The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of th…
Example Compare two expressions: SELECT NULLIF(25, 25); Definition and Usage The NULLIF() function compares two expressions and returns NULL if they are equal. Otherwise, the first expression is retu…
Example Return the current user name and host name for the MySQL connection: SELECT SESSION_USER(); Definition and Usage The SESSION_USER() function returns the current user name and host name for th…
Example Return the current user name and host name for the MySQL connection: SELECT SYSTEM_USER(); Definition and Usage The SYSTEM_USER() function returns the current user name and host name for the…
MySQL USER() Function
Example Return the current version of the MySQL database: SELECT VERSION(); Definition and Usage The VERSION() function returns the current version of the MySQL database, as a string. Syntax VERSION(…
SQL Server has many built-in functions. This reference contains string, numeric, date, conversion, and some advanced functions in SQL Server. SQL Server String Functions Function Description ASCII Re…
Example Return the ASCII value of the first character in "CustomerName": SELECT ASCII(CustomerName) AS NumCodeOfFirstChar FROM Customers; Definition and Usage The ASCII() function returns the ASCII v…
Example Return the character based on the number code 65: SELECT CHAR(65) AS CodeToCharacter; Definition and Usage The CHAR() function returns the character based on the ASCII code. Syntax CHAR( code…
Example Search for "t" in string "Customer", and return position: SELECT CHARINDEX('t', 'Customer') AS MatchPosition; Definition and Usage The CHARINDEX() function searches for a substring in a strin…
Example Add two strings together: SELECT CONCAT('W3Schools', '.com'); Definition and Usage The CONCAT() function adds two or more strings together. Note: See also Concat with the + operator and CONCA…
Example Add 2 strings together: SELECT 'W3Schools' + '.com'; Definition and Usage The + operator allows you to add two or more strings together. Note: See also the CONCAT() and CONCAT_WS() functions.…
Example Add strings together. Use '.' to separate the concatenated string values: SELECT CONCAT_WS('.', 'www', 'W3Schools', 'com'); Definition and Usage The CONCAT_WS() function adds two or more stri…
SQL Server DATALENGTH() Function
SQL Server DIFFERENCE() Function
Example Format a date: DECLARE @d DATETIME = '12/01/2018'; SELECT FORMAT (@d, 'd', 'en-US') AS 'US English Result', FORMAT (@d, 'd', 'no') AS 'Norwegian Result', FORMAT (@d, 'd', 'zu') AS 'Zulu Resul…
Example Extract 3 characters from a string (starting from left): SELECT LEFT('SQL Tutorial', 3) AS ExtractString; Definition and Usage The LEFT() function extracts a number of characters from a strin…
SQL Server LEN() Function
Example Convert the text to lower-case: SELECT LOWER('SQL Tutorial is FUN!'); Definition and Usage The LOWER() function converts a string to lower-case. Note: Also look at the UPPER() function. Synta…
Example Remove leading spaces from a string: SELECT LTRIM(' SQL Tutorial') AS LeftTrimmedString; Definition and Usage The LTRIM() function removes leading spaces from a string. Note: Also look at the…
Example Return the Unicode character based on the number code 65: SELECT NCHAR(65) AS NumberCodeToUnicode; Definition and Usage The NCHAR() function returns the Unicode character based on the number…
Example Return the position of a pattern in a string: SELECT PATINDEX('%schools%', 'W3Schools.com'); Definition and Usage The PATINDEX() function returns the position of a pattern in a string. If the…
Example Return a Unicode string with bracket delimiters (default): SELECT QUOTENAME('abcdef'); Definition and Usage The QUOTENAME() function returns a Unicode string with delimiters added to make the…
SQL Server REPLACE() Function
Example Repeat a string: SELECT REPLICATE('SQL Tutorial', 5); Definition and Usage The REPLICATE() function repeats a string a specified number of times. Syntax REPLICATE( string, integer ) Parameter…
Example Reverse a string: SELECT REVERSE('SQL Tutorial'); Definition and Usage The REVERSE() function reverses a string and returns the result. Syntax REVERSE( string ) Parameter Values Parameter Des…
Example Extract 3 characters from a string (starting from right): SELECT RIGHT('SQL Tutorial', 3) AS ExtractString; Definition and Usage The RIGHT() function extracts a number of characters from a st…
Example Remove trailing spaces from a string: SELECT RTRIM('SQL Tutorial ') AS RightTrimmedString; Definition and Usage The RTRIM() function removes trailing spaces from a string. Note: Also look at…
SQL Server SOUNDEX() Function
Example Return a string with 10 spaces: SELECT SPACE(10); Definition and Usage The SPACE() function returns a string of the specified number of space characters. SPACE( number ) Parameter Values Para…
Example Return a number as a string: SELECT STR(185); Definition and Usage The STR() function returns a number as a string. Syntax STR( number, length, decimals ) Parameter Values Parameter Descripti…
SQL Server STUFF() Function
Example Extract 3 characters from a string, starting in position 1: SELECT SUBSTRING('SQL Tutorial', 1, 3) AS ExtractString; Definition and Usage The SUBSTRING() function extracts some characters fro…
Example Return the string from the first argument AFTER the characters specified in the second argument are translated into the characters specified in the third argument: SELECT TRANSLATE('Monday',…
Example Remove leading and trailing spaces from a string: SELECT TRIM(' SQL Tutorial! ') AS TrimmedString; Definition and Usage The TRIM() function removes the space character OR other specified char…
Example Return an integer value (the Unicode value), for the first character of the input expression: SELECT UNICODE('Atlanta'); Definition and Usage The UNICODE() function returns an integer value (…
Example Convert the text to upper-case: SELECT UPPER('SQL Tutorial is FUN!'); Definition and Usage The UPPER() function converts a string to upper-case. Note: Also look at the LOWER() function. Synta…
Example Return the absolute value of a number: SELECT Abs(-243.5) AS AbsNum; Definition and Usage The ABS() function returns the absolute value of a number. Syntax ABS( number ) Parameter Values Para…
Example Return the arc cosine of a number: SELECT ACOS(0.25); Definition and Usage The ACOS() function returns the arc cosine of a number. The specified number must be between -1 to 1, otherwise this…
Example Return the arc sine of a number: SELECT ASIN(0.25); Definition and Usage The ASIN() function returns the arc sine of a number. The specified number must be between -1 to 1, otherwise this fun…
Example Return the arc tangent of a number: SELECT ATAN(2.5); Definition and Usage The ATAN() function returns the arc tangent of a number. Syntax ATAN( number ) Parameter Values Parameter Descriptio…
Example Return the arc tangent of two values: SELECT ATN2(0.50, 1); Definition and Usage The ATN2() function returns the arc tangent of two numbers. Syntax ATN2( a, b ) Parameter Values Parameter Des…
Example Return the average value for the "Price" column in the "Products" table: SELECT AVG(Price) AS AveragePrice FROM Products; Definition and Usage The AVG() function returns the average value of…
SQL Server CEILING() Function
Example Return the number of products in the "Products" table: SELECT COUNT(ProductID) AS NumberOfProducts FROM Products; Definition and Usage The COUNT() function returns the number of records retur…
Example Return the cosine of a number: SELECT COS(2); Definition and Usage The COS() function returns the cosine of a number. Syntax COS( number ) Parameter Values Parameter Description number Requir…
Example Return the cotangent of a number: SELECT COT(6); Definition and Usage The COT() function returns the cotangent of a number. Syntax COT( number ) Parameter Values Parameter Description number…
Example Convert a radian value into degrees: SELECT DEGREES(1.5); Definition and Usage The DEGREES() function converts a value in radians to degrees. Note: See also the RADIANS() and PI() functions.…
Example Return e raised to the power of 1: SELECT EXP(1); Definition and Usage The EXP() function returns e raised to the power of a specified number. The constant e (2.718281...), is the base of nat…
SQL Server FLOOR() Function
Example Return the natural logarithm of 2: SELECT LOG(2); Definition and Usage The LOG() function returns the natural logarithm of a specified number, or the logarithm of the number to the specified…
Example Return the base-10 logarithm of 2: SELECT LOG10(2); Definition and Usage The LOG10() function returns the natural logarithm of a number to base 10. Note: Also look at the LOG() function. Synt…
Example Find the price of the most expensive product in the "Products" table: SELECT MAX(Price) AS LargestPrice FROM Products; Definition and Usage The MAX() function returns the maximum value in a s…
Example Find the price of the cheapest product in the "Products" table: SELECT MIN(Price) AS SmallestPrice FROM Products; Definition and Usage The MIN() function returns the minimum value in a set of…
Example Return the value of PI: SELECT PI(); Definition and Usage The PI() function returns the value of PI. Note: Also look at the DEGREES() and RADIANS() functions. Syntax PI() Technical Details Wo…
Example Return 4 raised to the second power: SELECT POWER(4, 2); Definition and Usage The POWER() function returns the value of a number raised to the power of another number. Syntax POWER( a, b ) Pa…
Example Convert a degree value into radians: SELECT RADIANS(180); Definition and Usage The RADIANS() function converts a degree value into radians. Syntax RADIANS( number ) Parameter Values Parameter…
Example Return a random decimal number (no seed value - so it returns a completely random number >= 0 and <1): SELECT RAND(); Definition and Usage The RAND() function returns a random number between…
SQL Server ROUND() Function
Example Return the sign of a number: SELECT SIGN(255.5); Definition and Usage The SIGN() function returns the sign of a number. This function will return one of the following: If number > 0, it retur…
Example Return the sine of a number: SELECT SIN(2); Definition and Usage The SIN() function returns the sine of a number. Syntax SIN( number ) Parameter Values Parameter Description number Required.…
Example Return the square root of a number: SELECT SQRT(64); Definition and Usage The SQRT() function returns the square root of a number. Syntax SQRT( number ) Parameter Values Parameter Description…
Example Return the square of a number: SELECT SQUARE(64); Definition and Usage The SQUARE() function returns the square of a number. Syntax SQUARE( number ) Parameter Values Parameter Description num…
Example Return the sum of the "Quantity" field in the "OrderDetails" table: SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails; Definition and Usage The SUM() function calculates the sum of…
Example Return the tangent of a number: SELECT TAN(1.75); Definition and Usage The TAN() function returns the tangent of a number. Syntax TAN( number ) Parameter Values Parameter Description number R…
SQL Server CURRENT_TIMESTAMP Function
Example Add one year to a date, then return the date: SELECT DATEADD(year, 1, '2017/08/25') AS DateAdd; Definition and Usage The DATEADD() function adds a time/date interval to a date and then return…
Example Return the difference between two date values, in years: SELECT DATEDIFF(year, '2017/08/25', '2011/08/25') AS DateDiff; Definition and Usage The DATEDIFF() function returns the difference bet…
Example Return a date from its parts: SELECT DATEFROMPARTS(2018, 10, 31) AS DateFromParts; Definition and Usage The DATEFROMPARTS() function returns a date from the specified parts (year, month, and…
Example Return a specified part of a date: SELECT DATENAME(year, '2017/08/25') AS DatePartString; Definition and Usage The DATENAME() function returns a specified part of a date. This function return…
Example Return a specified part of a date: SELECT DATEPART(year, '2017/08/25') AS DatePartInt; Definition and Usage The DATEPART() function returns a specified part of a date. This function returns t…
Example Return the day of the month for a date: SELECT DAY('2017/08/25') AS DayOfMonth; Definition and Usage The DAY() function returns the day of the month (from 1 to 31) for a specified date. Synta…
SQL Server GETDATE() Function
Example Return the current UTC date and time: SELECT GETUTCDATE(); Definition and Usage The GETUTCDATE() function returns the current database system UTC date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm'…
Example Check if the expression is a valid date: SELECT ISDATE('2017-08-25'); Definition and Usage The ISDATE() function checks an expression and returns 1 if it is a valid date, otherwise 0. Syntax…
Example Return the month part of a date: SELECT MONTH('2017/08/25') AS Month; Definition and Usage The MONTH() function returns the month part for a specified date (a number from 1 to 12). Syntax MON…
Example Return the date and time of the SQL Server: SELECT SYSDATETIME() AS SysDateTime; Definition and Usage The SYSDATETIME() function returns the date and time of the computer where the SQL Server…
Example Return the year part of a date: SELECT YEAR('2017/08/25') AS Year; Definition and Usage The YEAR() function returns the year part for a specified date. Syntax YEAR( date ) Parameter Values Pa…
SQL Server CAST() Function
Example Return the first non-null value in a list: SELECT COALESCE(NULL, NULL, NULL, 'W3Schools.com', NULL, 'Example.com'); Definition and Usage The COALESCE() function returns the first non-null val…
SQL Server CONVERT() Function
Example Return the name of the current user in the SQL Server database: SELECT CURRENT_USER; Definition and Usage The CURRENT_USER function returns the name of the current user in the SQL Server data…
Example Return "YES" if the condition is TRUE, or "NO" if the condition is FALSE: SELECT IIF(500<1000, 'YES', 'NO'); Definition and Usage The IIF() function returns a value if a condition is TRUE, or…
Example Return the specified value IF the expression is NULL, otherwise return the expression: SELECT ISNULL(NULL, 'W3Schools.com'); Definition and Usage The ISNULL() function returns a specified val…
Example Tests whether the expression is numeric: SELECT ISNUMERIC(4567); Definition and Usage The ISNUMERIC() function tests whether an expression is numeric. This function returns 1 if the expressio…
Example Compare two expressions: SELECT NULLIF(25, 25); Definition and Usage The NULLIF() function returns NULL if two expressions are equal, otherwise it returns the first expression. Syntax NULLIF(…
Example Return the user name of the current user: SELECT SESSION_USER; Definition and Usage The SESSION_USER function returns the name of the current user in the SQL Server database. Syntax SESSION_U…
Example Return the session settings for a specified option: SELECT SESSIONPROPERTY('ANSI_NULLS'); Definition and Usage The SESSIONPROPERTY() function returns the session settings for a specified opti…
Example Return the login name information for the current user: SELECT SYSTEM_USER; Definition and Usage The SYSTEM_USER function returns the login name for the current user. Syntax SYSTEM_USER Techn…
Example Return the database user name (will return the current user since no id is specified): SELECT USER_NAME(); Definition and Usage The USER_NAME() function returns the database user name based o…
MS Access has many built-in functions. This reference contains the string, numeric, and date functions in MS Access. MS Access String Functions Function Description Asc Returns the ASCII value for th…
Example Return the ASCII value of the first character in "CustomerName": SELECT Asc(CustomerName) AS NumCodeOfFirstChar FROM Customers; Definition and Usage The Asc() function returns the ASCII value…
Example Return the character based on the number code 65: SELECT Chr(65) AS CodeToCharacter; Definition and Usage The Chr() function returns the character for the specified ASCII number code. Syntax…
Example Add 3 columns together into one "Address" column: SELECT Address & ", " & PostalCode & ", " & City AS Address FROM Customers; Definition and Usage The & operator allows you to add two or more…
Example CurDir() CurDir("D") Result: C:\Documents and Settings\user\My Documents D:\ Definition and Usage The CurDir() function returns the full path for a specified drive. Syntax CurDir( drive ) Par…
Example Format the "Price" column to Currency: SELECT Format(Price, "Currency") AS FormattedPrice FROM Products; Definition and Usage The Format() function formats a value with the specified format.…
Example Search for "t" in string "Customer", and return position: SELECT InStr("Customer", "t") AS MatchPosition; Definition and Usage The InStr() function gets the position of the first occurrence o…
Example Search for "t" in string "Customer" (start from the end of the string) and return position: SELECT InstrRev("Customer", "t") AS MatchPosition; Definition and Usage The InstrRev() function get…
MS Access LCase() Function
Example Extract 3 characters from a string (starting from left): SELECT Left("SQL Tutorial", 3) AS ExtractString; Definition and Usage The Left() function extracts a number of characters from a strin…
Example Return the length of a string: SELECT Len("SQL Tutorial") AS LengthOfString; Definition and Usage The Len() function returns the length of a string. Syntax Len( string/varname ) Parameter Val…
Example Remove leading spaces from a string: SELECT LTrim(" SQL Tutorial") AS LeftTrimmedString; Definition and Usage The LTrim() function removes leading spaces from a string. Note: See also the RTr…
Example Extract characters from a string, starting at position 3: SELECT Mid("SQL Tutorial", 3) AS ExtractString; Definition and Usage The Mid() function extracts some characters from a string (start…
Example Replace "i" with "a" in the string: SELECT Replace("My name is Willy Wonka", "i", "a") AS ReplaceString; Definition and Usage The Replace() function replaces a substring within a string, with…
Example Extract 4 characters from a string (starting from right): SELECT Right("SQL Tutorial is cool", 4) AS ExtractString; Definition and Usage The Right() function extracts a number of characters f…
Example Remove trailing spaces from a string: SELECT RTrim("SQL Tutorial ") AS RightTrimmedString; Definition and Usage The RTrim() function removes trailing spaces from a string. Note: See also the…
Example Return a string with 5 spaces: SELECT Space(5) AS SpaceString; Definition and Usage The Space() function returns a string of the specified number of space characters. Syntax Space( number ) P…
Example Split strings: SELECT Split("SQL Tutorial is fun!") AS SplitString; Result: {"SQL", "Tutorial", "is", "fun!"} SELECT Split ("red:green:yellow:blue", ":", 2) AS SplitString; Result: {"red", "g…
Example Return a number as a string: SELECT Str(42) AS ConvertToString; Definition and Usage The Str() function returns a number as a string. Syntax Str( number ) Parameter Values Parameter Descripti…
Example Compare two strings: SELECT StrComp("SQL Tutorial", "SQL Tutorial") AS CompString; Definition and Usage The StrComp() function compares two strings. The result is returned as an integer based…
Example Convert a string: SELECT StrConv("SQL Tutorial is cool", 1) AS ConvertedString; Definition and Usage The StrConv() function returns a converted string. Syntax StrConv( string1, conversion, LC…
Example Reverse a string: SELECT StrReverse("SQL Tutorial") AS StringReverse; Definition and Usage The StrReverse() function reverses a string and returns the result. Syntax StrReverse( string ) Para…
Example Remove leading and trailing spaces from a string: SELECT Trim(" SQL Tutorial ") AS TrimmedString; Definition and Usage The Trim() function removes both leading and trailing spaces from a stri…
MS Access UCase() Function
Example Return the absolute value of a number: SELECT Abs(-243.5) AS AbsNum; Definition and Usage The Abs() function returns the absolute (positive) value of a number. Syntax Abs( number ) Parameter…
Example Return the arc tangent of a number: SELECT Atn(5) AS AtnNum; Definition and Usage The Atn() function returns the arc tangent of a number. Syntax Atn( number ) Parameter Values Parameter Descr…
Example Return the average value for the "Price" column in the "Products" table: SELECT Avg(Price) AS AveragePrice FROM Products; Definition and Usage The Avg() function returns the average value of…
Example Return the cosine of an angle: SELECT Cos(5) AS CosNum; Definition and Usage The Cos() function returns the cosine of an angle. Syntax Cos( number ) Parameter Values Parameter Description num…
Example Return the number of products in the "Products" table: SELECT Count(ProductID) AS NumberOfProducts FROM Products; Definition and Usage The Count() function returns the number of records retur…
Example Return e raised to the 5th power: SELECT Exp(5) AS ExpNum; Definition and Usage The Exp() function returns e raised to the power of a specified number. The constant e (2.718281�), is the ba…
Example Return the integer part of a number: SELECT Fix(756.43) AS FixNum; Definition and Usage The Fix() function returns the integer part of a number. Syntax Fix( number ) Parameter Values Paramete…
Example Format a numeric expression to percent: SELECT Format(0.5, "Percent") AS FormattedNum; Definition and Usage The Format() function formats a numeric value with the specified format. Syntax For…
Example Return the integer part of a number: SELECT Int(756.43) AS IntNum; Definition and Usage The Int() function returns the integer part of a number. Note: If number is negative, this function ret…
Example Find the price of the most expensive product in the "Products" table: SELECT Max(Price) AS LargestPrice FROM Products; Definition and Usage The Max() function returns the maximum value in a s…
Example Find the price of the cheapest product in the "Products" table: SELECT Min(Price) AS SmallestPrice FROM Products; Definition and Usage The Min() function returns the minimum value in a set of…
Definition and Usage The Randomize() function initializes the random number generator (used by Rnd() ) with a seed. Syntax Randomize( seed ) Parameter Values Parameter Description seed Optional. A nu…
Example Return one random number between 1 and 10, and another random number between 100 and 499: SELECT Int((10 - 1 + 1) * Rnd + 1) AS RandNumBetween1and10, Int((499 - 100 + 1) * Rnd + 100) AS RandN…
Example Round the Price column to 1 decimal in the "Products" table: SELECT ProductName, Price, Round(Price, 1) AS RoundedPrice FROM Products; Definition and Usage The Round() function rounds a numbe…
Example Return the sign of a number: SELECT Sgn(243.5) AS SgnNum; Definition and Usage The Sgn() function returns the sign of a number. This function will return one of the following: If number > 0,…
Example Return the square root of a number: SELECT Sqr(64) AS SqrNum; Definition and Usage The Sqr() function returns the square root of a number. Syntax Sqr( number ) Parameter Values Parameter Desc…
Example Return the sum of the "Quantity" field in the "OrderDetails" table: SELECT Sum(Quantity) AS TotalItemsOrdered FROM OrderDetails; Definition and Usage The Sum() function calculates the sum of…
Example Read a string and return the numbers found in the string: SELECT Val(PostalCode) FROM Customers; Definition and Usage The Val() function reads a string and returns the numbers found in the st…
Example Return the current system date: SELECT CustomerName, Date() AS CurrentDate FROM Customers; Definition and Usage The Date() function returns the current system date. Syntax Date() Parameter Va…
Example Add two years to a specified date: SELECT DateAdd("yyyy", 2, #22/11/2017#); Definition and Usage The DateAdd() function adds a time/date interval to a date and then returns the date. Syntax D…
Example Return the difference between two dates, in years: SELECT DateDiff("yyyy", #13/01/1998#, #09/05/2017#); Definition and Usage The DateDiff() function returns the difference between two dates.…
Example Return the year part of the specified date: SELECT DatePart("yyyy", #09/05/2017#); Definition and Usage The DatePart() function returns a specified part of a date, as an integer. Syntax DateP…
Example Return a date from it's parts: SELECT DateSerial(2017, 4, 20); Definition and Usage The DateSerial() function returns a date from the specified parts (year, month, and day values). Syntax Dat…
Example Return a date based on the given string: SELECT DateValue("May 17, 2017"); Definition and Usage The DateValue() function returns a date based on a string. Note: If the given string does not i…
Example Return the day of the month for the date: SELECT Day(#05/17/2017#); Definition and Usage The Day() function returns the day of the month for a given date. This function returns an integer bet…
Example Format the "BirthDate" column to a long date: SELECT Format(BirthDate, "Long Date") AS FormattedBirthDate FROM Employees; Definition and Usage The Format() function formats a date value with…
Example Return the hour for a specified time: SELECT Hour(#14:48:23#); Definition and Usage The Hour() function returns the hour part of a time/datetime. This function returns an integer between 0 an…
Example Return the minute part of a specified time: SELECT Minute(#14:48:23#); Definition and Usage The Minute() function returns the minute part of a time/datetime. This function returns an integer…
Example Return the month part for a specified date: SELECT Month(#05/17/2017#); Definition and Usage The Month() function returns the month part for a given date. This function returns an integer bet…
Example Return the name of the month based on a number: SELECT MonthName(3); Definition and Usage The MonthName() function returns the name of the month based on a number. Syntax MonthName( number, a…
Example Return the computer system's current date and time: SELECT CustomerName, Now() AS CurrentDateTime FROM Customers; Definition and Usage The Now() function returns the current date and time bas…
Example Return the seconds part of a specified time: SELECT Second(#14:48:23#); Definition and Usage The Second() function returns the seconds part of a time/datetime. This function returns an intege…
Example Return the current system time: SELECT CustomerName, Time() AS CurrentTime FROM Customers; Definition and Usage The Time() function returns the current system time. Syntax Time() Parameter Va…
Example Return a time from it's parts: SELECT TimeSerial(15, 9, 20); Definition and Usage The TimeSerial() function returns a time from the specified parts (hour, minute, and second value). Syntax Ti…
Example Return a time based on the given string: SELECT TimeValue("17:15:38"); Definition and Usage The TimeValue() function returns a time based on a string. Syntax TimeValue( string ) Parameter Val…
Example Return the weekday number for a date: SELECT Weekday(#05/17/2017#); Definition and Usage The Weekday() function returns the weekday number for a given date. This function returns an integer b…
Example Return the weekday name based on a number: SELECT WeekdayName(3); Definition and Usage The WeekdayName() function returns the weekday name based on a number. Syntax WeekdayName( number, abbre…
Example Return the year part of a specified date: SELECT Year(#05/17/2017#); Definition and Usage The Year() function returns the year part of a given date. This function returns an integer between 1…
Example Return the name of the current database user: SELECT CurrentUser(); Definition and Usage The CurrentUser() function returns the name of the current database user. Syntax CurrentUser() Technic…
Example Return a string that contains the value of an operating system environment variable: SELECT Environ(1); Definition and Usage The Environ() function returns a string that contains the value of…
Example Check whether the expression can be converted to a date: SELECT IsDate(Now()); Definition and Usage The IsDate() function checks whether an expression can be converted to a date. This functio…
Example Check whether the expression is a Null value: SELECT IsNull(null); Definition and Usage The IsNull() function checks whether an expression contains Null (no data). This function returns a Boo…
Example Check whether the expression is a valid number: SELECT IsNumeric(1030); Definition and Usage The IsNumeric() function checks whether an expression is a valid number. This function returns a B…
SQL Statement Syntax AND / OR SELECT column_name(s) FROM table_name WHERE condition AND|OR condition ALTER TABLE ALTER TABLE table_name ADD column_name datatype or ALTER TABLE table_name DROP COLUMN…