Solved

formating of String results.

Posted on 2011-03-04
6
292 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:ewangoya
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 32

Expert Comment

by:ewangoya
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:ewangoya
ewangoya 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now