Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views4 pages

String Functions String Functions Are Used For Manipulating and Extracting Data From Strings

Uploaded by

socialhari6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

String Functions String Functions Are Used For Manipulating and Extracting Data From Strings

Uploaded by

socialhari6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

String functions

String functions are used for manipulating and extracting data from strings.

Fun Name Description Syntax Example Output

Returns the ASCII value for the first character


ASCII() SELECT ASCII('A') FROM DUAL; 65
in a string.

Returns a concatenated string from multiple SELECT CONCAT('Hello', ' ', 'World') 'Hello
CONCAT()
arguments. FROM DUAL; World'

LOWER()/
Converts a string to lowercase. SELECT LOWER('HELLO') FROM DUAL; 'hello'
LCASE()

UPPER()/
Converts a string to uppercase. SELECT UPPER('hello') FROM DUAL; 'HELLO'
UCASE()

LENGTH() Returns the length of a string in bytes. SELECT LENGTH('Hello') FROM DUAL; 5

Extracts a substring from a string, given a SELECT SUBSTRING('Example String', 1,


SUBSTRING()/ 'Example'
starting position and length. 7) FROM DUAL;
SUBSTR()

'String
Removes leading and/or trailing spaces (or SELECT TRIM(' String with spaces ')
TRIM() with
specified characters) from a string. FROM DUAL;
spaces'

Extracts a specified number of characters SELECT LEFT('MySQL Function', 5)


LEFT() 'MySQL'
from the left side of a string. FROM DUAL;

Extracts a specified number of characters SELECT RIGHT('MySQL Function', 8)


RIGHT() 'Function'
from the right side of a string. FROM DUAL;

Replaces all occurrences of a specified SELECT REPLACE('Hello World', 'World', 'Hello


REPLACE()
substring within a string. 'MySQL') FROM DUAL; MySQL'

Returns the position of the first occurrence of SELECT INSTR('MySQL Functions',


INSTR() 7
a substring within a string. 'Functions') FROM DUAL;

Left-pads a string to a specified length with


LPAD() SELECT LPAD('123', 5, '0') FROM DUAL; '00123'
another string.

Right-pads a string to a specified length with


RPAD() SELECT RPAD('123', 5, '0') FROM DUAL; '12300'
another string.
2. Numeric functions

Numeric functions are used for calculations and manipulations on numeric values.

Function
Description Syntax Example Output
Name

Returns the absolute (non-negative)


ABS() SELECT ABS(-10) FROM DUAL; 10
value of a number.

CEIL()/ Rounds a number up to the nearest


SELECT CEIL(12.34) FROM DUAL; 13
CEILING() whole integer.

Rounds a number down to the


FLOOR() SELECT FLOOR(12.98) FROM DUAL; 12
nearest whole integer.

Rounds a number to a specified


ROUND() SELECT ROUND(15.6789, 2) FROM DUAL; 15.68
number of decimal places.

POW()/ Returns the result of a number


SELECT POWER(2, 3) FROM DUAL; 8
POWER() raised to a specified power.

Returns the square root of a


SQRT() SELECT SQRT(16) FROM DUAL; 4
number.

Truncates (removes the decimal


portion) a number to a specified
TRUNCATE() SELECT TRUNCATE(123.456, 2) FROM DUAL; 123.45
number of decimal places without
rounding.

Returns the remainder of a division


MOD() SELECT MOD(13, 2) FROM DUAL; 1
operation.

Returns a random floating-point (random


RAND() value between 0 (inclusive) and 1 SELECT RAND() FROM DUAL; value, e.g.,
(exclusive). 0.654321)

Formats a number to a format like


FORMAT() "#,###,###.##", rounded to a SELECT FORMAT(1234567.89, 2) FROM DUAL; '1,234,567.89'
specified number of decimal places.

3. Date and time functions


These functions manipulate and extract information from date and time values.

Function
Description Syntax Example Example
Name

CURDATE()
(current date, e.g.,
CURRENT_D Returns the current date. SELECT CURDATE() FROM DUAL;
'2025-08-18')
ATE()

NOW()/ (current date and


Returns the current date
CURRENT_TI SELECT NOW() FROM DUAL; time, e.g., '2025-08-
and time.
MESTAMP() 18 13:05:00')

Extracts the date part


SELECT DATE('2025-08-18 13:05:00')
DATE() from a date or datetime '2025-08-18'
FROM DUAL;
expression.

Returns the month


SELECT MONTH('2025-08-18') FROM
MONTH() number (1-12) from a 8
DUAL;
date.

Returns the year from a


YEAR() SELECT YEAR('2025-08-18') FROM DUAL; 2025
date.

Returns the day of the


DAY() SELECT DAY('2025-08-18') FROM DUAL; 18
month (1-31) from a date.

Adds a time/date interval SELECT DATE_ADD('2025-08-18',


DATE_ADD() '2025-08-25'
to a date value. INTERVAL 7 DAY) FROM DUAL;

Subtracts a time/date SELECT DATE_SUB('2025-08-18',


DATE_SUB() '2025-07-18'
interval from a date value. INTERVAL 1 MONTH) FROM DUAL;

Formats a date value


DATE_FORM SELECT DATE_FORMAT('2025-08-18',
according to a specified '2025-08-18'
AT() '%Y-%m-%d') FROM DUAL;
format string.

Returns the number of


SELECT DATEDIFF('2025-08-25', '2025-
DATEDIFF() days between two date 7
08-18') FROM DUAL;
values.

Converts a string to a date


STR_TO_DAT SELECT STR_TO_DATE('January,25,2021',
value, given a format '2021-01-25'
E() '%M,%e,%Y') FROM DUAL;
string.
4. Control flow functions

Control flow functions introduce conditional logic into your queries.

Function
Description Syntax Example Example
Name

Allows you to add if-else logic to a query, evaluating SELECT CASE WHEN 1 > 0
CASE conditions and returning different values based on THEN 'True' ELSE 'False' END 'True'
those conditions. FROM DUAL;

Returns a value based on a specified condition; a SELECT IF(1 = 1, 'Yes', 'No')


IF() 'Yes'
shorthand for a simple CASE statement. FROM DUAL;

Returns the first argument if it is not NULL, SELECT IFNULL(NULL, 'Default


IFNULL()
otherwise returns the second argument. 'Default Value') FROM DUAL; Value'

Returns NULL if the two arguments are equal, SELECT NULLIF(10, 10) FROM
NULLIF() NULL
otherwise returns the first argument. DUAL;

5. Aggregate functions

Aggregate functions operate on a set of rows and return a single summary value.

Function Name Description Syntax Example Example

Returns the number of rows in a


SELECT COUNT(*) FROM (total number
COUNT() group, including rows with NULL
Customers; of customers)
values.

COUNT(DISTINCT Returns the count of a number of SELECT COUNT(DISTINCT (no of unique


column_name) different values in a specified column. country) FROM Customers; countries)

Calculates the total sum of values in a SELECT SUM(order_total) (total sum of


SUM()
numeric column. FROM Orders; all orders)

Calculates the average value of a SELECT AVG(price) FROM (average price


AVG()
numeric column. Products; of products)

Retrieves the minimum value from a SELECT MIN(birth_date) FROM (earliest birth
MIN()
column. Employees; date)

MAX() Retrieves the maximum value from a SELECT MAX(salary) FROM (highest
column. Employees; salary)

You might also like