April 18, 2009 at 12:31 pm
I am reading two values from a cursor selection into two local variables. I then want to concatenate those variables. This is normally ok, except in this case I'm getting trouble.
Below is a simplified version of the code which produces the problem.
This is what is the print statements output:
X
Y
XY
Y
As you can see, the last print statement should produce 'YX', but it doesn't
Can anyone help?
------------------------------------------------------------------------------
-- COL1 data type is varchar(10)
-- COL2 data type is text
DECARE @VALUE1 VARCHAR(10) , @VALUE2 VARCHAR(10)
DECLARE MYCUR CURSOR FOR SELECT COL1, SUBSTRING(COL2,1,10) FROM TABLE
OPEN MYCUR
FETCH NEXT FROM MYCUR INTO @VALUE1, @VALUE2
PRINT @VALUE1
PRINT @VALUE2
PRINT @VALUE1 + @VALUE2
PRINT @VALUE2 + @VALUE1
CLOSE MYCUR
DEALLOCATE MYCUR
April 18, 2009 at 12:43 pm
No cursor needed, here is a set based way to accomplish your task.
select
COL1,
SUBSTRING(COL2,1,10) as COL2,
COL1 + SUBSTRING(COL2,1,10) as Col1Col2,
SUBSTRING(COL2,1,10) + COL1 as Col2Col1
from
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply