SQL Server Functions
SQL Server Functions
The cast function converts a data type into another data type.
Output:
'10'
The coalesce function returns the first non-null value from a list of
expressions.
SELECT COALESCE(NULL,'value2','value3');
Output:
'value2'
SELECT Isnull('value','null');
Output:
'value'
SELECT Ifnull('value','null');
Output:
'value'
The NVL function replaces null values with a specified default value.
Output:
'default value'
SELECTCASE
WHEN1=1THEN'true'
ELSE'false'
END;
Output:
'true'
SELECT CONVERT(VARCHAR,10);
Output:
'10'
SELECT *
FROM String_split('apple,banana,cherry',',');
Output:
SELECT Rand();
Output:
0.7750247322012854
If Function Example:
SELECT IF(1=1,'true','false');
Output:
'True'
SELECTMid('apple',2,3);
Output:
'ppl'
The date add function adds a specified number of intervals (such as days,
months, or years) to a date.
SELECT Dateadd(day,7,'2023-05-05');
Output:
'2023-05-12'
The date format function reformats a date value into a string with a specified
format.
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.
SELECT Datepart(year,'2023-05-05');
Output:
2023
The day of the week function returns the day of the week for a given date.
SELECT Dayofweek('2023-05-05');
Output:
The week function returns the week number for a given date.
SELECT Week('2023-05-05');
Output:
18
The weekday function returns the weekday index for a given date.
SELECT Weekday('2023-05-05');
Output:
SELECT Year('2023-05-05');
Output:
2023
SELECT Len('banana');
Output:
The left function extracts a specified number of characters from the beginning
of a string.
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.
SELECTRIGHT('banana',3);
Output:
'ana'
The trim function removes leading and trailing spaces from a string.
Output:
'banana'
The ascii function returns the ASCII code value for a specified character.
SELECTAscii('A');
Output:
65
SELECT Concat('apple','banana','cherry');
Output:
'applebananacherry'
SELECT Format(12345.6789,'#,##0.00');
Output:
12,345.68
The replace function replaces a specified string with another string in a given
expression.
SELECT Replace('banana','a','e');
Output:
Benene
SELECTSubstring('banana',2,3);
Output:
'nan'
The in string function finds the position of a specified substring within a string.
In String Function Example:
SELECTInstr('banana','a');
Output:
The stuff function replaces a substring within a string with another substring.
SELECTStuff('banana',2,3,'oro');
Output:
'borona'