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
 Reversing a string character

Author  Topic 

tadin
Yak Posting Veteran

63 Posts

Posted - 2007-03-19 : 14:32:45
create a function to reverese a string pass it abcd and it returns dcba

you can't use the built in reverese function...

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 14:40:45
We do not do homework here.

In real life scenarios, there is absolutely no law against using REVERSE function.
If your professor can't make better exams, tell him he's a twit.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 15:03:37
DECLARE @String VARCHAR(20)
SELECT @String = 'abcd'

DECLARE @Len INT
SELECT @Len = DATALENGTH(@String)

DECLARE @Result VARCHAR(20)
SELECT @Result = ''

WHILE @Len > 0
BEGIN
SET @Result = @Result + SUBSTRING(@String, @Len, 1)
SET @Len = @Len - 1
END


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-03-19 : 15:05:47
so he asked nicly and you did his homework for him?
nice going...

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 15:08:29
I figured out he would never even know or acknowledge what BOL is...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

tadin
Yak Posting Veteran

63 Posts

Posted - 2007-03-19 : 15:34:21
I tested your syntax embedding in the functions and it works great. I might have further questions on it.

CREATE function reverseString (@inString varchar(200) )
returns varchar(200)
as
BEGIN

DECLARE @String VARCHAR(20)
SELECT @String = 'abcd'

DECLARE @Len INT
SELECT @Len = DATALENGTH(@String)

DECLARE @Result VARCHAR(20)
SELECT @Result = ''

WHILE @Len > 0
BEGIN
SET @Result = @Result + SUBSTRING(@String, @Len, 1)
SET @Len = @Len - 1
end
return @result
END

select dbo.reverseString('abcd')
Go to Top of Page

tadin
Yak Posting Veteran

63 Posts

Posted - 2007-03-19 : 15:44:25
DECLARE @String VARCHAR(20)
SELECT @String = 'abcd' -- how can i make the user choose their own string to input from the function call.

So, far you have been very grateful.
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-03-19 : 16:15:33
http://msdn2.microsoft.com/en-us/library/ms186755.aspx


www.elsasoft.org
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 16:21:28
CREATE function dbo.reverseString (@inString varchar(200) )
returns varchar(200)
as
BEGIN

DECLARE @Len INT
SELECT @Len = DATALENGTH(@inString)

DECLARE @Result VARCHAR(200)
SELECT @Result = ''

WHILE @Len > 0
BEGIN
SET @Result = @Result + SUBSTRING(@inString, @Len, 1)
SET @Len = @Len - 1
end
return @result
END


select dbo.reverseString('abcd')


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

tadin
Yak Posting Veteran

63 Posts

Posted - 2007-03-19 : 16:56:56
This has been very helpful. I would like to go in detail, discussion of each syntax. I mean if you could comment out what's going on would be very helpful.I'm using, the book, Beginning Sql, by paul wilton, john Colby and some of the stuffs you specified aren't in the book. http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764577328.html I'm using this book.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-19 : 19:14:56
Refer to this book. All the stuff used in the function is cover in this book. Best of all, it is F R E E


KH

Go to Top of Page
   

- Advertisement -