Hello, SQL 2008 R2
Because the person who created the database table used Microsoft conversion from Access to SQL 2000 our zip codes are now of data type FLOAT. We are living with this but we have one process that pulls over 2 million records into a table used for reports but it takes a long time to run.
So breaking down the query one thing I found I needed an index which I created. But when I went a little further testing a few columns at a time I found that before the function call database.dbo.udf2_CnvtZip5(Zip) the query took 1:38 after 4:04. So I code the conversion in the query
which only took 1:48.
Since we have about six differenct zip codes we wanted a function, however the time to run the query is getting prohibitive. Note that the test code I was working with only had the one zip.
The function
So any comments or suggestions? Converting the table is not an option at this time or I would do that in a hear beat.
Thanks
djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
Because the person who created the database table used Microsoft conversion from Access to SQL 2000 our zip codes are now of data type FLOAT. We are living with this but we have one process that pulls over 2 million records into a table used for reports but it takes a long time to run.
So breaking down the query one thing I found I needed an index which I created. But when I went a little further testing a few columns at a time I found that before the function call database.dbo.udf2_CnvtZip5(Zip) the query took 1:38 after 4:04. So I code the conversion in the query
Code:
CASE
WHEN ISNULL(Zip, 0) = 0 THEN NULL
WHEN Zip > 99999.0 THEN RIGHT('00000' + CAST(CAST(Zip/10000 AS INT) AS VARCHAR), 5)
ELSE RIGHT('00000' + CAST(CAST(Zip AS INT) AS VARCHAR), 5)
END AS MyZip
Since we have about six differenct zip codes we wanted a function, however the time to run the query is getting prohibitive. Note that the test code I was working with only had the one zip.
The function
Code:
ALTER FUNCTION [dbo].[udf2_CnvtZip5]
(
@ZipIn float
)
RETURNS varchar(5)
AS
-- Outputs a five digit zip from Float
BEGIN
DECLARE @ZipOut VARCHAR(25)
IF @ZipIn IS NULL OR @ZipIn = 0
SET @ZipOut = NULL
ELSE
IF @ZipIn > 99999.0
SET @ZipOut = RIGHT('00000' + CAST(CAST(@ZipIn/10000 AS INT) AS VARCHAR), 5)
ELSE
SET @ZipOut = RIGHT('00000' + CAST(CAST(@ZipIn AS INT) AS VARCHAR), 5)
RETURN @ZipOut
END
Thanks
djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!