loading..
Русский    English
16:55

REVERSE function

This function reverses a string, just like reading it from the right to left. That is the result of the query
SELECT REVERSE('abcdef')

Console
Execute
  1. SELECT REVERSE('abcdef')
will be 'fedcba'. In case of the absence of the RIGHT function in the language the query

Console
Execute
  1. SELECT RIGHT('abcdef', 3)
can be equally replaced with the query

Console
Execute
  1. SELECT REVERSE(LEFT(REVERSE('abcdef'), 3))

I see the profit of this function in the following. Let we need to determine a position not the first, but the last occurrence of any character (or a sequence of characters) in a character string. Let's remind an example, where we were determining the position of the first character "a" in the name of the ship "California":

Console
Execute
  1. SELECT CHARINDEX('a', name) first_a
  2. FROM Ships
  3. WHERE name = 'California'

Now let's determine the position of the last occurrence of the character "a" in this name. The function CHARINDEX('a', REVERSE(name)) will allow us to find this position, but from the right. In order to get the position of this character, but from the left, it's enough to write

Console
Execute
  1. SELECT LEN(name) + 1 - CHARINDEX('a', REVERSE(name)) first_a
  2. FROM Ships
  3. WHERE name = 'California'
Bookmark and Share
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
спортивный костюм Brotsy . Atomskill
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.