ASCII
Sample :-
DECLARE @CharValue char(5)
SET @CharValue = 'C'
SELECT @CharValue AS 'CHAR', ASCII(@CharValue) AS 'ASCII'
/* Output :-
CHAR ASCII
C 67
*/
--In below code also ASCII function will only consider the leftmost character.
DECLARE @CharValue char(5)
SET @CharValue = 'CODE'
SELECT @CharValue AS 'CHAR', ASCII(@CharValue) AS 'ASCII'
/* Output
CHAR ASCII
CODE 67 */
CHAR
Sample :-
SELECT CHAR(90) AS 'CHAR'
/* Output
CHAR
Z
*/
SELECT CHAR(97) AS 'CHAR'
/* Output
CHAR
a
*/
SELECT CHAR(256) AS 'CHAR'
/* Output
CHAR
NULL
*/
NCHAR
Sample :-
DECLARE @arabicletter nvarchar(10) = N'ك';
SELECT NCHAR(UNICODE(@arabicletter)) AS 'NCHAR', CHAR(UNICODE(@arabicletter)) AS 'CHAR', UNICODE(@arabicletter) AS 'UNICODE'
/* Output
NCHAR CHAR UNICODE
ك NULL 1603
*/
CHARINDEX
Sample :-
DECLARE @StringValue varchar(100);
SELECT @StringValue = 'String Data Types and Functions';
SELECT CHARINDEX('Types', @StringValue) AS 'CHARINDEX'; --Searching for a existent expression
SELECT CHARINDEX('Data', @StringValue, 6) AS 'CHARINDEX'; --Searching from a specific position
SELECT CHARINDEX('Shemeer', @StringValue) AS 'CHARINDEX'; --Searching for a nonexistent expression
/*
The output shows respetively
CHARINDEX
13
8
0
*/
CONCAT
CONCAT ( string_value1, string_value2 [, string_valueN ] )
Where string_value is a string value to concatenate to the other values.
Sample :-
DECLARE @StringValue varchar(100);
SELECT CONCAT('String', ' Data', ' Types' , ' and Functions') AS 'CAPTION';
/*
Output
CAPTION
String Data Types and Functions
*/
DIFFERENCE
Sample :-
SELECT DIFFERENCE('ABC','ABCD'); -- Returns 3, means more similarity
SELECT DIFFERENCE('ABC','ABC'); -- Returns 4, means strong similarity or same
SELECT DIFFERENCE('ABC','123'); -- Returns 0, means no similarity
FORMAT
Sample :-
DECLARE @d DATETIME = '10/01/2011';
SELECT FORMAT ( @d, 'd', 'zh-cn' ) AS 'Simplified Chinese (PRC) Result' -- Return 2011/10/1
SELECT FORMAT(101.2, 'C', 'en-us') AS 'Currency Format' -- Returns $101.20
LEFT
Sample :-
SELECT LEFT('SHEMEER',3) -- Returns SHE
SELECT LEFT('SHEMEER',100) -- Returns SHEMEER
--SELECT LEFT('SHEMEER',-1) -- Error 'Invalid length parameter passed to the left function.'
RIGHT
Sample :-
SELECT RIGHT('SHEMEER',3) -- Returns EER
SELECT RIGHT('SHEMEER',100) -- Returns SHEMEER
--SELECT RIGHT('SHEMEER',-1) -- Error 'Invalid length parameter passed to the right function..
LEN
Sample :-
SELECT LEN('SHEMEER') -- Returns 7
LOWER
Sample :-
SELECT LOWER('SHEMEER') -- Returns shemeer
UPPER
Returns a character expression with lowercase character data converted to uppercase. Click here to read more from msdn.
Sample :-
SELECT UPPER('shemeer') -- Returns SHEMEER
LTRIM
Sample :-
The following example uses LTRIM to remove leading spaces from a character string.
SELECT LTRIM(' CODEPROJECT ') -- Returns 'CODEPROJECT '
RTRIM
Sample :-
The following example uses RTRIM to remove trailing spaces from a character string.
SELECT RTRIM(' CODEPROJECT ') -- Returns ' CODEPROJECT'
PATINDEX
Sample :-
SELECT PATINDEX('%those%', 'CODEPROJECT for those who code'); -- Returns 17
QUOTENAME
Sample :-
SELECT QUOTENAME('DESC') -- Returns '[DESC]'
SELECT QUOTENAME('sql[]string') -- Returns '[sql[]]string]' , Notice that the right bracket in the string sql[]string is
doubled to indicate an escape character.
REPLACE
Sample :-
SELECT REPLACE('String Functions','String','Time'); -- Returns 'Time Functions'
REPLICATE
Sample :-
SELECT REPLICATE('0', 3) -- Returns 000
SELECT REPLICATE('0', -1) -- Returns NULL
REVERSE
REVERSE ( string_expression )
string_expression is an expression of a string or binary data type.
Sample :-
SELECT REVERSE('CODEPROJECT') -- Returns 'TCEJORPEDOC'
SOUNDEX
SOUNDEX ( character_expression )
where character_expression is an alphanumeric expression of character data .
Sample :-
SELECT SOUNDEX ('shy'), SOUNDEX ('shi'); -- Returns 'S000
SPACE
SPACE ( integer_expression )
Where integer_expression is a positive integer that indicates the number of spaces. If integer_expression is negative, a null
string is returned.
Sample :-
SELECT CONCAT('CODE',SPACE(2),'PROJCET') -- Returns 'CODE PROJCET'
To include spaces in Unicode data, or to return more than 8000 character spaces, use REPLICATE instead of SPACE.
STR
Sample :-
SELECT 'SL NO' + 1 -- Returns Error
--'Conversion failed when converting the varchar value 'SL NO' to data type int.'
SELECT 'SL NO' + STR(1,2) -- Returns 'SL NO 1'
SELECT STR (FLOOR (123.45), 8, 3); -- Returns ' 123.000'
SELECT STR(123.45, 2, 2); -- Returns '**'
--When the expression exceeds the specified length, the string returns ** for the specified length.
STUFF
The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at
the start position and then inserts the second string into the first string at the start position. Click here to read more
Sample :-
SELECT STUFF('CODEFOR THOSE WHO CODE', 5, 0, ' PROJECT ');
-- Returns 'CODE PROJECT FOR THOSE WHO CODE'
--Where 5 is the position where we want to insert ' PROJECT ',
--and 0 represnts the number of characters to be replaced from the position 5
SUBSTRING
Sample :-
SELECT SUBSTRING('CODE PROJECT FOR THOSE WHO CODE', 6, 7);
-- Returns 'PROJECT'
UNICODE
Sample :-
DECLARE @arabicletter nvarchar(10) = N'ك';
SELECT NCHAR(UNICODE(@arabicletter)) AS 'NCHAR', CHAR(UNICODE(@arabicletter)) AS 'CHAR', UNICODE(@arabicletter) AS 'UNICODE'
/* Output
NCHAR CHAR UNICODE
ك NULL 1603
*/