Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concat within cursor won't work :-( What am I missing? 1

Status
Not open for further replies.

RhythmAddict112

Programmer
Jun 17, 2004
625
US
Hey all,
Thanks for looking at my post...I am sure I'm either not understanding something here, or just flat out doing something wrong but..the concatenation of the line below in read isn't working. (The value is null) How can I get this to concatenate?

Code:
    DECLARE @currYear varchar(4)
    DECLARE @sqlQ varchar(8000)
	 
    DECLARE c1 CURSOR FOR
	select parsedString from ParseString('2006,2005,2004', ',') 
	--ParseString() is a function that is basically doing a split()

    OPEN c1

    FETCH NEXT FROM c1
    INTO @currYear

    WHILE @@FETCH_STATUS = 0
    BEGIN
		
		[COLOR=red]set @sqlQ =  concat('select * from lkp_h where k=' + @CurrYear,@sqlQ)[/color]
		

	FETCH NEXT FROM c1
        INTO @currYear
	
    END

    CLOSE c1
    DEALLOCATE c1

    select @sqlQ

 
Code:
    DECLARE @currYear varchar(4)
    DECLARE @sqlQ varchar(8000)
    
    [!]Set @sqlQ = ''[/!]

    DECLARE c1 CURSOR FOR
    select parsedString from ParseString('2006,2005,2004', ',')
    --ParseString() is a function that is basically doing a split()

    OPEN c1

    FETCH NEXT FROM c1
    INTO @currYear

    WHILE @@FETCH_STATUS = 0
    BEGIN
        
        set @sqlQ =  concat('select * from lkp_h where k=' + @CurrYear,@sqlQ)
        

    FETCH NEXT FROM c1
        INTO @currYear
    
    END

    CLOSE c1
    DEALLOCATE c1

    select @sqlQ

-George

"the screen with the little boxes in the window." - Moron
 
The thing you need to remember is that the default behavior of SQL Server is such that when you concatenate strings... if one value is null, the output will be null.

Ex:

Code:
Select 'Concatenate With' + NULL
Select 'Hello' + 'World'

The first output will be null, but the second one will be what you expect. Like I said... This is the default behavior, which can be changed, but I don't necessarily recommend it.

-George

"the screen with the little boxes in the window." - Moron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top