SQL String Functions

  1. -- SQL String Functions (Samples)  
  2.   
  3. --1-) LEN: It gives charater length  
  4.   
  5. Select EmployeeID, LEN(LastName) as [LastNameCharacterLen], LEN(FirstName) as [FirstNameCharacterLen] from Employees  
  6.   
  7. go  
  8.   
  9.    --2-) Concat : It merges two or more strings  
  10.   
  11.    Select CONCAT(FirstName , ' ' ,LastName) as [Full Namefrom Employees   
  12.   
  13.    Select CONCAT(FirstName , SPACE(1) , LastName) as [Full Namefrom Employees -- Space(1) it's a space  
  14.   
  15. go  
  16.   
  17.    --3-) CharIndex Find out character indexs(  
  18.    -- Where is the b letter? etc...  
  19.    Select CHARINDEX('ber',FirstName) [{CharIndex]from Employees  
  20.   
  21.    Select CHARINDEX('senta',Title) as [CharIndex] from Employees  
  22.   
  23. go  
  24.   
  25.    --4-) Lower : It gives a lower character for all strings  
  26.   
  27.    Select FirstName,LastName,Title,LOWER(TitleOfCourtesy) AS [TitleOfCourtesy] from Employees  
  28.   
  29. go  
  30.   
  31.    --5-) Upper : It gives a upper character for all strings  
  32.   
  33.    Select FirstName,LastName,UPPER(TitleOfCourtesy) as[TitleOfCourtesy], Title from Employees  
  34.   
  35. go  
  36.   
  37.       --6-) LTrim ve RTrim : It's remove space if you use Ltrim = clear left spaces , Rtrim clear right spaces  
  38.   
  39.    Declare @character varchar(30)  
  40.    set @character='    from which data from SQL Server                 '  
  41.    Select @character as[ Karakter]  
  42.    Select LTRIM(@characteras[Left Trim]  
  43.    Select RTRIM(@characteras[Right Trim]  
  44.   
  45. go  
  46.   
  47.    --7-)Substring : This method extracts strings and create a new string value  
  48.   
  49.    Declare @city  varchar(20)  
  50.    set @city='Istanbul,Turkey,Canada'  
  51.    Select SUBSTRING(@city,0,5)  
  52.    Select SUBSTRING(@city,0,15)