Thursday, 29 December 2016

SQL SERVER -- String Functions-- Part-20

SQL SERVER -- String Functions-- Part-20

                   By Using String Functions we can perform following string operations.

LEN Function:-    
              Using LEN Function we can get the total number of characters in the given string.

DECLARE @STR VARCHAR(100)='Bangalore,karnataka,India'
SELECT @STR AS ActualString
SELECT LEN(@STR) as TotalLength


REVERSE Function:-   
                 Used to print the given string in the reverse order/manner.

DECLARE @STR VARCHAR(100)='Bangalore,karnataka,India'
SELECT @STR AS ActualString
SELECT REVERSE(@STR)



LTRIM Function:-    
                               Used to eliminate the unwanted spaces from the left side of the given string.

DECLARE @STR VARCHAR(100)='  Bangalore,karnataka,India'
SELECT @STR AS ActualString
SELECT LTRIM(@STR)


RTRIM Function:-    
                  Used to eliminate the unwanted spaces from the right side of the given string. 

DECLARE @STR VARCHAR(100)='Bangalore,karnataka,India'
SELECT @STR AS ActualString
SELECT RTRIM(@STR)


CHARINDEX Function:- 
                                       Used to get the position of the character from the given string.

DECLARE @STR VARCHAR(100)='Bangalore,karnataka,India'
SELECT @STR AS ActualString
SELECT CHARINDEX('K',@STR) AS POSITION

   

SUBSTRING Function:-    
                                      Used to extract the part of the given string based on the starting and ending position.

DECLARE @STR VARCHAR(100)='Bangalore,karnataka,India'
SELECT @STR AS ActualString
SELECT SUBSTRING(@STR,1,CHARINDEX(',',@STR)-1) AS CityName



ISNULL Function:-    
                               Used to replace the NULL values with user defined values.

SELECT FirstName
      ,MiddleName
         ,LastName
         ,FirstName+ MiddleName+ LastName AS FullName
         ,FirstName+ ISNULL(MiddleName,'') + LastName AS FullName_with_ISNULL

FROM DimCustomer


REPLACE Function:
                                 Used to REPLACE the part of the string using user defined values.

DECLARE @STR VARCHAR(100)='Bangalore,karnataka,India'
SELECT @STR AS ActualString
SELECT REPLACE(@STR,',','/')


COALESCE Function:
                                 Used to get first not NULL values from the given expressions.

SELECT FirstName
      ,MiddleName
         ,LastName
    ,COALESCE(MiddleName,FirstName,LastName)

FROM DimCustomer






Thanks for Looking into this.....


No comments:

Post a Comment