MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 10th, 2008, 01:24 PM
Offspring22 Offspring22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 5 Offspring22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 13 sec
Reputation Power: 0
Seperating data in a field...

I’m relatively new to SQL, and have a project here at work that I need to get done. I have a table, with a field named temp_link_number, in the format of “0017899162|0019043116|0019043116”. What I need to do is separate these into different entries, so the rest of the data in the record is the same, for all 3 entries, but that number will be unique in each new entry (and in a different field of just "link_number".

Does anyone have any suggestions of what code I can use to select the first 10 digits, the 2nd 10, and 3rd ten? Which are all separated by a |? (the | should not show up in any of the new entries). They are all 10 digits as well, if something is easier using that….. Any tips are greatly appreciated!

Reply With Quote
  #2  
Old July 10th, 2008, 04:36 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,717 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 18 h 31 m 7 sec
Reputation Power: 986
Code:
SELECT SUBSTRING(temp_link_number,1,10) AS link_number
     , other
     , columns
  FROM daTable
UNION ALL
SELECT SUBSTRING(temp_link_number,12,10) 
     , other
     , columns
  FROM daTable
UNION ALL
SELECT SUBSTRING(temp_link_number,23,10) 
     , other
     , columns
  FROM daTable
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old July 10th, 2008, 05:23 PM
Offspring22 Offspring22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 5 Offspring22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 13 sec
Reputation Power: 0
Thanks, that looks like it could be what I need.... One question.... not all temp_link_number's have 3, some have 1, or 2, 3 or even 4.... if it finds a record that doesn't have more then 10 characters, will it just ignore them, or create lines with blank link numbers?

Reply With Quote
  #4  
Old July 10th, 2008, 05:24 PM
Offspring22 Offspring22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 5 Offspring22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 13 sec
Reputation Power: 0
Also, should each select have "as link_number" or does only the first need it as you have shown?

Reply With Quote
  #5  
Old July 10th, 2008, 05:36 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,717 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 18 h 31 m 7 sec
Reputation Power: 986
regarding whether there are 3 numbers, or 4, or only 2... use as many subselects in the UNION query as you think there might be positions, and in each one, use a WHERE condition on the length of the column value to determine whether to extract the number

so, extract the substring from 1 for 10 if the length of the column value is >= 10

extract the substring from 12 for 10 if the length of the column value is >= 21

and so on

and yes, you need provide the column alias only on the first subselect (that's how UNION queries work)

Reply With Quote
  #6  
Old July 10th, 2008, 05:47 PM
Offspring22 Offspring22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 5 Offspring22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 13 sec
Reputation Power: 0
That seems to make sense. One (hopefully) last thing... How does the column value command work? Whats the proper syntax for mssql?

Reply With Quote
  #7  
Old July 10th, 2008, 06:20 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,717 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 18 h 31 m 7 sec
Reputation Power: 986
Quote:
Originally Posted by Offspring22
How does the column value command work?
you mean the length of the column value?

you can use either LEN(temp_link_number) or DATALENGTH(temp_link_number)

look 'em up in da manual to see how they differ


Reply With Quote
  #8  
Old July 14th, 2008, 12:03 PM
Offspring22 Offspring22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 5 Offspring22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 13 sec
Reputation Power: 0
Quote:
Originally Posted by r937
you mean the length of the column value?

you can use either LEN(temp_link_number) or DATALENGTH(temp_link_number)

look 'em up in da manual to see how they differ



Awesome, Thanks a ton, I beleive I have it formated as needed now. Just needed the point in the right direction

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Seperating data in a field...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway