Author |
Topic |
harrisw48
Starting Member
34 Posts |
Posted - 2007-01-15 : 11:40:34
|
Any see where this is going wrong:I always get NULLS instead of the reversed stringALTER function dbo.backwards(@normal varchar(200))returns varchar(200)asbegin declare @counter as int declare @backwards as varchar(200) set @counter = len(@normal) while @counter > 0 begin set @backwards = @backwards + substring(@normal,@counter,1) set @counter = @counter - 1 endreturn (@backwards)end |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-01-15 : 11:50:57
|
Wouldn't it be easier to use the built-in REVERSE function?CODO ERGO SUM |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-15 : 11:51:58
|
Easier? Yes. l33t ? No.- Jeff |
 |
|
harrisw48
Starting Member
34 Posts |
Posted - 2007-01-15 : 12:01:23
|
It possibly would but quicker to use the REVERSE function I just wanted to know why this bit of code doesnt do what I think it should. |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-01-15 : 12:07:24
|
quote: Originally posted by harrisw48 It possibly would but quicker to use the REVERSE function I just wanted to know why this bit of code doesnt do what I think it should.
I'm sure you'll eventually figure it out. It's fairly obvious.CODO ERGO SUM |
 |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-01-15 : 12:59:11
|
it is? Go with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-15 : 13:18:43
|
!ysae yreV Peter LarssonHelsingborg, Sweden |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-15 : 13:18:47
|
@backwards is never set to an initial value; thus, it is NULL and appending anything to NULL results in: Null.- Jeff |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-01-15 : 13:36:48
|
This should fix it:ALTER function dbo.backwards(@normal varchar(200))returns varchar(200)asbeginreturn reverse(@normal)end CODO ERGO SUM |
 |
|
Alan Schofield
Starting Member
23 Posts |
Posted - 2007-01-16 : 11:12:03
|
Yep, just add SET @Backwards = '' before you do any processing on it.... |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-01-16 : 12:06:25
|
quote: Originally posted by Alan Schofield Yep, just add SET @Backwards = '' before you do any processing on it....
Unless, of course, the input parameter @normal is null...CODO ERGO SUM |
 |
|
|