Get string to the right : RIGHT « String Functions « SQL Server / T-SQL






Get string to the right


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 JAVA2S\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
           
       








Related examples in the same category

1.RIGHT(): starts at the right-most character and counts to the left, returning the specified number of characters
2.RIGHT: return a number of characters from the right-hand side of a string
3.RIGHT(@RIGHT_STRING,10)