Sunday, February 17, 2013

SQL Server LEFT String Function


SQL Server LEFT String Function

Left string function gets left portion of the string  of specified length, If length is more than string length entire string will be displayed,
Ex:  get  last 4 digits of SSN#
select  left('611-19-6789',3) 

O/P:
        611
Ex 2: Get Middle 2 digits from SSN
select  reverse(left(reverse(left('611-19-6789',6)),2))
O/P:   19
 

Ex 3: Get Numbers from telephone number
select replace(left('708-234-5433',20),'-','')
O/P:
7082345433

Ex 4:  Get First Name of user 'Smith Mathew'

declare @str nvarchar(100),@charidx int,@strLen int;
set @str='Smith Mathew'
set @strLen = len(@str)
set @charidx = CHARINDEX (N' ',@str)
select '--'+left(@str,@strLen-@charidx)+'--'


O/P:

Firstname
--Smith --


Tags:SQL Server LEFT String Function,Get firstname of user in SQL Server,
Get Numbers from telephone number in SQL Server,
get  last 4 digits of SSN# in sql server, Get Numbers from telephone number

No comments:

Post a Comment