Unix Technical Forum

Split a field

This is a discussion on Split a field within the SQL Server forums, part of the Microsoft SQL Server category; --> Whats the best way to do the following? Field1 in Table1 contains numbers and characters seperated by # Examples: ...


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 02-29-2008, 08:27 AM
chudson007@hotmail.com
 
Posts: n/a
Default Split a field

Whats the best way to do the following?

Field1 in Table1 contains numbers and characters seperated by #
Examples: aaa#01, kjhkjhjh#21 and jlkjlkj#123

How can I create two new fields in Table1, one containing what is to
the left of the # and the other what is to the right?

Regards,
Ciarán

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 08:27 AM
Hugo Kornelis
 
Posts: n/a
Default Re: Split a field

On 12 May 2005 08:49:49 -0700, chudson007@hotmail.com wrote:

>Whats the best way to do the following?
>
>Field1 in Table1 contains numbers and characters seperated by #
>Examples: aaa#01, kjhkjhjh#21 and jlkjlkj#123
>
>How can I create two new fields in Table1, one containing what is to
>the left of the # and the other what is to the right?
>
>Regards,
>Ciarán


Hi Ciarán,

UPDATE MyTable
SET NewCol1 = LEFT(ConcatCol,
CHARINDEX('#', ConcatCol) - 1)
, NewCol2 = SUBSTRING(ConcatCol,
CHARINDEX('#', ConcatCol) + 1,
LEN(ConcatCol))
WHERE ConcatCol LIKE '%#%'

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 08:28 AM
chudson007@hotmail.com
 
Posts: n/a
Default Re: Split a field

Perfect.
Cheers!
Ciarán

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 08:28 AM
chudson007@hotmail.com
 
Posts: n/a
Default Re: Split a field

Hugo,

How shoud I modify the SQL so that if there is no '#' in ConcatCol,
then NewCol1 = ConcatCol

Regards,
Ciarán

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 08:28 AM
Hugo Kornelis
 
Posts: n/a
Default Re: Split a field

On 13 May 2005 03:07:06 -0700, chudson007@hotmail.com wrote:

>Hugo,
>
>How shoud I modify the SQL so that if there is no '#' in ConcatCol,
>then NewCol1 = ConcatCol
>
>Regards,
>Ciarán


Hi Ciarán,

UPDATE MyTable
SET NewCol1 = CASE
WHEN ConcatCol LIKE '%#%'
THEN LEFT(ConcatCol,
CHARINDEX('#', ConcatCol) - 1)
ELSE ConcatCol
END
, NewCol2 = CASE
WHEN ConcatCol LIKE '%#%'
THEN SUBSTRING(ConcatCol,
CHARINDEX('#', ConcatCol) + 1,
LEN(ConcatCol))
ELSE NULL
END

Or you could simply run two consecutive UPDATE statements (the one from
my first message, followed by an update that sets newcol1 equal to
concatcol for the remaining rows).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
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 11:22 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com