Get last n character from string in SQL Server | Code By Topic

Get last n character from string in SQL Server


Dear Code How to get last n character from string in SQL Server? For example how we get last 4 or 3 character from string.
Dear Topic Some one asked me couple of day before. I know its solve by substring and len function of SQL Server.But i write down a query more than one way . I assume that all of you aware about the inbuilt function of SQL Server .

Code Sample [SQL SERVER Query]

DECLARE @inputString varchar(100)
DECLARE @inputNumber int
SET @inputString='I am code bytopic'
SET @inputNumber=5

--first approch
SELECT RIGHT(@inputString,@inputNumber)

--second approch
SELECT REVERSE(SUBSTRING(REVERSE(@inputString),1,@inputNumber))

--third approch

SELECT SUBSTRING(@inputString,LEN(@inputString)-@inputNumber+1,LEN(@inputString))

Output looks like below.

get last m char in sql server

I solve this with three approach , Use reverse method of SQL Server which reverse the number .I use len function of SQL Server which return the length of given string in SQL Server.For example
SELECT LEN('ROHIT') then it will return 5.

read more..