Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

formating of String results.

Posted on 2011-03-04
6
Medium Priority
?
313 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 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
Independent Software Vendors: 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

715 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