SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Reverse string without built in functions


Reverse string without built in functions

Author
Message
GSquared
GSquared
SSC Guru
SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)

Group: General Forum Members
Points: 57369 Visits: 9730
Michael Valentine Jones (4/17/2009)
OK, as long as we are doing useless SQL, I want to add numbers together without using any Arithmetic, Logical, Assignment, String, Bitwise, or Unary Operators or any Mathematical or Aggregate Functions.

create table #Sums (
Num1 int not null,
Num2 int not null,
Sum12 int);

insert into #Sums (Num1, Num2, Sum12)
select 0, 0, 0 union all
select 1, 0, 1 union all
select 2, 0, 2 union all
select 3, 0, 3 union all
select 4, 0, 4 union all
select 5, 0, 5 union all
select 6, 0, 6 union all
select 7, 0, 7 union all
select 8, 0, 8 union all
select 9, 0, 9 union all
select 0, 1, 1 union all
select 1, 1, 2 union all
select 2, 1, 3 union all
select 3, 1, 4 union all
select 4, 1, 5 union all
select 5, 1, 6 union all
select 6, 1, 7 union all
select 7, 1, 8 union all
select 8, 1, 9 union all
select 9, 1, 10 union all
select 0, 2, 2 union all
select 1, 2, 3 union all
select 2, 2, 4 union all
select 3, 2, 5 union all
select 4, 2, 6 union all
select 5, 2, 7 union all
select 6, 2, 8 union all
select 7, 2, 9 union all
select 8, 2, 10 union all
select 9, 2, 11 union all
select 0, 3, 3 union all
select 1, 3, 4 union all
select 2, 3, 5 union all
select 3, 3, 6 union all
select 4, 3, 7 union all
select 5, 3, 8 union all
select 6, 3, 9 union all
select 7, 3, 10 union all
select 8, 3, 11 union all
select 9, 3, 12 union all
select 0, 4, 4 union all
select 1, 4, 5 union all
select 2, 4, 6 union all
select 3, 4, 7 union all
select 4, 4, 8 union all
select 5, 4, 9 union all
select 6, 4, 10 union all
select 7, 4, 11 union all
select 8, 4, 12 union all
select 9, 4, 13 union all
select 0, 5, 5 union all
select 1, 5, 6 union all
select 2, 5, 7 union all
select 3, 5, 8 union all
select 4, 5, 9 union all
select 5, 5, 10 union all
select 6, 5, 11 union all
select 7, 5, 12 union all
select 8, 5, 13 union all
select 9, 5, 14 union all
select 0, 6, 6 union all
select 1, 6, 7 union all
select 2, 6, 8 union all
select 3, 6, 9 union all
select 4, 6, 10 union all
select 5, 6, 11 union all
select 6, 6, 12 union all
select 7, 6, 13 union all
select 8, 6, 14 union all
select 9, 6, 15 union all
select 0, 7, 7 union all
select 1, 7, 8 union all
select 2, 7, 9 union all
select 3, 7, 10 union all
select 4, 7, 11 union all
select 5, 7, 12 union all
select 6, 7, 13 union all
select 7, 7, 14 union all
select 8, 7, 15 union all
select 9, 7, 16 union all
select 0, 8, 8 union all
select 1, 8, 9 union all
select 2, 8, 10 union all
select 3, 8, 11 union all
select 4, 8, 12 union all
select 5, 8, 13 union all
select 6, 8, 14 union all
select 7, 8, 15 union all
select 8, 8, 16 union all
select 9, 8, 17 union all
select 0, 9, 9 union all
select 1, 9, 10 union all
select 2, 9, 11 union all
select 3, 9, 12 union all
select 4, 9, 13 union all
select 5, 9, 14 union all
select 6, 9, 15 union all
select 7, 9, 16 union all
select 8, 9, 17 union all
select 9, 9, 18;

declare @Num1 int, @Num2 int;

select @Num1 = 1, @Num2 = 5;

select Sum12 as [Sum]
from #Sums
where Num1 = @Num1
and Num2 = @Num2;



The table can be extended pretty much indefinitely, of course.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34750 Visits: 9518
Heh. At least someone remembers their 3rd grade Addition tables! :-)

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14357 Visits: 11848
GSquared (4/17/2009)
Michael Valentine Jones (4/17/2009)
OK, as long as we are doing useless SQL, I want to add numbers together without using any Arithmetic, Logical, Assignment, String, Bitwise, or Unary Operators or any Mathematical or Aggregate Functions.

create table #Sums (
Num1 int not null,
Num2 int not null,
Sum12 int);

insert into #Sums (Num1, Num2, Sum12)
select 0, 0, 0 union all
select 1, 0, 1 union all
select 2, 0, 2 union all
select 3, 0, 3 union all
select 4, 0, 4 union all
select 5, 0, 5 union all
select 6, 0, 6 union all
select 7, 0, 7 union all
select 8, 0, 8 union all
select 9, 0, 9 union all
select 0, 1, 1 union all
select 1, 1, 2 union all
select 2, 1, 3 union all
select 3, 1, 4 union all
select 4, 1, 5 union all
select 5, 1, 6 union all
select 6, 1, 7 union all
select 7, 1, 8 union all
select 8, 1, 9 union all
select 9, 1, 10 union all
select 0, 2, 2 union all
select 1, 2, 3 union all
select 2, 2, 4 union all
select 3, 2, 5 union all
select 4, 2, 6 union all
select 5, 2, 7 union all
select 6, 2, 8 union all
select 7, 2, 9 union all
select 8, 2, 10 union all
select 9, 2, 11 union all
select 0, 3, 3 union all
select 1, 3, 4 union all
select 2, 3, 5 union all
select 3, 3, 6 union all
select 4, 3, 7 union all
select 5, 3, 8 union all
select 6, 3, 9 union all
select 7, 3, 10 union all
select 8, 3, 11 union all
select 9, 3, 12 union all
select 0, 4, 4 union all
select 1, 4, 5 union all
select 2, 4, 6 union all
select 3, 4, 7 union all
select 4, 4, 8 union all
select 5, 4, 9 union all
select 6, 4, 10 union all
select 7, 4, 11 union all
select 8, 4, 12 union all
select 9, 4, 13 union all
select 0, 5, 5 union all
select 1, 5, 6 union all
select 2, 5, 7 union all
select 3, 5, 8 union all
select 4, 5, 9 union all
select 5, 5, 10 union all
select 6, 5, 11 union all
select 7, 5, 12 union all
select 8, 5, 13 union all
select 9, 5, 14 union all
select 0, 6, 6 union all
select 1, 6, 7 union all
select 2, 6, 8 union all
select 3, 6, 9 union all
select 4, 6, 10 union all
select 5, 6, 11 union all
select 6, 6, 12 union all
select 7, 6, 13 union all
select 8, 6, 14 union all
select 9, 6, 15 union all
select 0, 7, 7 union all
select 1, 7, 8 union all
select 2, 7, 9 union all
select 3, 7, 10 union all
select 4, 7, 11 union all
select 5, 7, 12 union all
select 6, 7, 13 union all
select 7, 7, 14 union all
select 8, 7, 15 union all
select 9, 7, 16 union all
select 0, 8, 8 union all
select 1, 8, 9 union all
select 2, 8, 10 union all
select 3, 8, 11 union all
select 4, 8, 12 union all
select 5, 8, 13 union all
select 6, 8, 14 union all
select 7, 8, 15 union all
select 8, 8, 16 union all
select 9, 8, 17 union all
select 0, 9, 9 union all
select 1, 9, 10 union all
select 2, 9, 11 union all
select 3, 9, 12 union all
select 4, 9, 13 union all
select 5, 9, 14 union all
select 6, 9, 15 union all
select 7, 9, 16 union all
select 8, 9, 17 union all
select 9, 9, 18;

declare @Num1 int, @Num2 int;

select @Num1 = 1, @Num2 = 5;

select Sum12 as [Sum]
from #Sums
where Num1 = @Num1
and Num2 = @Num2;



The table can be extended pretty much indefinitely, of course.


I did say no Assignment operators, so that disqualifies this statement:
select @Num1 = 1, @Num2 = 5;


GSquared
GSquared
SSC Guru
SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)

Group: General Forum Members
Points: 57369 Visits: 9730
That's not part of the addition. That's just setting up a framework to run it. Run the query without that, with the numbers directly in the Where clause, instead of using variables, you'll be fine.

If you truly want addition without any mathematical, logical, etc., arguments, here you go:

Question: "What's some number added to another number?" (Can't have assignments in there, so you can't actually state what numbers you want to add.)

Answer: "A number."

It's a correct answer! Woot! :-)

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
jcrawf02
jcrawf02
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4900 Visits: 19324
GSquared (4/20/2009)
That's not part of the addition. That's just setting up a framework to run it. Run the query without that, with the numbers directly in the Where clause, instead of using variables, you'll be fine.

If you truly want addition without any mathematical, logical, etc., arguments, here you go:

Question: "What's some number added to another number?" (Can't have assignments in there, so you can't actually state what numbers you want to add.)

Answer: "A number."

It's a correct answer! Woot! :-)


I was gonna suggest sp_Abacus, but your solution runs faster.

---------------------------------------------------------
How best to post your question
How to post performance problems
Tally Table:What it is and how it replaces a loop

"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
abhi620
abhi620
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 109
hi,
it is batch code it gives revers of all rows in the table........
i think this is good solution for --reversing data with out using REVERSE() function


create table std
(id int identity,
name varchar(20))

insert into std(name)
values ('abhijeet'),('omkar'),('tushar')

create table #table
(id int ,
name varchar(20))




declare @str varchar(20)

--if exists(select * from std)
--begin
declare @count int
set @count=(select COUNT(*) from std)

--declare @tempcntid int=@count
declare @i int=@count


--while exists(select * from std)
while @i!=0
begin
set @str=(select name from std where id=@i)
declare @str2 varchar(20)=' '
declare @j int=1

while @j<=LEN(@str)
begin
set @str2=SUBSTRING(@str,@j,1)+ @str2
set @j=@j+1
end
--print @str2
insert into #table(id,name)values(@i,@str2)
--set @tempcntid=@tempcntid-1
set @i=@i-1
end
go

select * from std
go
select * from #table
go
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)

Group: General Forum Members
Points: 213763 Visits: 41977
abhi620 (5/20/2011)
i think this is good solution for...


With only very few exceptions, the RBAR of a WHILE loop is almost never a good solution. Please try it on a couple of hundred thousand rows and see.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
James_R_Alves
James_R_Alves
SSC-Enthusiastic
SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)

Group: General Forum Members
Points: 187 Visits: 196
Just modify the next iteration call a bit, and the recursive solution works:

ALTER function dbo.StringReverse
(
@InString varchar(20)
)
returns varchar(20)
AS
begin
declare @RevString varchar(20)

IF len(@InString) in (0,1)
set @RevString = @InString

ELSE
set @RevString =
(
RIGHT(@instring,1) -- Could use SUBSTRING, but why not RIGHT ?
+
dbo.StringReverse(substring(@InString, 1, len(@InString)-1))
)
return @RevString
end

Obviously, "WITHOUT using any built-in functions" isn't quite accurate, as SUBSTRING and LEN are used, So if there are exceptions, RIGHT could also be used to provide better clarity...
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41200 Visits: 20000
Here's another method:

-- Parameters
DECLARE @String VARCHAR(8000)
SET @String = 'Reverse'

-- Local Variables
DECLARE @OutputString VARCHAR(8000); SET @OutputString = '';
DECLARE @StrLen INT; SET @StrLen = DATALENGTH(@String);
DECLARE @MappingTable TABLE (n INT PRIMARY KEY CLUSTERED);

INSERT INTO @MappingTable (n)
SELECT TOP(@StrLen) n = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM syscolumns a, syscolumns b;

UPDATE @MappingTable SET @OutputString = @OutputString + SUBSTRING(@String,(@StrLen+1)-n,1);

SELECT FormattedAmount = @OutputString;



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
sivajaggu11
sivajaggu11
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 10
declare @string varchar(60)
set @string='reverse string'
;with Reverse_string
as
(
select @string string,cast('' as varchar(50)) Rstring,LEN(@string)LN
union all
select SUBSTRING(string,0,ln)string,cast(Rstring+substring(string,LN,1)as varchar(50)) Rstring,ln-1 ln from Reverse_string
where LN>0
)
select Rstring from Reverse_string where ln=0
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search