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


Why won't this query return data?


Why won't this query return data?

Author
Message
jshahan
jshahan
SSChasing Mays
SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)

Group: General Forum Members
Points: 651 Visits: 2112

-- create the tally table
SELECT TOP (30)
IDENTITY(INT,0,1) AS N
INTO #Tally
FROM Master.dbo.SysColumns sc1,
Master.dbo.SysColumns sc2
-- Add an index
ALTER TABLE #Tally
ADD CONSTRAINT PK_Tally_N2
PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100

-- strip the commas and keep the string horizontal
declare @datastring varchar(8000),
@datastring2 varchar(8000)
set @datastring = 'A,B,C,D,E,F,G,H,I,J,K'
set @datastring2 = ''

-- Why doesn't the select directly below return anything?
select @datastring2 = @datastring2 + substring(@datastring,N,1)
from #tally
where n <=len(@datastring)
and substring(@datastring,N,1) <> ','

-- this one returns the data
select @datastring2 as CleanString



Hello all

I'm putting together some materials to demonstrate some of the uses for tally tables (thanks Jeff :-) )
and stumbled across a curious issue with the query above that "stuffs" @datastring2.

It doesn't directly return any data (doesn't have to really) but I can't explain why and I was hoping someone could explain that to me.

This is not urgent in any way. I'm just trying to keep learning from you wonderful folks
CapnHector
CapnHector
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1721 Visits: 1789
Are you talking about the following not returning any data?

select @datastring2 = @datastring2 + substring(@datastring,N,1)
from Tally
where n <=len(@datastring)
and substring(@datastring,N,1) <> ','




its because you are actually assigning the output of the query to @datastring2. when you assign a value to a variable using SELECT you get the last value returned from the query (When working with a scalar variable). your query assigns @datastring2 + the substring to the variable @datastring2 which builds the string you want to return.

to demonstrate the principal i will use integers and my tally table

DECLARE @Demo INT = 0

SELECT @Demo = @Demo + N
FROM Tally
WHERE N <= 10

SELECT @Demo



when you run your query you are "Adding" (Concatenating) the variable @datastring2 with the substring so instead of a value of 1+2+3+4+5+6+7+8+9+10 (55) you get your completed string assigned to your variable. once you have assigned a value to your variable the final SELECT @WhatEver returns the value.


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
subbu1
subbu1
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1093 Visits: 695
plc check below query...
declare @a int=0
declare @t table(id int)
insert into @t(id) values(1),(2),(3)
select @a=ID from @t
select @a first

/****************** STMT*******************/
set @a=0
select @a=@a+ID from @t
select @a second
Arjun Sivadasan
Arjun Sivadasan
Right there with Babe
Right there with Babe (792 reputation)Right there with Babe (792 reputation)Right there with Babe (792 reputation)Right there with Babe (792 reputation)Right there with Babe (792 reputation)Right there with Babe (792 reputation)Right there with Babe (792 reputation)Right there with Babe (792 reputation)

Group: General Forum Members
Points: 792 Visits: 976
Check this out. My team ran into an issue when concatenating like in your example. The issue was that the output was different for the same query when a certain index was present/absent.

http://connect.microsoft.com/SQLServer/feedback/details/383641/in-variable-concatenation-and-order-by-filters-results-like-where-condition
jshahan
jshahan
SSChasing Mays
SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)

Group: General Forum Members
Points: 651 Visits: 2112
So it is as simple as the output being redirected from the results pane to the variable. I expected the successful concatenation into the variable but not the redirection as well.

Thank to all for the responses.
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