|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
|
#2
|
||||
|
||||
|
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
|
|
#3
|
|||
|
|||
|
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?
|
|
#4
|
|||
|
|||
|
Also, should each select have "as link_number" or does only the first need it as you have shown?
|
|
#5
|
||||
|
||||
|
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) |
|
#6
|
|||
|
|||
|
That seems to make sense. One (hopefully) last thing... How does the column value command work? Whats the proper syntax for mssql?
|
|
#7
|
||||
|
||||
|
Quote:
you can use either LEN(temp_link_number) or DATALENGTH(temp_link_number) look 'em up in da manual to see how they differ ![]() |
|
#8
|
|||
|
|||
|
Quote:
Awesome, Thanks a ton, I beleive I have it formated as needed now. Just needed the point in the right direction ![]() |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Seperating data in a field... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|