Solved

Cannot concatenate a string in t-SQL

Posted on 2004-09-21
6
2,696 Views
Last Modified: 2012-05-05
I'm passing a comma separated string to my stored procedure, looping through it and trying to concatenate a string (sql). The loop is working as i can print each individual item as i go through it - but the variable that i am concatenating prints as blank after the loop - what is going wrong? here is the test code:

CREATE procedure wsp_search
@table varchar(32),
@list varchar(128),
@allwords int

AS

declare @sql varchar(4000),@term varchar(10), @pos int

begin
      set nocount on

      set @list = ltrim(rtrim(@list))+ ','
      set @pos = charindex(',', @list, 1)

      if replace(@list, ',', '') <> ''
      begin
            while @pos > 0
                  begin
                        set @term = ltrim(rtrim(left(@list, @pos - 1)))
                        print len(@term)
                        if len(@term)>0
                              begin
                                    --construct sql here
                                    select @sql = @sql  + '|||' + @term
                                    --select @sql=@term *this is ok but above is not!!!*
                                    --print @sql
                              end
                        set @list = right(@list, len(@list) - @pos)
                        set @pos = charindex(',', @list, 1)
      
                  end
      end
      print @sql
      --exec(@sql)      
end

GO
0
Comment
Question by:lildrc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 17

Accepted Solution

by:
BillAn1 earned 65 total points
ID: 12114276
you have "select @sql = @sql  + '|||' + @term"
but you never initialise @sql. It's initial value will be NULL, and concatinataing NULL with something else results in NULL. You just need to add an initial line of code to set @sql = '' before you go into the loop.

CREATE procedure wsp_search
@table varchar(32),
@list varchar(128),
@allwords int

AS

declare @sql varchar(4000),@term varchar(10), @pos int

begin
     set nocount on

     set @sql = ''

     set @list = ltrim(rtrim(@list))+ ','
     set @pos = charindex(',', @list, 1)

     if replace(@list, ',', '') <> ''
     begin
          while @pos > 0
               begin
                    set @term = ltrim(rtrim(left(@list, @pos - 1)))
                    print len(@term)
                    if len(@term)>0
                         begin
                              --construct sql here
                              select @sql = @sql  + '|||' + @term
                              --select @sql=@term *this is ok but above is not!!!*
                              --print @sql
                         end
                    set @list = right(@list, len(@list) - @pos)
                    set @pos = charindex(',', @list, 1)
     
               end
     end
     print @sql
     --exec(@sql)    
end
0
 
LVL 15

Assisted Solution

by:jdlambert1
jdlambert1 earned 60 total points
ID: 12114279
You're not initializing @sql. Try this:

declare @sql varchar(4000),@term varchar(10), @pos int
set @sql = ''
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12114291
You're still fast on the draw, Bill!
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 17

Expert Comment

by:BillAn1
ID: 12114542
dead heat, I think!!
0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 12114580
I got your goal- why do not use REPLACE function:

select '|||'+ replace ('1,2,3,4,5,6,7',',','|||')
--result |||1|||2|||3|||4|||5|||6|||7
0
 

Author Comment

by:lildrc
ID: 12115707
looks good guys - that was puzzling me for ages! - i'll try it out at work tomorrow and issue the points.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question