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

SQL Server Functions

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

SQL Server Functions

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

SQL Server Functions

1. Cast Function: CAST()

The cast function converts a data type into another data type.

Cast Function Example:

SELECT Cast(10 AS VARCHAR);

Output:

'10'

2. Coalesce Function: COALESCE()

The coalesce function returns the first non-null value from a list of
expressions.

Coalesce Function Example:

SELECT COALESCE(NULL,'value2','value3');

Output:

'value2'

3. Is Null Function: ISNULL()

The is null function checks if an expression is null; if it is, it returns a


replacement value.

Is Null Function Example:

SELECT Isnull('value','null');

Output:

'value'

4. If Null Function: IFNULL()


The if null function is similar to the is null function, but the arguments are
reversed.

If Null Function Example:

SELECT Ifnull('value','null');

Output:

'value'

5. NVL Function: NVL()

The NVL function replaces null values with a specified default value.

NVL Function Example:

SELECT Nvl(NULL,'default value');

Output:

'default value'

6. Case Expression: CASE

The case expression performs conditional operations based on a specified


expression.

Case Expression Example:

SELECTCASE
WHEN1=1THEN'true'
ELSE'false'
END;

Output:

'true'

7. Convert Function: CONVERT()

The convert function converts a value into a specified data type.


Convert Function Example:

SELECT CONVERT(VARCHAR,10);

Output:

'10'

8. Split Function: STRING_SPLIT()

The split function splits a string into a table of substrings.

Split Function Example:

SELECT *
FROM String_split('apple,banana,cherry',',');

Output:

apple banana cherry

9. Random Function: RAND()

The random function generates a random number between 0 and 1.

Random Function Example:

SELECT Rand();

Output:

0.7750247322012854

10. If Function: IF()

The if function performs a conditional operation based on a specified


expression.

If Function Example:

SELECT IF(1=1,'true','false');

Output:
'True'

11. Mid Function: MID()

The mid function extracts a substring from a string, starting at a specified


position and with a specified length.

Mid Function Example:

SELECTMid('apple',2,3);

Output:

'ppl'

SQL Date Functions


12. Date Add Function: DATEADD()

The date add function adds a specified number of intervals (such as days,
months, or years) to a date.

Date Add Function Example:

SELECT Dateadd(day,7,'2023-05-05');

Output:

'2023-05-12'

13. Date Format Function: DATE_FORMAT()

The date format function reformats a date value into a string with a specified
format.

Date Format Example:

SELECT Date_format('2023-05-05','%m/%d/%Y');

Output:

'05/05/2023'
14. Date Part Function: DATEPART()

The date part function extracts a specified part of a date, such as year, month,
or day.

Date Part Example:

SELECT Datepart(year,'2023-05-05');

Output:

2023

15. Day of the Week Function: DAYOFWEEK()

The day of the week function returns the day of the week for a given date.

Day of the Week Example:

SELECT Dayofweek('2023-05-05');

Output:

16. Week Function: WEEK()

The week function returns the week number for a given date.

Week Function Example:

SELECT Week('2023-05-05');

Output:

18

17. Weekday Function: WEEKDAY()

The weekday function returns the weekday index for a given date.

Weekday Function Example:

SELECT Weekday('2023-05-05');
Output:

18. Year Function: YEAR()

The year function extracts the year from a date value.

Year Function Example:

SELECT Year('2023-05-05');

Output:

2023

SQL String Functions


19. Length Function: LEN()

The length function is used to return the length of a string.

Length Function Example:

SELECT Len('banana');

Output:

20. Left Function: LEFT()

The left function extracts a specified number of characters from the beginning
of a string.

Left Function Example:

SELECT LEFT('banana',3);

Output:

'ban'
21. Right Function: RIGHT()

The right function extracts a specified number of characters from the end of a
string.

Right Function Example:

SELECTRIGHT('banana',3);

Output:

'ana'

22. Trim Function: TRIM()

The trim function removes leading and trailing spaces from a string.

Trim Function Example:

SELECTTrim(' banana ');

Output:

'banana'

23. Ascii Function: ASCII()

The ascii function returns the ASCII code value for a specified character.

Ascii Function Example:

SELECTAscii('A');

Output:

65

24. Concatenate Function: CONCAT()

The concatenate function links two or more strings together.

Concat Function Example:

SELECT Concat('apple','banana','cherry');
Output:

'applebananacherry'

25. Format Function: FORMAT()

The format function reformats a value into a specified string format.

Format Function Example:

SELECT Format(12345.6789,'#,##0.00');

Output:

12,345.68

26. Replace Function: REPLACE()

The replace function replaces a specified string with another string in a given
expression.

Replace Function Example:

SELECT Replace('banana','a','e');

Output:

Benene

27. Substring Function: SUBSTRING()

The SUBSTRING function extracts a substring from a given string.

Substring Function Example:

SELECTSubstring('banana',2,3);

Output:

'nan'

28. In String Function: INSTR()

The in string function finds the position of a specified substring within a string.
In String Function Example:

SELECTInstr('banana','a');

Output:

29. Stuff Function: STUFF()

The stuff function replaces a substring within a string with another substring.

Stuff Function Example:

SELECTStuff('banana',2,3,'oro');

Output:

'borona'

You might also like