Solved

formating of String results.

Posted on 2011-03-04
6
307 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
[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
  • 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 250 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 250 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

617 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