September 25, 2012 at 6:05 am
hi all,
i have the word 'tapsaw1'
can i get the result as follows by not tusing while loop?
tapsaw1
tapsaw
tapsa
taps
tap
ta
t
apsaw1
apsaw
apsa
aps
ap
a
psaw1
..
..
Like this?
thanks in advance
September 25, 2012 at 6:15 am
Use a numbers table
DECLARE @s-2 VARCHAR(10)
SET @s-2 = 'tapsaw1'
SELECT SUBSTRING(@s,t2.number,t1.number)
FROM master.dbo.spt_values t1
INNER JOIN master.dbo.spt_values t2 ON t2.type='P' AND t2.number BETWEEN 1 AND LEN(@s) - t1.number + 1
WHERE t1.type='P' AND t1.number BETWEEN 1 AND LEN(@s)
ORDER BY t2.number ,t1.number DESC
Fixed...
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537September 25, 2012 at 7:02 am
Mark-101232 (9/25/2012)
Use a numbers table
DECLARE @s-2 VARCHAR(10)
SET @s-2 = 'tapsaw1'
SELECT SUBSTRING(@s,t2.number,t1.number)
FROM master.dbo.spt_values t1
INNER JOIN master.dbo.spt_values t2 ON t2.type='P' AND t2.number BETWEEN 1 AND t1.number-1
WHERE t1.type='P' AND t1.number BETWEEN 1 AND LEN(@s)
ORDER BY t2.number ,t1.number DESC
That doesn't work for me:
----------
tapsaw1
tapsaw
tapsa
taps
tap
ta
apsaw1
apsaw1
apsaw
apsa
aps
psaw1
psaw1
psaw1
psaw
saw1
saw1
saw1
aw1
aw1
w1
September 25, 2012 at 7:58 am
This does though, with small adjustments:
DECLARE @s-2 VARCHAR(10)
SET @s-2 = 'tapsaw1'
SELECT SUBSTRING(@s,t2.number,t1.number-(t2.number-1))--,t2.number,t1.number-(t2.number-1)
FROM master.dbo.spt_values t1
INNER JOIN master.dbo.spt_values t2 ON t2.type='P' AND t2.number BETWEEN 1 AND t1.number
WHERE t1.type='P' AND t1.number BETWEEN 1 AND LEN(@s)
ORDER BY t2.number ,t1.number DESC
----------
tapsaw1
tapsaw
tapsa
taps
tap
ta
t
apsaw1
apsaw
apsa
aps
ap
a
psaw1
psaw
psa
ps
p
saw1
saw
sa
s
aw1
aw
a
w1
w
1
September 25, 2012 at 8:07 am
I made a different adjustment and seems easier to read (at least for me :-D)
SELECT SUBSTRING(@s,t2.number,t1.number)
FROM master.dbo.spt_values t1
INNER JOIN master.dbo.spt_values t2 ON t2.type='P' AND t2.number BETWEEN 1 AND LEN(@s)
WHERE t1.type='P' AND t1.number BETWEEN 1 AND LEN(@s) - t2.number + 1
ORDER BY t2.number ,t1.number DESC
September 25, 2012 at 8:13 am
Luis Cazares (9/25/2012)
I made a different adjustment and seems easier to read (at least for me :-D)
SELECT SUBSTRING(@s,t2.number,t1.number)
FROM master.dbo.spt_values t1
INNER JOIN master.dbo.spt_values t2 ON t2.type='P' AND t2.number BETWEEN 1 AND LEN(@s)
WHERE t1.type='P' AND t1.number BETWEEN 1 AND LEN(@s) - t2.number + 1
ORDER BY t2.number ,t1.number DESC
which is almost identical to the fix I put in
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537September 25, 2012 at 8:17 am
Mark-101232 (9/25/2012)
Luis Cazares (9/25/2012)
I made a different adjustment and seems easier to read (at least for me :-D)
SELECT SUBSTRING(@s,t2.number,t1.number)
FROM master.dbo.spt_values t1
INNER JOIN master.dbo.spt_values t2 ON t2.type='P' AND t2.number BETWEEN 1 AND LEN(@s)
WHERE t1.type='P' AND t1.number BETWEEN 1 AND LEN(@s) - t2.number + 1
ORDER BY t2.number ,t1.number DESC
which is almost identical to the fix I put in
:w00t: I didn't see it before 😀
September 25, 2012 at 8:54 pm
Just for fun and to show that there's always more than one way to skin a cat:
DECLARE @s-2 VARCHAR(10) = 'tapsaw1'
;WITH rCTE (n, s) AS (
SELECT LEN(@s), @s-2
UNION ALL
SELECT LEN(s) - 1, SUBSTRING(s, 2, LEN(s) - 1)
FROM rCTE
WHERE LEN(s) > 1
UNION ALL
SELECT LEN(s) - 1, SUBSTRING(s, 1, LEN(s) - 1)
FROM rCTE
WHERE LEN(s) > 1)
SELECT DISTINCT s
FROM rCTE
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply