|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Postcode problemI have a table containing address of contacts. I have been asked to extract these details, but there must be a separation between Address and Postcode. The Address fields are made up of 6 fields. The script below is for the table described above: CREATE TABLE [Customer Address Unit Details] ( [First Name] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Surname] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Address Line 1] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Address Line 2] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Address Line 3] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Address Line 4] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Address Line 5] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Post Code] [char] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Telephone Number] [char] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Email ID] [char] (25) ) ON [PRIMARY] GO I can quite eaily concatonate the Address fields 1-5, by delimiting the string, and then query the Postcode as another field. The problem that I have, is that sometimes the postcode has been entered on to the Address fields 1-5 instead of the designated Postcode field. What is the easiest that I can extract the Postcode for all Customers, for when the Postcode does not exist in the designated field? Kind Regards Ricky (SQL2K/Win2K) SELECT COALESCE(
[Post Code], [Address Line 1], [Address Line 2], [Address Line 3], [Address Line 4], [Address Line 5], '') As [Post Code] ...is what comes to mind. However, I reckon that in the case PostCode is NULL, the postal code can exist together with other address information in any other Address Line column. So you may have to look into string pattern matching and that sort of stuff. But SQL isn't suitable for that, I'd handle those tasks in my application layer instead, not in the database. Cheers, Johan Sjöström MSc, MCAD ricky skrev: Show quote > Hi > > I have a table containing address of contacts. I have been asked to extract > these details, but there must be a separation between Address and Postcode. > > The Address fields are made up of 6 fields. > > The script below is for the table described above: > > CREATE TABLE [Customer Address Unit Details] ( > [First Name] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [Surname] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [Address Line 1] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [Address Line 2] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [Address Line 3] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [Address Line 4] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [Address Line 5] [char] (42) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [Post Code] [char] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [Telephone Number] [char] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , > [Email ID] [char] (25) > ) ON [PRIMARY] > GO > > I can quite eaily concatonate the Address fields 1-5, by delimiting the > string, and then query the Postcode as another field. The problem that I > have, is that sometimes the postcode has been entered on to the Address > fields 1-5 instead of the designated Postcode field. > > What is the easiest that I can extract the Postcode for all Customers, for > when the Postcode does not exist in the designated field? > > Kind Regards > > Ricky > (SQL2K/Win2K) ricky (ri***@ricky.com) writes:
> I can quite eaily concatonate the Address fields 1-5, by delimiting the [Post Code] = CASE WHEN coalesce([Post Code], '') LIKE @pattern> string, and then query the Postcode as another field. The problem that I > have, is that sometimes the postcode has been entered on to the Address > fields 1-5 instead of the designated Postcode field. > > What is the easiest that I can extract the Postcode for all Customers, for > when the Postcode does not exist in the designated field? WHEN coalesce([Address Line 5), ''] LIKE @pattern ... END @pattern here has been assigned a pattern that describes how post codes looks where you are located. (Which judging from your IP address is the UK, and I am definitely not going to try to suggest a pattern for thos post codes!) Since post codes may have entered with or without spaces, you may also want to add things like: coalesce(replace([Post Code], ' ', ''), '') Yes, this will be messy. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx Hi guys
Thank you for your replies, I don't think I was clear in my description, what I meant to convey was that the Postcode could reside in any of the five Address fields, I assume from your responses that there is no easy way to perform a search and yes you are correct, I am from the UK. Kind Regards Ricky Show quote "Erland Sommarskog" <esq***@sommarskog.se> wrote in message news:Xns9835B9347BBYazorman@127.0.0.1... > ricky (ri***@ricky.com) writes: > > I can quite eaily concatonate the Address fields 1-5, by delimiting the > > string, and then query the Postcode as another field. The problem that I > > have, is that sometimes the postcode has been entered on to the Address > > fields 1-5 instead of the designated Postcode field. > > > > What is the easiest that I can extract the Postcode for all Customers, for > > when the Postcode does not exist in the designated field? > > [Post Code] = CASE WHEN coalesce([Post Code], '') LIKE @pattern > WHEN coalesce([Address Line 5), ''] LIKE @pattern > ... > END > > @pattern here has been assigned a pattern that describes how post codes > looks where you are located. (Which judging from your IP address is the > UK, and I am definitely not going to try to suggest a pattern for thos > post codes!) > > Since post codes may have entered with or without spaces, you may also > want to add things like: > > coalesce(replace([Post Code], ' ', ''), '') > > Yes, this will be messy. > > > -- > Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se > > Books Online for SQL Server 2005 at > http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx > Books Online for SQL Server 2000 at > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx ricky (ri***@ricky.com) writes:
> Thank you for your replies, I don't think I was clear in my description, Yes, that was precisely how I understood your question. And we added the> what I meant to convey was that the Postcode could reside in any of the > five Address fields, tacit assumption that it was best to search the address fields in reverse order. > I assume from your responses that there is no easy way to perform a search The code will indeed be messy, but there is some chance to work somethingfrom the outlines that were posted in the thread. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx >> I am from the UK. << I do know if this is still true for 'UK postcode databases', but theRoyal Mail holds the copyright and will licence you an 'outwards' database (i.e. the first segment of the full postcode) for about 150 a year, or an 'inwards' database (i.e. the full 9-or 10-character postcode) for about 500 per year. Throw the Postal Codes in a table and use a REFERENCES constraint. Didn't you use a column (which is nothing like a field!!) which as too big? Your approach is to invite as much bad data as you can, then clean it up as an after thought. If you were going to stage data then use a file (which does have fields), then bring it in. Your front end language should support files and scrubbing. Dassgar Systems Box #915 Bennington, VT 05201-0915 USA Global-Z; international postal codes for lots of places Hi Joe
Thanks for tip. You're quite right about the scenario, although the bad data is not by my choice, it is due to the fact that this is an off-the-shelf mainframe product, which lacks UI field validation and the back end tables are locked down, I've just inherited this project. Kind Regards Ricky Show quote "--CELKO--" <jcelko***@earthlink.net> wrote in message news:1157499952.842783.7440@p79g2000cwp.googlegroups.com... > >> I am from the UK. << > > I do know if this is still true for 'UK postcode databases', but the > Royal Mail holds the copyright and will licence you an 'outwards' > database (i.e. the first segment of the full postcode) for about 150 a > year, or an 'inwards' database (i.e. the full 9-or 10-character > postcode) for about 500 per year. > > Throw the Postal Codes in a table and use a REFERENCES constraint. > Didn't you use a column (which is nothing like a field!!) which as too > big? > > Your approach is to invite as much bad data as you can, then clean it > up as an after thought. If you were going to stage data then use a > file (which does have fields), then bring it in. Your front end > language should support files and scrubbing. > > Dassgar Systems > Box #915 > Bennington, VT 05201-0915 > USA > Global-Z; international postal codes for lots of places > Fixing this well will without a doubt look good on your resume. It's not all
bad. ML --- http://milambda.blogspot.com/ > [Post Code] = CASE WHEN coalesce([Post Code], '') LIKE @pattern This "CASE" statement lacks "THEN" expressions. You probably meant something> WHEN coalesce([Address Line 5), ''] LIKE @pattern > ... > END like that: [Post Code] = CASE WHEN coalesce([Post Code], '') LIKE @pattern THEN [Post Code] WHEN coalesce([Address Line 5], '') LIKE @pattern THEN [Address Line 5] WHEN coalesce([Address Line 4], '') LIKE @pattern THEN [Address Line 4] WHEN coalesce([Address Line 3], '') LIKE @pattern THEN [Address Line 3] WHEN coalesce([Address Line 2], '') LIKE @pattern THEN [Address Line 2] WHEN coalesce([Address Line 1], '') LIKE @pattern THEN [Address Line 1] END Probably there should be some extraction done in those "THEN" expressions (because "Address Line 5" can contain post code plus some other address data). Kamil 'Hilarion' Nowicki Hilarion (hilari0n@noemail.nospam) writes:
Show quote > This "CASE" statement lacks "THEN" expressions. You probably meant Yes, that was what I was thinking of. Thanks for the correction.> something like that: > > [Post Code] = CASE > WHEN coalesce([Post Code], '') LIKE @pattern THEN [Post Code] > WHEN coalesce([Address Line 5], '') LIKE @pattern THEN [Address Line 5] > WHEN coalesce([Address Line 4], '') LIKE @pattern THEN [Address Line 4] > WHEN coalesce([Address Line 3], '') LIKE @pattern THEN [Address Line 3] > WHEN coalesce([Address Line 2], '') LIKE @pattern THEN [Address Line 2] > WHEN coalesce([Address Line 1], '') LIKE @pattern THEN [Address Line 1] > END > > Probably there should be some extraction done in those "THEN" expressions > (because "Address Line 5" can contain post code plus some other address > data). -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
|||||||||||||||||||||||