Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Substituted data values

Author  Topic 

tleonard
Starting Member

13 Posts

Posted - 2008-11-09 : 19:05:10
I'm trying to find out if there is anyway to change the value of a string variable containing 0123 to abcd where 0=a, 1=b, 2=c, 3=d. Likewise, 3210 = dcba. Thank you.

tl

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-09 : 22:58:27
[code]SELECT REPLACE(REPLACE(REPLACE(REPLACE(stringcol,'0','a'),'1','b'),'2','c'),'3','d') FROM Table[/code]
Go to Top of Page

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-11-09 : 23:04:52
try this
DECLARE @Str VARCHAR(MAX)
SELECT @Str = '3210' -- 0123

SELECT @Str = REPLACE(@Str, Num.Val, CHAR(CH.number))
FROM Master..spt_Values CH
INNER JOIN (SELECT SUBSTRING(@Str, Number, 1) AS 'Val'
FROM Master..spt_values
WHERE type = 'P'
and number > 0
and Number <= LEN(@Str)) Num ON Num.Val = CH.Number - 97

SELECT @Str



"There is only one difference between a dream and an aim. A dream requires soundless sleep to see, whereas an aim requires sleepless efforts to achieve..!!"
Go to Top of Page

soorajtnpki
Posting Yak Master

231 Posts

Posted - 2008-11-10 : 02:41:13
hi
try this

DECLARE @Str VARCHAR(30)
declare @result varchar(30)
SELECT @Str = '0123456' -- 0123
set @result=''
while(len(@str) <> '')
begin
set @result=@result+char((ascii(left(@str,1)))+49)
set @Str=replace(@Str,left(@Str,1),'')
end
select @result

ok tanx
Go to Top of Page

tleonard
Starting Member

13 Posts

Posted - 2008-11-10 : 16:31:14
Thank you. This provided what I was looking for.
Go to Top of Page
   

- Advertisement -