Friday 21 February 2014

Character arrays in T-SQL

--Inputs
declare @x varchar(100)
set @x = 'WORDS'
--Table of letters (like an array)
select substring(@x, number, 1) as v
from master.dbo.spt_values where number between 1 and len(@x) group by number order by number
--Example manipulation
declare @y varchar(100)
select @y = isnull(@y, '') + char(ascii(substring(@x, number, 1))+1)
from master.dbo.spt_values where number between 1 and len(@x) group by number order by number
select @x as 'Original', @y as 'Manipulated (shifted by 1)'
/*Results
v   
----
W
O
R
D
S
Original         Manipulated (shifted by 1)  
---------------- -----------------------------
WORDS            XPSET
*/

No comments:

Post a Comment