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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Reverse a String

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 string

ALTER function dbo.backwards(@normal varchar(200))
returns varchar(200)
as
begin
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
end

return (@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
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-01-15 : 11:51:58
Easier? Yes. l33t ? No.

- Jeff
Go to Top of Page

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.
Go to Top of Page

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
Go to Top of Page

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
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-15 : 13:18:43
!ysae yreV


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

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
Go to Top of Page

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)
as
begin
return reverse(@normal)
end




CODO ERGO SUM
Go to Top of Page

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....
Go to Top of Page

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
Go to Top of Page
   

- Advertisement -