loading..
Русский    English
14:00

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  A database management system (DBMS) by Microsoft Corporation. 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: (-7)

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
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.