Sunday, April 30, 2017

RIGHT() in SQL Server


SQL RIGHT() function is used to extract a substring from a given string, starting from the right most position. RIGHT() takes two parameters i.e. string and no of characters as input and returns the substring based on the parameters. RIGHT() does not works on NULL Values, NULL values are not considered. When negative length is specified as parameter, RIGHT() returns an error.


Syntax:


        SELECT RIGHT(expression, number_of_characters)


expression is the text whose substring from right is to be extracted.

number_of_characters is a positive string which specifies how many characters of given string will be returned.


Example 1:
RIGHT() on numeric column.

SELECT * FROM TempEmployeePayHistory


SELECT Rate, RIGHT(Rate,3) FROM TempEmployeePayHistory



Example 2:
RIGHT() on column with NULL values.

          SELECT * FROM TempEmployeePayHistory


SELECT Rate, RIGHT(Rate,3) FROM TempEmployeePayHistory


Example 3:
RIGHT() on text column.

          SELECT * FROM DatabaseLog

  
          SELECT DISTINCT [Event], RIGHT([Event], 10) FROM DatabaseLog


Example 4:
RIGHT() with negative value as no of characters.

          SELECT DISTINCT [Event], RIGHT([Event], -10) FROM DatabaseLog


Example 5:
RIGHT() with no of characters greater than expression length.

          SELECT DISTINCT [Event], RIGHT([Event], 20) FROM DatabaseLog



Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.

0 comments: