Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
This is what I'm trying to do:If String AB06 is then I need to get AB05; if it is AB11 I need to get AB10.My select statement looks like this:
select 'AB'+(cast(substring(period,3,2) as int)-1) from table.
Result is AB6. But I want AB06 (ABzero6)But when I do this, I'm getting AB6 instead of AB06. Can someone please tell me how to have '0' in front of 6? I guess when I cast it as integer, it is ignoring the leading 0.Thanks.
sshelper
Posting Yak Master
216 Posts
Posted - 2007-04-25 : 16:15:24
Try the following:select 'AB'+(RIGHT('00' + cast(cast(substring(period,3,2) as int)-1 as varchar(2)))) from table.SQL Server Helperhttp://www.sql-server-helper.com
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts
Posted - 2007-04-25 : 16:48:51
select Period, New_Period = 'AB'+right(right(Period,2)+9999,2)from ( select Period = 'AB01' union all select Period = 'AB02' union all select Period = 'AB04' union all select Period = 'AB06' union all select Period = 'AB11' ) aResults:Period New_Period ------ ---------- AB01 AB00AB02 AB01AB04 AB03AB06 AB05AB11 AB10(5 row(s) affected)