Unix Technical Forum

SQL Statement Problem : Separate long varchar into word seqments 35chars long

This is a discussion on SQL Statement Problem : Separate long varchar into word seqments 35chars long within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi there, I'm having a really tough time with a SQL statement and I am wondering if someone is ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-10-2008, 03:12 PM
jephperro
 
Posts: n/a
Default SQL Statement Problem : Separate long varchar into word seqments 35chars long

Hi there,

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 @find varchar(100) ;
SET @find = 'BIOLOGICAL SURVEY FOUNDATION OF ONTARIO HAPPY IF SOMEONE
CAN HELP SOLVE THIS SQL PROBLEM';
SELECT
@find as ORIGINALSTRING,
-- LEN(@find ) as [LengthOfOriginal],
-- REVERSE(LEFT(@find, 34)) as reverseL,
35-(charindex(' ', REVERSE(LEFT(@find, 34)),0)) as
LocationOfLastSpaceBeforeBreaking,
SUBSTRING(@find, 0, 35-(charindex(' ', REVERSE(LEFT(@find, 34)),0)))
as PART1,
SUBSTRING(@find, 35-(charindex(' ', REVERSE(LEFT(@find, 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?

I appreciate any help.

Jeff
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-12-2008, 10:08 AM
Erland Sommarskog
 
Posts: n/a
Default Re: SQL Statement Problem : Separate long varchar into word seqments 35 chars long

jephperro (jeff.perreault@gmail.com) writes:
> 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.


T-SQL is definitely a poor choice for this sort of job. If you are on
SQL 2005, write a function in C# or VB .Net for the task. Probably
you should use the RegEx classes.

If you are on SQL 2000, try to find solutions client-side.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-13-2008, 07:13 PM
eisaacs@gmail.com
 
Posts: n/a
Default Re: SQL Statement Problem : Separate long varchar into word seqments35 chars long

You might also want to look into address normalization tools,
depending on how much data you're talking about. The standardized/
normalized USPS addresses are a lot shorter than the addresses people
tend to give you. Things like Avenue are shortened to AVE and North
becomes N, etc. Normalizing the addresses might help you out a lot.

If you put in an address here at the USPS site in the link below, it
will normalize it as an example:
http://zip4.usps.com/zip4/welcome.jsp

You might be able to create a webservice that uses this website to
normalize all your addresses, or buy some third-party tool that does
the same.

That's probably going in another direction from what you're thinking,
but if you're mailing items, it's also worth the cost to verify that
the addresses are valid before mailing.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-16-2008, 02:39 PM
--CELKO--
 
Posts: n/a
Default Re: SQL Statement Problem : Separate long varchar into word seqments35 chars long

Look up mailing lists tools from Group 1 or Melissa Data. Life is too
short to write your own package.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-16-2008, 02:39 PM
steve
 
Posts: n/a
Default Re: SQL Statement Problem : Separate long varchar into word seqments35 chars long

Here is a relatively simple iterative solution to this problem:

'Splitting a string into fixed sized word segments'
http://beyondsql.blogspot.com/2008/0...nto-fixed.html

A relational system greatly simplifies problems like this :-)

best,
steve
www.beyondsql.blogspot.com

On May 9, 12:45 pm, jephperro <jeff.perrea...@gmail.com> wrote:
> Hi there,
>
> 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.
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-18-2008, 11:02 PM
steve
 
Posts: n/a
Default Re: SQL Statement Problem : Separate long varchar into word seqments35 chars long

The solution to this problem at:

'Splitting a string into fixed sized word segments'
http://beyondsql.blogspot.com/2008/0...nto-fixed.html

has been updated. I've tried to make it easier to understand. The
comments, I hope, are clearer. If anyone has ideas for further
development, enhancements or direction, by all means contact me If
someone is in a pinch and this can help let me know. I'm always happy
to help someone out.

best,
www.beyondsql.blogspot.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-20-2008, 06:55 PM
Ed Murphy
 
Posts: n/a
Default Re: SQL Statement Problem : Separate long varchar into word seqments35 chars long

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 @find varchar(100) ;
> SET @find = 'BIOLOGICAL SURVEY FOUNDATION OF ONTARIO HAPPY IF SOMEONE
> CAN HELP SOLVE THIS SQL PROBLEM';
> SELECT
> @find as ORIGINALSTRING,
> -- LEN(@find ) as [LengthOfOriginal],
> -- REVERSE(LEFT(@find, 34)) as reverseL,
> 35-(charindex(' ', REVERSE(LEFT(@find, 34)),0)) as
> LocationOfLastSpaceBeforeBreaking,
> SUBSTRING(@find, 0, 35-(charindex(' ', REVERSE(LEFT(@find, 34)),0)))
> as PART1,
> SUBSTRING(@find, 35-(charindex(' ', REVERSE(LEFT(@find, 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:56 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com