loading..
Русский    English
01:57

RIGHT function

The function RIGHT that complements LEFT returns a specified number of characters from the right of a character expression:

  1. RIGHT(character_expression , integer_expression)

Here is, for example, the way to determine the names of the ships that start and end with the same letter:

Console
Execute
  1. SELECT name
  2. FROM Ships
  3. WHERE LEFT(name, 1) = RIGHT(name, 1)

The thing that we got an empty resulting set means that such ships are absent in our database. Let's take a combination - a class and a name of a ship.

The combining of two string values into one is called concatenation, and in the  Cистема управления реляционными базами данных (СУБД), разработанная корпорацией Microsoft. SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL Server sign "+" is used for this operation ("||" in standard). So,

Console
Execute
  1. SELECT *
  2. FROM (SELECT class +' '+ name AS cn
  3. FROM Ships
  4. ) x
  5. WHERE LEFT(cn, 1) = RIGHT(cn, 1)

Here we separate by space the class and the name of a ship. Besides, in order not to repeat the whole construction in the function argument, we use a subquery. The result will look like this:

Cn
Iowa Missouri
North Carolina Washington

But what if a string expression will contain only one character? The query will output it. You can easily check it by

Console
Execute
  1. SELECT *
  2. FROM (SELECT class +' '+ name AS cn
  3. FROM Ships
  4. UNION ALL
  5. SELECT 'a' AS nc
  6. ) x
  7. WHERE LEFT(cn, 1) = RIGHT(cn, 1)

In order to exclude this case, one more useful function LEN can be used.

Suggested exercises: 35

Bookmark and Share
The book was updated
yesterday
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.
Rambler's Top100