Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Microsoft SQL Server > Re: SQL Stateme...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 7 Topic 11031 of 11517
Post > Topic >>

Re: SQL Statement Problem : Separate long varchar into word seqments

by Ed Murphy <emurphy42@[EMAIL PROTECTED] > May 19, 2008 at 11:08 PM

jephperro wrote:

> I'm having a really tough time with a SQL statement and I am wondering
> if someone is able to help out or point me in the right direction.
> 
> I have a table of names which can be very long.  These names get
> printed on envelopes.  The problem is the envelope can only hold 35
> characters per line.  I have to divide into 4 lines at most.
> 
> So I need to separate these long varchars into segments, no longer
> than 35 characters but preserving whole words.
> 
> So far my approach has been to take a LEFT segment, REVERSE it, find
> the first space with CHARINDEX and use it to calculate how many
> characters to take in a SUBBSTRING.
> 
> Here's an example of what I have been trying.  I can find the first
> two segments, but then it starts to get confusing.
> 
> 
> DECLARE @[EMAIL PROTECTED]
  varchar(100) ;
> SET @[EMAIL PROTECTED]
 = 'BIOLOGICAL SURVEY FOUNDATION OF ONTARIO HAPPY IF SOMEONE
> CAN HELP SOLVE THIS SQL PROBLEM';
> SELECT
> @[EMAIL PROTECTED]
 as ORIGINALSTRING,
> -- LEN(@[EMAIL PROTECTED]
 ) as [LengthOfOriginal],
> -- REVERSE(LEFT(@[EMAIL PROTECTED]
 34)) as reverseL,
> 35-(charindex(' ', REVERSE(LEFT(@[EMAIL PROTECTED]
 34)),0)) as
> LocationOfLastSpaceBeforeBreaking,
> SUBSTRING(@[EMAIL PROTECTED]
 0, 35-(charindex(' ', REVERSE(LEFT(@[EMAIL PROTECTED]
 34)),0)))
> as PART1,
> SUBSTRING(@[EMAIL PROTECTED]
 35-(charindex(' ', REVERSE(LEFT(@[EMAIL PROTECTED]
 34)),0)), 35 )
> as PART2,
> ' ? ' as PART3,
> ' ? ' as PART4
> 
> Can anyone suggest a better approach?  Am I going to be able to do
> this in SQL?

create table LongNames (
  Name varchar(100)
)

insert into LongNames (Name) values (
  'BIOLOGICAL SURVEY FOUNDATION OF ONTARIO HAPPY IF SOMEONE CAN HELP
SOLVE THIS SQL PROBLEM'
)

create table #SplitNames (
  Name  varchar(101),
  Line1 varchar(35),
  Line2 varchar(35),
  Line3 varchar(35),
  Line4 varchar(35)
)

insert into #SplitNames (Name)
select Name + ' ' from LongNames

update #SplitNames
set Line1 = substring(Name, 0, 35-(charindex(' ', reverse(left(Name,
34)),0))),
    Name  = substring(Name, 35-(charindex(' ', reverse(left(Name,
34)),0)), 100 )

update #SplitNames
set Line2 = substring(Name, 0, 35-(charindex(' ', reverse(left(Name,
34)),0))),
    Name  = substring(Name, 35-(charindex(' ', reverse(left(Name,
34)),0)), 100 )

update #SplitNames
set Line3 = substring(Name, 0, 35-(charindex(' ', reverse(left(Name,
34)),0))),
    Name  = substring(Name, 35-(charindex(' ', reverse(left(Name,
34)),0)), 100 )

update #SplitNames
set Line4 = substring(Name, 0, 35-(charindex(' ', reverse(left(Name,
34)),0))),
    Name  = substring(Name, 35-(charindex(' ', reverse(left(Name,
34)),0)), 100 )

select * from #SplitNames

drop table #SplitNames

drop table LongNames
 




 7 Posts in Topic:
SQL Statement Problem : Separate long varchar into word seqments
jephperro <jeff.perrea  2008-05-09 12:45:21 
Re: SQL Statement Problem : Separate long varchar into word seqm
Erland Sommarskog <esq  2008-05-11 21:46:20 
Re: SQL Statement Problem : Separate long varchar into word seqm
eisaacs@[EMAIL PROTECTED]  2008-05-12 17:33:26 
Re: SQL Statement Problem : Separate long varchar into word seqm
--CELKO-- <jcelko212@[  2008-05-13 12:13:30 
Re: SQL Statement Problem : Separate long varchar into word seqm
steve <rog11228@[EMAIL  2008-05-14 03:23:23 
Re: SQL Statement Problem : Separate long varchar into word seqm
steve <rog11228@[EMAIL  2008-05-16 19:18:06 
Re: SQL Statement Problem : Separate long varchar into word seqm
Ed Murphy <emurphy42@[  2008-05-19 23:08:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Dec 2 22:08:10 CST 2008.