Saturday, February 16, 2013

SQL Server RIGHT String Function

SQL Server RIGHT String Function

Right string function gets right 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  right('611-19-6789',4) 

O/P:
        6789

Ex 2: Get Middle 2 digits from SSN

select  reverse(right(reverse(right('611-19-6789',7)),2))

O/P:   19

 

Ex 3: Get Numbers from telephone number

select replace(right('708-234-5433',20),'-','')

O/P:
7082345433

Ex 4:  Get Right portion/Lastname of user 'Smith Mathew'

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



O/P:

Lastname
mathew



Tags:SQL Server RIGHT String Function,Get Right portion/Lastname 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