| Author |
Topic |
|
JezLisle
Posting Yak Master
132 Posts |
Posted - 2010-01-20 : 10:49:39
|
| I have an address field [Address 1] that has address input like this belowAddress 1Rehaghstown, 4 Castlefield8 St Martins Ave38 Rockfield Road100 Retreat Park,Gould, 1350 William Perse TerraceI have used this below in my query to split out the address into 2 fields, but my problem is that I want just the numbers to appear in field1 and then the text side of the address into field2SELECTSUBSTRING([Address 1],0,CHARINDEX(' ',[Address 1])),SUBSTRING([Address 1], (CHARINDEX(' ',[Address 1])+1),LEN([Address 1])),[Address 2],[Address 3],[Address 4],[County],FROM MyTableHow can I do it so that it splits how I am looking for and not this Number Address1Rehaghstown, 4 Castlefield8 St Martins Ave38 Rockfield Road100 Retreat Park,Gould, 1350 William Perse TerraceI'm looking for thisNumber Address1 Rehaghstown,4 Castlefield8 St Martins Ave38 Rockfield Road100 Retreat Park, Gould,1350 William Perse Terrace |
|
|
DP978
Constraint Violating Yak Guru
269 Posts |
Posted - 2010-01-20 : 11:05:34
|
quote: Originally posted by JezLisle I have an address field [Address 1] that has address input like this belowAddress 1Rehaghstown, 4 Castlefield8 St Martins Ave38 Rockfield Road100 Retreat Park,Gould, 1350 William Perse TerraceI have used this below in my query to split out the address into 2 fields, but my problem is that I want just the numbers to appear in field1 and then the text side of the address into field2SELECTSUBSTRING([Address 1],0,CHARINDEX(' ',[Address 1])),SUBSTRING([Address 1], (CHARINDEX(' ',[Address 1])+1),LEN([Address 1])),[Address 2],[Address 3],[Address 4],[County],FROM MyTableHow can I do it so that it splits how I am looking for and not this Number Address1Rehaghstown, 4 Castlefield8 St Martins Ave38 Rockfield Road100 Retreat Park,Gould, 1350 William Perse TerraceI'm looking for thisNumber Address1 Rehaghstown,4 Castlefield8 St Martins Ave38 Rockfield Road100 Retreat Park, Gould,1350 William Perse Terrace
You will get cautions against this 1st option, search the forums to see why, but if you know your data you can try something like this...SELECTCase when IsNumeric(Cast((SUBSTRING([Address 1],0,CHARINDEX(' ',[Address 1]))) as Int) = 1 then (SUBSTRING([Address 1],0,CHARINDEX(' ',[Address 1]))) else '' end as 1stPart,Case when IsNumeric(Cast((SUBSTRING([Address 1],0,CHARINDEX(' ',[Address 1]))) as Int) = 1 then SUBSTRING([Address 1], (CHARINDEX(' ',[Address 1])+1) else [Address 1] end as 2ndPart ,LEN([Address 1])),[Address 2],[Address 3],[Address 4],[County],FROM MyTableThe better option is probably to check and see if the first substring IS LIKE '[0-9]%', if it is then do your substring formula, if it is not then leave field 1 blank. For field two you need to check again, if the 1st part is a number then you are good getting the 2nd part, if it is not then just return [Address 1] |
 |
|
|
|
|
|