String Manipulation

  • Real simple issue ! All I am trying to do is build up string value 1 char at a time .

     Set @Last_Match_Name = (+ @Last_Match_Name + @Last_Char)

    Does not work !

    If  @Last_Match_Name  contains 'AB' (from prior @Last_Char's)

    and current value of @Last_Char = 'C'

    I expected to see @Last_Match_Name  value = 'ABC' ,

    instead I get NULL ?

     

    Help

    BennyG

     

     

     

  • ..this seemed to work for me...not sure what you have in your variable declarations...?!?!

    declare @varName varchar(10)
    declare @Last_Match_Name varchar(10)
    declare @Last_Char char(1)
    declare @ctr tinyint
    
    set @ctr = 1
    set @Last_Match_Name = ''
    set @varName = 'abcdefgh'
    
    while @ctr < 10
    begin
    	set @Last_Char = substring(@varName, @ctr, 1)
    	Set @Last_Match_Name = (+ @Last_Match_Name + @Last_Char)
    	set @ctr = @ctr + 1
    	continue
    end
    
    select @Last_Match_Name as concatenatedColumn
    







    **ASCII stupid question, get a stupid ANSI !!!**

  • First, make sure that @Last_Match_Name is big enough to hold the longer string.

    Without knowing the surrounding code, there's two solutions:

    If you are in a loop of some sort:

    IF @Last_Char IS NOT NULL

       Set @Last_Match_Name = @Last_Match_Name + @Last_Char

    If @Last_Char is from a table you could try:

    SELECT @Last_Match_Name = @Last_Match_Name + Last_Char_Column

    FROM MyTable

    WHERE Last_Char_Column IS NOT NULL

     

     

     


    Julian Kuiters
    juliankuiters.id.au

  • Thanks Guys.

    The check for NULL was the answer !

    Many thanks for your help.

    BennyG

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply