June 14, 2005 at 8:47 am
Please excuse my terminology... but happy it seemed to make sense
June 14, 2005 at 8:50 am
I have ran the original code and it does differ to the count I get using my own code. I have checked and it seems to exclude NULLS.
June 14, 2005 at 8:50 am
NP... that concept is hard to grasp.
June 15, 2005 at 2:34 am
I have to work with PostCodes regularly. We have a problem where users input incomplete postcodes as well as incorrectly formatted postcodes.
I have created the following view which looks at the postcodes and stips out just the postcode region. Makes allowances for london postcodes which differ from the rest of the country. If the postcode does not conform to Royalmail standards then returns PostCodeError
A bit messy but it works
Karl
CREATE VIEW dbo.Conn_Clients_PostCode_Region
AS
SELECT Client_Ref, Post_Code,
PostCodeRegion= CASE
-- Test for null or Blank!
When Post_Code is null then 'PostCodeError'--'Post_Code_Error Null'
When Post_Code ='' then 'PostCodeError'--'Post_Code_Error Blank'
-- illegal char
when (Post_Code LIKE '%[`!"£$%^&*()_+#''*?_;,./]%' ) then 'PostCodeError'--'Post_Code Illeagal Char'
-- starts with number
when substring(ltrim(Post_Code),1,1) not LIKE '[a-z]' then 'PostCodeError'--'Post_Code Illeagal first Char'
-- Second char can't be 0 with number
when substring(ltrim(Post_Code),2,1) = '0' then 'PostCodeError'--'Post_Code Illeagal second Char'
WHEN
-- No Space, Just first Part
(SELECT CHARINDEX(' ', ltrim(Post_Code)) ) = 0 and (len(ltrim(Post_Code))) < 5 THEN
-- Test for London!, Last Char would be letter if so get chars before last letter'
Case
-- Not letter
When isnumeric(SUBSTRING ( ltrim(Post_Code) , Len(ltrim(Post_Code)) , 1 )) = 1 then ltrim(Post_Code)
-- Is Letter
When isnumeric(SUBSTRING ( ltrim(Post_Code) , Len(ltrim(Post_Code)) , 1 )) = 0 then
-- But is second to last a number if not then error
Case
When isnumeric(SUBSTRING ( ltrim(Post_Code) , Len(ltrim(Post_Code))-1 , 1 )) = 1 then
SUBSTRING (ltrim(Post_Code) , 1 , Len(ltrim(Post_Code))-1 )
Else 'PostCodeError'--'Post_Code_Error no number'
end
end
WHEN
-- No Space, Just first Part but longer than 4 char
(SELECT CHARINDEX(' ', ltrim(Post_Code)) ) = 0 and (len(ltrim(Post_Code))) > 4 THEN
'PostCodeError'--'Error'
--Correctly formed
WHEN
(SELECT CHARINDEX(' ', ltrim(Post_Code)) ) > 0
and (SELECT CHARINDEX(' ', ltrim(Post_Code)) )< 6
THEN
--LEFT(ltrim(Post_Code), (CHARINDEX(' ', ltrim(Post_Code)) - 1))
-- Test for London!, Last Char would be letter if so get chars before last letter'
Case
-- Not letter
When isnumeric(SUBSTRING ( ltrim(Post_Code) , (CHARINDEX(' ', ltrim(Post_Code))-1 ) , 1)) = 1 then
SUBSTRING (ltrim(Post_Code) , 1 , (CHARINDEX(' ', ltrim(Post_Code))) )
-- Is Letter
When isnumeric(SUBSTRING ( ltrim(Post_Code) , (CHARINDEX(' ', ltrim(Post_Code))-1 ) , 1 )) = 0 then
--SUBSTRING (ltrim(Post_Code) , 1 , (CHARINDEX(' ', ltrim(Post_Code))-2) )
-- is char before letter a number
Case
When isnumeric(SUBSTRING ( ltrim(Post_Code) , (CHARINDEX(' ', ltrim(Post_Code))-2 ) , 1 )) = 0 then
'PostCodeError'
else
SUBSTRING (ltrim(Post_Code) , 1 , (CHARINDEX(' ', ltrim(Post_Code))-2) )
end
End
WHEN
(SELECT CHARINDEX(' ', ltrim(Post_Code)) ) > 4 THEN 'PostCodeError'--'Post_Code_Error no number'
END
FROM Clients
June 15, 2005 at 2:39 am
Thanks everyone for your help. Its much appreciated.
Carl
June 27, 2005 at 9:54 am
Try:
Select Left(@Str,CharIndex(' ',@Str+' ')-1)
June 28, 2005 at 1:50 am
Test Msg
June 28, 2005 at 2:05 am
Hi All,
Kindly can you please suggest me for the following doubts.
What's the future of SQL Server 2000 Administration & MCDBA.
Does it required for students to upgrade their edition from SQL Server 2000
to SQL Server 5000.
Kindly advice me is it good to study or not...
Thanks in advance
Best Regards
Viewing 8 posts - 16 through 22 (of 22 total)
You must be logged in to reply to this topic. Login to reply