Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

formating of String results.

Posted on 2011-03-04
6
Medium Priority
?
317 Views
Last Modified: 2012-06-21
I have the following Code (see below). It works fine except after the first quote a return is inserted.  What I want to do is take this string and use it as part of a where clause but I need the proper format.  Below is the current results.
DLID = '94045' DLID = '
16070' DLID = '
17136' DLID = '
27793' DLID = '
45871' DLID = '
76162' DLID = '
65493' DLID = '
17881' DLID = '
85790' DLSID = '
110509' DLID = '
84701'

This is what I want:

DLID = '94045' DLID = '16070' DLID = '17136' DLID = '27793' DLID = '45871' DLID = '76162' DLID = '65493' DLID = '17881' DLID = '85790' DLID = '110509' DLID = '84701'

Hope this make sense.
declare @LTable table  (
		LTableID int Identity(1,1) Primary key not null,
		LNum varchar(25))


   DECLARE @position int, @string NVARCHAR(MAX),@separator NCHAR(1), @Value varchar(max);
Declare @Cnt int, @MaxCnt int, @BuildWhere varchar(max);
	set @string = '94045
16070
17136
27793
45871
76162
65493
17881
85790
110509
84701'
   SET @position = 1
	set @separator = CHAR(13)
	set @Cnt = 1
   SET @string = @string + @separator
   WHILE charindex(@separator,@string,@position) <> 0
      BEGIN
set @Value = substring(@string, @position, charindex(@separator,@string,@position) - @position)
         INSERT into @LTable
         SELECT @Value
         SET @position = charindex(@separator,@string,@position) + 1

if @Cnt = 1 
set @BuildWhere = ' DLID = ' + '''' + @Value + ''''
else
set @BuildWhere = @BuildWhere + ' DLID = ' + '''' + @Value + ''''

set @Cnt = 2

END

Print @BuildWhere

Open in new window

0
Comment
Question by:MadIce
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35040335
Use replace to get rid of the Carriage return
declare @LTable table  (
		LTableID int Identity(1,1) Primary key not null,
		LNum varchar(25))


   DECLARE @position int, @string NVARCHAR(MAX),@separator NCHAR(1), @Value varchar(max);
Declare @Cnt int, @MaxCnt int, @BuildWhere varchar(max);
	set @string = '94045
16070
17136
27793
45871
76162
65493
17881
85790
110509
84701'
   SET @position = 1
	set @separator = CHAR(13)
	set @Cnt = 1
   SET @string = @string + @separator
   WHILE charindex(@separator,@string,@position) <> 0
      BEGIN
set @Value = substring(@string, @position, charindex(@separator,@string,@position) - @position)
         INSERT into @LTable
         SELECT @Value
         SET @position = charindex(@separator,@string,@position) + 1

if @Cnt = 1 
set @BuildWhere = ' DLID = ' + '''' + @Value + ''''
else
set @BuildWhere = @BuildWhere + ' DLID = ' + '''' + @Value + ''''

set @Cnt = 2

END

SET @BuildWhere = REPLACE(@BuildWhere, CHAR(13), ' ')

Print @BuildWhere

Open in new window

0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 1000 total points
ID: 35040384
Use char(10) and replace

declare @LTable table  (
LTableID int Identity(1,1) Primary key not null,
LNum varchar(25))


DECLARE @position int, @string NVARCHAR(MAX),@separator NCHAR(1), @Value varchar(max);
Declare @Cnt int, @MaxCnt int, @BuildWhere varchar(max);
      set @string = '94045
16070
17136
27793
45871
76162
65493
17881
85790
110509
84701'
   SET @position = 1
      set @separator = CHAR(13)
      set @Cnt = 1
   SET @string = @string + @separator
   WHILE charindex(@separator,@string,@position) <> 0
      BEGIN
      set @Value = substring(@string, @position, charindex(@separator,@string,@position) - @position)

INSERT into @LTable
         SELECT @Value
         SET @position = charindex(@separator,@string,@position) + 1

if @Cnt = 1
set @BuildWhere = ' DLSID = ' + '''' + REPLACE(@Value, CHAR(10), '') + ''''
else
set @BuildWhere = @BuildWhere + ' DLSID = ' + '''' + REPLACE(@Value, CHAR(10), '') + ''''

set @Cnt = 2
END

Print @BuildWhere
0
 

Author Comment

by:MadIce
ID: 35040406
ewangoya. Tried that and got the same results. Tried this too
SET @BuildWhere = REPLACE(@BuildWhere, CHAR(13), '')
without the space.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35040424
replace char(10) also
declare @LTable table  (
		LTableID int Identity(1,1) Primary key not null,
		LNum varchar(25))


   DECLARE @position int, @string NVARCHAR(MAX),@separator NCHAR(1), @Value varchar(max);
Declare @Cnt int, @MaxCnt int, @BuildWhere varchar(max);
	set @string = '94045
16070
17136
27793
45871
76162
65493
17881
85790
110509
84701'
   SET @position = 1
	set @separator = CHAR(13)
	set @Cnt = 1
   SET @string = @string + @separator
   WHILE charindex(@separator,@string,@position) <> 0
      BEGIN
set @Value = substring(@string, @position, charindex(@separator,@string,@position) - @position)
         INSERT into @LTable
         SELECT @Value
         SET @position = charindex(@separator,@string,@position) + 1

if @Cnt = 1 
set @BuildWhere = ' DLID = ' + '''' + @Value + ''''
else
set @BuildWhere = @BuildWhere + ' DLID = ' + '''' + @Value + ''''

set @Cnt = 2

END

SET @BuildWhere = REPLACE(REPLACE(@BuildWhere, CHAR(13), ' '), CHAR(10), ' ')
print @BuildWhere

Open in new window

0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 1000 total points
ID: 35040432
use this

SET @BuildWhere = REPLACE(REPLACE(@BuildWhere, CHAR(13), ' '), CHAR(10), '')
0
 

Author Closing Comment

by:MadIce
ID: 35040495
Both of these answers worked.  UnifiedIS just had to remove a set of quotes. Thanks so much.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

886 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