Home Forums SQL Server 2005 T-SQL (SS2K5) Extracting a specific number of words from a string in sql RE: Extracting a specific number of words from a string in sql

  • I need to do this to parse out data required by an agency we send address details too to place the contents of our address line 1 to their separate Flat No, house number, and street name fields. So I'm basically going to try to select the first two words if address line 1 starts with Flat in to Flat No, if it begins with a number then place the number in to house number field, and then put the remainder (or the else) in to the street name field. Not entirely 100% reliable but will do. Kenneth's response above will work nicely if I'm only looking for two words but will not work with instances like my fourth line where there's multiple spaces. Would prefer a function though that I can re-use taking in a string and a number representing the number of words required. Like the way it works though thanks Kenneth.