SQL Server/T-SQL/String Functions/RIGHT

Материал из SQL эксперт
Перейти к: навигация, поиск

Get string to the right

   <source lang="sql">

25> 26> IF EXISTS(SELECT name FROM sys.tables 27> WHERE name = "T") 28> DROP TABLE T 29> GO 1> 2> CREATE TABLE T ( 3> c1 int, 4> c2 varchar(8000) 5> ) 6> GO 1> 2> DECLARE @v1 varchar(max) 3> 4> SET @v1 = REPLICATE("A",7999) + "B" 5> INSERT T VALUES (1, @v1) 6> SELECT RIGHT(c2,2) "Right 2 of c2" FROM T 7> 8> SET @v1 = @v1 + "B" 9> INSERT T VALUES (2, @v1) 10> SELECT RIGHT(c2,2) "Right 2 of c2" FROM T 11> GO (1 rows affected) Right 2 of c2


AB Msg 8152, Level 16, State 10, Server sqle\SQLEXPRESS, Line 9 String or binary data would be truncated. The statement has been terminated. (1 rows affected) Right 2 of c2


AB (1 rows affected) 1> 2> drop table t 3> GO

      </source>
   
  


RIGHT: return a number of characters from the right-hand side of a string

   <source lang="sql">

1> -- RIGHT: return a number of characters from the right-hand side of a string. 2> 3> DECLARE @RIGHT_STRING char(100) 4> SET @RIGHT_STRING = "www.sqle.ru" 5> 6> SELECT RIGHT(@RIGHT_STRING,10) 7> GO


(1 rows affected) 1>

      </source>
   
  


RIGHT(@RIGHT_STRING,10)

   <source lang="sql">

1> -- This produces a result of ten spaces, because the last 10 characters are space filled. 2> 3> DECLARE @RIGHT_STRING varchar(100) 4> SET @RIGHT_STRING = "www.sqle.ru" 5> 6> SELECT RIGHT(@RIGHT_STRING,10) 7> GO


sqle.ru (1 rows affected) 1>

      </source>
   
  


RIGHT(): starts at the right-most character and counts to the left, returning the specified number of characters

   <source lang="sql">

1> 2> -- RIGHT(): starts at the right-most character and counts to the left, returning the 3> -- specified number of characters. 4> 5> 6> DECLARE @FullName VarChar(25) 7> SET @FullName = "George Washington" 8> SELECT RIGHT(@FullName, 5) 9> 10> GO


ngton (1 rows affected) 1>

      </source>