0% found this document useful (0 votes)
11 views

Functions

The document describes various mathematical, numerical, string, logical and date/time functions used in SQL. Some key functions include ABS() for absolute value, ROUND() for rounding numbers, CONCAT() for concatenating strings, AND/OR/NOT for logical operations, and NOW() to return the current date and time.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Functions

The document describes various mathematical, numerical, string, logical and date/time functions used in SQL. Some key functions include ABS() for absolute value, ROUND() for rounding numbers, CONCAT() for concatenating strings, AND/OR/NOT for logical operations, and NOW() to return the current date and time.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Mathematical Functions:

1. ABS () - Absolute Value:


 Meaning: Returns the absolute value of a
number.
 Syntax: ABS (number)

 Example: ABS (-10)

 Output: 10

2. ROUND () - Round to Nearest Integer:


 Meaning: Rounds a number to the nearest
integer or to the specified number of decimal
places.
 Syntax: ROUND (number, decimal places)
 Example: ROUND (15.678, 2)

 Output: 15.68

3. POWER() - Exponentiation:
 Meaning: Raises a number to the power of
another.
 Syntax: POWER(base, exponent)

 Example: POWER(2, 3)
 Output: 8

4. EXP() - Exponential:
 Meaning: Returns e raised to the power of a
specified number.
 Syntax: EXP(number)

 Example: EXP(2)
 Output: 7.3890560989306495

5. LOG() - Natural Logarithm:


 Meaning: Returns the natural logarithm of a
number.
 Syntax: LOG(number)
 Example: LOG(10)

 Output: 2.302585092994046

6. PI() - Pi Constant:
 Meaning: Returns the mathematical constant pi.
 Syntax: PI()
 Example: PI()
 Output: 3.141592653589793

Numerical Functions:

1. RAND() - Random Number:


 Meaning: Returns a random number between 0
and 1.
 Syntax: RAND()

 Example: RAND()
 Output: 0.837419287

2. SQRT() - Square Root:


 Meaning: Returns the square root of a number.

 Syntax: SQRT(number)

 Example: SQRT(25)
 Output: 5

3. AVG() - Average:
 Meaning: Returns the average value of a numeric
column.
 Syntax: AVG(column)
 Example: AVG(salary)

 Output: 55000

4. COUNT() - Count Rows:


 Meaning: Returns the number of rows in a result
set.
 Syntax: COUNT(column)

 Example: COUNT(employee_id)

 Output: 30

5. MAX() - Maximum Value:


 Meaning: Returns the maximum value in a set of

values.
 Syntax: MAX(column)
 Example: MAX(sales)
 Output: 100000

6. MIN() - Minimum Value:


 Meaning: Returns the minimum value in a set of
values.
 Syntax: MIN(column)

 Example: MIN(salary)

 Output: 30000

7. SQUARE() - Square of a Number:


 Meaning: Returns the square of a number.

 Syntax: SQUARE(number)

 Example: SQUARE(4)
 Output: 16

8. SUM() - Sum of Values:


 Meaning: Returns the sum of all values in a

numeric column.
 Syntax: SUM(column)

 Example: SUM(sales)

 Output: 500000

String Functions:

1. CONCAT() or CONCATENATE() - Concatenate Strings:


 Meaning: Concatenates two or more strings.
 Syntax: CONCAT(string1, string2, ...)

 Example: CONCAT('Hello', ' ', 'World')

 Output: 'Hello World'

2. LENGTH() or LEN() - String Length:


 Meaning: Returns the length of a string.
 Syntax: LENGTH(string) or LEN(string)

 Example: LENGTH('Hello')

 Output: 5

3. UPPER() - Uppercase Conversion:


 Meaning: Converts a string to uppercase.
 Syntax: UPPER(string)

 Example: UPPER('hello')

 Output: 'HELLO'

4. LOWER() - Lowercase Conversion:


 Meaning: Converts a string to lowercase.

 Syntax: LOWER(string)

 Example: LOWER('WORLD')

 Output: 'world'

5. TRIM() - Remove Spaces:


 Meaning: Removes leading and trailing spaces
from a string.
 Syntax: TRIM(string)

 Example: TRIM(' Hello ')


 Output: 'Hello'

6. ASCII() - ASCII Value:


 Meaning: Returns the ASCII value of the leftmost
character of a string.
 Syntax: ASCII(string)

 Example: ASCII('A')
 Output: 65

7. CHAR() - ASCII to Character:


 Meaning: Converts an ASCII value to a character.

 Syntax: CHAR(ascii_value)

 Example: CHAR(65)
 Output: 'A'

8. CHARINDEX() - Substring Position:


 Meaning: Returns the position of a substring
within a string.
 Syntax: CHARINDEX(substring, string)
 Example: CHARINDEX('lo', 'Hello')

 Output: 3

9. REPLACE() - Replace Substring:


 Meaning: Replaces occurrences of a specified
substring with another substring.
 Syntax: REPLACE(string, old_substring,
new_substring)
 Example: REPLACE('Hello World', 'World',
'Universe')
 Output: 'Hello Universe'
10. LTRIM() - Left Trim:
 Meaning: Removes leading spaces from a string.
 Syntax: LTRIM(string)
 Example: LTRIM(' Hello')
 Output: 'Hello'
11. RTRIM() - Right Trim:
 Meaning: Removes trailing spaces from a string.
 Syntax: RTRIM(string)
 Example: RTRIM('Hello ')
 Output: 'Hello'

Logical Functions:

1. AND - Logical AND:


 Meaning: Returns true if all conditions are true.
 Syntax: condition1 AND condition2

 Example: salary > 50000 AND department = 'IT'

 Output: True or False

2. OR - Logical OR:
 Meaning: Returns true if at least one condition is
true.
 Syntax: condition1 OR condition2

 Example: age > 21 OR income > 50000

 Output: True or False

3. NOT - Logical NOT:


 Meaning: Returns true if the condition is false
and vice versa.
 Syntax: NOT condition
 Example: NOT status = 'Inactive'
 Output: True or False

Date & Time Functions:

1. NOW() - Current Date and Time:


 Meaning: Returns the current date and time.

 Syntax: NOW()
 Example: NOW()

 Output: '2024-01-16 14:30:00'

2. CURDATE() - Current Date:


 Meaning: Returns the current date.

 Syntax: CURDATE()
 Example: CURDATE()

 Output: '2024-01-16'

3. CURTIME() - Current Time:


 Meaning: Returns the current time.

 Syntax: CURTIME()

 Example: CURTIME()
 Output: '14:30:00'

4. YEAR() - Extract Year:


 Meaning: Extracts the year from a date.

 Syntax: YEAR(date)
 Example: YEAR('2024-01-16')

 Output: 2024

5. MONTH() - Extract Month:


 Meaning: Extracts the month from a date.

 Syntax: MONTH(date)

 Example: MONTH('2024-01-16')
 Output: 1

6. DAY() - Extract Day:


 Meaning: Extracts the day from a date.
 Syntax: DAY(date)
 Example: DAY('2024-01-16')
 Output: 16

You might also like