Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How to COALESCE this one?

Author  Topic 

Gooser
Starting Member

15 Posts

Posted - 2006-07-13 : 18:11:30
Here's a complex question that may have a simple answer...Given this database & data:


[CODE]

USE [master]
GO
/****** Object: Database [CoalS] Script Date: 07/13/2006 11:22:11 ******/
CREATE DATABASE [CoalS] ON PRIMARY
( NAME = N'CoalS', FILENAME = N'z:\MSSQL\data\CoalS.mdf' , SIZE = 1024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
LOG ON
( NAME = N'CoalS_log', FILENAME = N'z:\MSSQL\data\CoalS_log.ldf' , SIZE = 1024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
COLLATE SQL_Latin1_General_CP1_CI_AS
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'CoalS', @new_cmptlevel=80
GO
ALTER DATABASE [CoalS] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [CoalS] SET ANSI_NULLS OFF
GO
ALTER DATABASE [CoalS] SET ANSI_PADDING OFF
GO
ALTER DATABASE [CoalS] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [CoalS] SET ARITHABORT OFF
GO
ALTER DATABASE [CoalS] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [CoalS] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [CoalS] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [CoalS] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [CoalS] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [CoalS] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [CoalS] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [CoalS] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [CoalS] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [CoalS] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [CoalS] SET READ_WRITE
GO
ALTER DATABASE [CoalS] SET RECOVERY FULL
GO
ALTER DATABASE [CoalS] SET MULTI_USER
GO
ALTER DATABASE [CoalS] SET TORN_PAGE_DETECTION ON

USE [CoalS]
GO
/****** Object: Table [dbo].[Risk] Script Date: 07/13/2006 15:04:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Risk](
[Risk_ID] [int] NOT NULL,
[Risk_Number] [int] NOT NULL,
[Address_ID] [int] NOT NULL,
CONSTRAINT [PK_Risk] PRIMARY KEY CLUSTERED
(
[Risk_ID] ASC
) ON [PRIMARY]
) ON [PRIMARY]


USE [CoalS]
GO
/****** Object: Table [dbo].[Address] Script Date: 07/13/2006 15:04:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Address](
[Address_ID] [int] NOT NULL,
[Address_Number_NBR] [int] NULL,
[Address_Directional_Prefix_TX] [varchar](4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Address_Rootword_TX] [varchar](64) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Address_Street_Type_TX] [varchar](32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Address_Directional_Suffix_TX] [varchar](4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Address_Ste_or_Apt_CD] [int] NULL,
[Address_Ste_or_Apt_NBR] [int] NULL,
[Address_City_TX] [varchar](64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Address_County_TX] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Address_State_CD] [int] NOT NULL,
[Address_ZIP_CD] [varchar](9) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_Address] PRIMARY KEY CLUSTERED
(
[Address_ID] ASC
) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


USE [CoalS]
GO
/****** Object: Table [dbo].[State] Script Date: 07/13/2006 15:04:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[State](
[State_ID] [int] NOT NULL,
[State_Name_TX] [varchar](39) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[State_CD] [char](2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
CONSTRAINT [PK_State] PRIMARY KEY CLUSTERED
(
[State_ID] ASC
) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


USE [CoalS]
GO
/****** Object: Table [dbo].[Suite_or_Apartment] Script Date: 07/13/2006 15:04:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Suite_or_Apartment](
[Ste_or_Apt_ID] [int] NOT NULL,
[Ste_or_Apt_CD] [char](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Ste_or_Apt_TX] [varchar](9) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

USE CoalS

INSERT INTO CoalS.dbo.Risk VALUES ( 1, 1111, 7 )
INSERT INTO CoalS.dbo.Risk VALUES ( 2, 1221, 6 )
INSERT INTO CoalS.dbo.Risk VALUES ( 3, 1331, 5 )
INSERT INTO CoalS.dbo.Risk VALUES ( 4, 1441, 4 )
INSERT INTO CoalS.dbo.Risk VALUES ( 5, 1551, 3 )
INSERT INTO CoalS.dbo.Risk VALUES ( 6, 1661, 2 )
INSERT INTO CoalS.dbo.Risk VALUES ( 7, 1771, 1 )
INSERT INTO CoalS.dbo.Risk VALUES ( 8, 1881, 8 )

INSERT INTO CoalS.dbo.Address VALUES ( 1, 320, 'E', 'Ash', 'Street', null, null, null, 'Ironwood', 'Gogebic', 3, '49938')
INSERT INTO CoalS.dbo.Address VALUES ( 2, 158, null, 'Black River', 'Road', null, null, null, 'Bessemer', 'Gogebic', 3, '499110148')
INSERT INTO CoalS.dbo.Address VALUES ( 3, 24567, null, '306', 'HWY', null, null, null, 'Sardis', 'Panola', 6, '38666')
INSERT INTO CoalS.dbo.Address VALUES ( 4, 1, null, 'Westbrook', 'Drive', null, null, null, 'Hampton', 'Hampton City', 16, '23665')
INSERT INTO CoalS.dbo.Address VALUES ( 5, 49, null, 'Spruce', 'Street', null, null, null, 'Langley', 'Hampton City', 16, '23666')
INSERT INTO CoalS.dbo.Address VALUES ( 6, 5224, null, 'Chicago', 'Avenue', 'SW', 2, 26, 'Lakewood', 'Pierce', 1, '98499')
INSERT INTO CoalS.dbo.Address VALUES ( 7, 1835, 'W', 'Day Island', 'Boulevard', 'W', null, null, 'University Place', 'Pierce', 1, '98466')
INSERT INTO CoalS.dbo.Address VALUES ( 8, 100, null, 'First', 'Avenue', 'W', 1, 500, 'Seattle', 'King', 1, '98119')
INSERT INTO CoalS.dbo.Address VALUES ( 9, 325, 'W', 'Ash', 'Street', null, null, null, 'Ironwood', 'Gogebic', 3, '49938')
INSERT INTO CoalS.dbo.Address VALUES ( 10, 222, 'E', 'Ayer', 'Street', null, null, null, 'Ironwood', 'Gogebic', 3, '49938')
INSERT INTO CoalS.dbo.Address VALUES ( 11, 249, 'W', 'Ash', 'Street', null, null, null, 'Ironwood', 'Gogebic', 3, '49938')
INSERT INTO CoalS.dbo.Address VALUES ( 12, null, null, 'Ayer', 'Street', null, 2, 123, 'Ironwood', 'Gogebic', 3, '49938')
INSERT INTO CoalS.dbo.Address VALUES ( 13, null, null, 'Heikkala', 'Road', null, null, null, 'Ironwood', 'Gogebic', 3, '49938')
INSERT INTO CoalS.dbo.Address VALUES ( 14, 148, 'E', 'St John''s', 'Road', null, 1, 148, 'Bessemer', 'Gogebic', 3, '49911')
INSERT INTO CoalS.dbo.Address VALUES ( 15, 221, null, 'Baker', 'Street', null, null, null, 'Batesville', 'Panola', 6, '38606')
INSERT INTO CoalS.dbo.Address VALUES ( 16, 9999, 'E', 'Streetname', 'Street', 'N', 1, 1, 'Duluth', 'Superior', 5, '44444')

INSERT INTO CoalS.dbo.State VALUES ( 1, 'Washington', 'WA')
INSERT INTO CoalS.dbo.State VALUES ( 2, 'Rhode Island and Providence Plantations', 'RI')
INSERT INTO CoalS.dbo.State VALUES ( 3, 'Michigan', 'MI')
INSERT INTO CoalS.dbo.State VALUES ( 4, 'Wisconsin', 'WI')
INSERT INTO CoalS.dbo.State VALUES ( 5, 'Minnestoa', 'MN')
INSERT INTO CoalS.dbo.State VALUES ( 6, 'Mississippi', 'MS')
INSERT INTO CoalS.dbo.State VALUES ( 7, 'Missouri', 'MO')
INSERT INTO CoalS.dbo.State VALUES ( 8, 'North Dakota', 'ND')
INSERT INTO CoalS.dbo.State VALUES ( 9, 'South Dakota', 'SD')
INSERT INTO CoalS.dbo.State VALUES ( 10, 'Delaware', 'DE')
INSERT INTO CoalS.dbo.State VALUES ( 11, 'Florida', 'FL')
INSERT INTO CoalS.dbo.State VALUES ( 12, 'North Carolina', 'NC')
INSERT INTO CoalS.dbo.State VALUES ( 13, 'South Carolina', 'SC')
INSERT INTO CoalS.dbo.State VALUES ( 14, 'Georgia', 'GA')
INSERT INTO CoalS.dbo.State VALUES ( 15, 'Tennessee', 'TN')
INSERT INTO CoalS.dbo.State VALUES ( 16, 'Virginia', 'VA')

INSERT INTO CoalS.dbo.Suite_or_Apartment VALUES ( 1, 'STE', 'Suite')
INSERT INTO CoalS.dbo.Suite_or_Apartment VALUES ( 2, 'APT', 'Apartment')



How do I get the stored procedure that I am working on:



/*

EXEC CoalS.dbo.tspRisk_SelectByAddressCriteria
NULL --@Address_Number_NBR int = NULL
, NULL --@Address_Directional_Prefix_TX varchar(3) = NULL
, NULL --@Address_Rootword_TX varchar(64) = NULL
, 'Street' --@Address_Street_Type_TX varchar(64) = NULL
, NULL --@Address_Directional_Suffix_TX varchar(3) = NULL
, NULL --@Address_Ste_or_Apt_CD int = NULL
, NULL --@Address_Ste_or_Apt_NBR int = NULL
, NULL --@Address_City_TX varchar(64) = NULL
, NULL --@Address_County_TX varchar(50) = NULL
, NULL --@Address_State_CD int = NULL
, NULL --@Address_ZIP_CD varchar(9) = NULL
, 0 --@rows_returned_cnt int OUT
, 0 --@rows_found_cnt int OUT

*/

/*

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[tspRisk_SelectByAddressCriteria]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[tspRisk_SelectByAddressCriteria]

*/
CREATE PROC [dbo].[tspRisk_SelectByAddressCriteria]

@Address_Number_NBR int = NULL
, @Address_Directional_Prefix_TX varchar(3) = NULL
, @Address_Rootword_TX varchar(64) = NULL
, @Address_Street_Type_TX varchar(64) = NULL
, @Address_Directional_Suffix_TX varchar(3) = NULL
, @Address_Ste_or_Apt_CD int = NULL
, @Address_Ste_or_Apt_NBR int = NULL
, @Address_City_TX varchar(64) = NULL
, @Address_County_TX varchar(50) = NULL
, @Address_State_CD int = NULL
, @Address_ZIP_CD varchar(9) = NULL
, @rows_returned_cnt int OUT
, @rows_found_cnt int OUT

AS

SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
SET NOCOUNT ON

BEGIN TRANSACTION

DECLARE @RC int

SELECT Address_ID
, Address_Number_NBR
, Address_Directional_Prefix_TX
, Address_Rootword_TX
, Address_Street_Type_TX
, Address_Directional_Suffix_TX
, sa.Ste_or_Apt_CD
, Address_Ste_or_Apt_NBR
, Address_City_TX
, Address_County_TX
, Address_State_CD
, Address_ZIP_CD
FROM [CoalS].[dbo].[Address] AS ad
JOIN CoalS.dbo.State AS st
ON st.State_ID = ad.Address_State_CD
LEFT OUTER JOIN CoalS.dbo.Suite_or_Apartment AS sa
ON sa.Ste_or_Apt_ID = ad.Address_Ste_or_Apt_CD
WHERE --Address_Number_NBR = COALESCE(@Address_Number_NBR, Address_Number_NBR) AND
Address_Directional_Prefix_TX = COALESCE(@Address_Directional_Prefix_TX, Address_Directional_Prefix_TX)
-- AND Address_Rootword_TX = COALESCE(@Address_Rootword_TX, Address_Rootword_TX)
AND Address_Street_Type_TX = COALESCE(@Address_Street_Type_TX, Address_Street_Type_TX)
AND Address_Directional_Suffix_TX = COALESCE(@Address_Directional_Suffix_TX, Address_Directional_Suffix_TX)
-- AND Address_Ste_or_Apt_CD = COALESCE(@Address_Ste_or_Apt_CD, Address_Ste_or_Apt_CD)
-- AND Address_Ste_or_Apt_NBR = COALESCE(@Address_Ste_or_Apt_NBR, Address_Ste_or_Apt_NBR)
-- AND Address_City_TX = COALESCE(@Address_City_TX, Address_City_TX)
-- AND Address_County_TX = COALESCE(@Address_County_TX, Address_County_TX)
-- AND Address_State_CD = COALESCE(@Address_State_CD, Address_State_CD)
-- AND Address_ZIP_CD = COALESCE(@Address_ZIP_CD, Address_ZIP_CD)

--Check for open transactions from this stored procedure
IF @RC <> 0
BEGIN
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
RETURN(@RC)
END

COMMIT TRANSACTION

RETURN(0)
[/code]

To take either values or nulls for the input parameters, and give me varied results as follow for what the user has input.

If the user puts in an Address_Rootword then, I want all of the rows that match, whether they have Directional or not.

If the user puts in an Address_Rootword plus a directional, then I want all the rows that match, whether they have a Directional or not (I will GROUP BY once this works).

The problem is, with the NULL values in the columns, the COALESCE, as written, trims those from the results. I don't want that. Is there a way to 'hinder' or manipulate COALESCE so that it gives me EVERYTHING when I get passed a NULL and there are some NULL values?

I can explain further, if necessary, but this should get some discussion going, I hope.

Please help me eliminate the evil dynamic sql from my database.

Thank you,
v/r

Gooser

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2006-07-13 : 22:19:48
instead of AND use OR?

or just remove the directional criteria since as you mentioned it doesn't matter if you have it or not

or

directional=coalesce(@directional,directional) --something like this


HTH

--------------------
keeping it simple...
Go to Top of Page

Gooser
Starting Member

15 Posts

Posted - 2006-07-14 : 10:33:56
Perhaps I didn't explain this thoroughly enough. It was, afterall, the end of the day.

The issue hear really is that I could take any number of values in the parameters, that is some would have a NULL (not filled in by the user on the webapp) or they could type in some of them. What I would like to do is based on what they do enter, use COALESCE to limit what they get back.

Say they enter an Address_Rootword and an Address_City--call them 1st and Seattle--I want them to get all addresses on 1st Ave N, 1st Ave W, S 1st Ave, SW 1st Street in Seattle.

Say they enter an Address_Rootword, a Directional and a City--call them N, 1st, and Seattle--I still want them to get all addresses on 1st Ave N, 1st Ave W, S 1st Ave, SW 1st Street in Seattle but I will add to the ORDER BY clause to get them to come out right, (that is with the directional they passed in first, then the others alphabetically).

I want this for all of the input parameters.

The problem is, with null values in the columns, the COALESCE is not working as I had hoped it would.

Thanks,

v/r

Gooser

"Why do today
that which might not need to be done tomorrow?" --me
Go to Top of Page

samuelclay
Yak Posting Veteran

71 Posts

Posted - 2006-07-14 : 17:34:50
hmm.. I have done similar queries just like you have, except at that time using field = isnull(@par,field)..

A problem that did arise on occasion is if there were null values in the database, so any search field that allowed nulls had to check for nulls, so the query became an uglier :
where isnull(Address_Directional_Prefix_TX ,'') = isnull(@Address_Directional_Prefix_TX,isnull(Address_Directional_Prefix_TX ,''))

or with coalesce:
where COALESCE(Address_Directional_Prefix_TX ,'')= COALESCE(@Address_Directional_Prefix_TX,COALESCE(Address_Directional_Prefix_TX ,''))

Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2006-07-14 : 18:16:45
First IF @RC <> 0 will always be false, since @RC is never assigned in the code you posted.

..

So if the parameter is null, it should always evaluate to true ?
And if the column value is NULL, and you pass a value in the parameter, it should evaluate to true ?
And if the column is NOT NULL, and you pass a value in the parameter, it must match to be true ?
->
(@Address_Directional_Prefix_TX IS NULL OR NULLIF(Address_Directional_Prefix_TX,@Address_Directional_Prefix_TX) IS NULL)

____________________
I have null problems
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-07-14 : 20:34:44
As rockmoose indicates, there are problems in your logic, which must be addressed before you can do it in SQL.

If the column Address1 can be null, and if I enter Null as the @Address1 parameter, what does that mean? Should *all* rows be returned, regardless of Address1, or just the Null values in Address1? If the answer is the former,then how do I pass in parameters to return only Null values in @Address1 ?



- Jeff
Go to Top of Page

tdietrich
Starting Member

2 Posts

Posted - 2006-07-17 : 11:14:13
You might want to try something like this...

...
WHERE
((@Address_Number_NBR IS NULL) OR (Address_Number_NBR = @Address_Number_NBR))
AND ((@Address_Directional_Prefix_TX IS NULL) OR (Address_Directional_Prefix_TX = @Address_Directional_Prefix_TX))
...

It looks like the transaction-related code is in there by mistake.

-- Tim
Go to Top of Page

Gooser
Starting Member

15 Posts

Posted - 2006-07-18 : 11:07:09
After much frustration and [at least] in part due to the database NOT being normalized, I decided to scrap most of the previous code and go with something like this:



USE [CoalS]
GO
/****** Object: StoredProcedure [dbo].[tspSelectRiskJunk] Script Date: 07/18/2006 07:36:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*

EXEC CoalS.dbo.tspSelectRiskJunk
NULL --@Address_ID int
, 190 --@Address_Number_NBR int
, NULL --@Address_High_NBR int
, NULL --@State_CD int
, NULL --@Ste_or_Apt_CD int
, NULL --@Address_Ste_or_Apt_NBR int
, NULL --@Address_Directional_Prefix_TX varchar 4
, NULL --@Address_Directional_Suffix_TX varchar 4
, NULL --@Address_ZIP_CD varchar(9)
, NULL --@Address_Street_Type_TX varchar(32)
, NULL --@Address_County_TX varchar(50)
, NULL --@Address_City_TX varchar(64)
, 'Squa' --@Address_Rootword_TX varchar(64)

*/

CREATE PROC [dbo].[tspSelectRiskJunk]
@Address_ID int = NULL
, @Address_Number_NBR int = NULL
, @Address_High_NBR int = NULL
, @State_CD int = NULL
, @Ste_or_Apt_CD int = NULL
, @Address_Ste_or_Apt_NBR int = NULL
, @Address_Directional_Prefix_TX varchar(4) = NULL
, @Address_Directional_Suffix_TX varchar(4) = NULL
, @Address_ZIP_CD varchar(9) = NULL
, @Address_Street_Type_TX varchar(32) = NULL
, @Address_County_TX varchar(50) = NULL
, @Address_City_TX varchar(64) = NULL
, @Address_Rootword_TX varchar(64) = NULL

AS

SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
SET NOCOUNT ON

BEGIN TRANSACTION

DECLARE
@RC int

/* ^ His Code ^ */
/* v My Code v */


SELECT TOP 100
ad.Address_ID
, rs.Risk_Number
, ad.Address_Number_NBR
, ad.Address_High_NBR
, ad.Address_Directional_Prefix_TX
, ad.Address_Rootword_TX
, ad.Address_Street_Type_TX
, ad.Address_Directional_Suffix_TX
, sa.Ste_or_Apt_CD
, ad.Address_Ste_or_Apt_NBR
, ad.Address_City_TX
, ad.Address_County_TX
, st.State_CD
, ad.Address_ZIP_CD
FROM [CoalS].[dbo].[Address] AS ad
JOIN CoalS.dbo.State AS st
ON st.State_ID = ad.Address_State_CD
LEFT OUTER JOIN CoalS.dbo.Suite_or_Apartment AS sa
ON sa.Ste_or_Apt_ID = ad.Address_Ste_or_Apt_CD
JOIN CoalS.dbo.Risk AS rs
ON rs.Address_ID = ad.Address_ID
ORDER BY CASE WHEN st.state_id = @State_CD THEN 0 --change 'WA' to @incomingvar
WHEN st.state_id IS NULL THEN 4
ELSE 2
END

, CASE WHEN Address_County_TX = @Address_County_TX THEN 0 --change 'Gogebic' to @incomingvar
WHEN Address_County_TX LIKE ( @Address_County_TX + '%' ) THEN 2
WHEN Address_County_TX LIKE ( '%' + @Address_County_TX + '%' ) THEN 4
WHEN Address_County_TX IS NULL THEN 8
ELSE 16
END
--city
, CASE WHEN Address_City_TX = @Address_City_TX THEN 0 --the record exactly matches the variable
WHEN Address_City_TX LIKE ( @Address_City_TX + '%' ) THEN 2 --the record starts with the variable, i.e. Ash returns Ash, Ashe, & Asheville but NOT Washington, nor Nashville
WHEN Address_City_TX LIKE ( '%' + @Address_City_TX + '%' ) --the record contains the variable passed in Ash returns Ash, Ashe, Asheville, Washington, Nashville, & Wash
AND ( st.state_id = @State_CD ) THEN 4 --AND the state passed in matches
WHEN Address_City_TX IS NULL THEN 8 --There is no City record in the database for the row
ELSE 16
END
--zip
, CASE WHEN Address_ZIP_CD LIKE @Address_ZIP_CD THEN 0 --Exact match
WHEN Address_ZIP_CD = LEFT ( @Address_ZIP_CD , 5 ) THEN 2 --Row has exact match on first 5 characters of what is passed in
WHEN Address_ZIP_CD LIKE ( @Address_ZIP_CD + '%' ) THEN 4 --First 5 digits of row has exact match on what is passed in
WHEN Address_ZIP_CD IS NULL THEN 16 --No ZIP in the database for the row
ELSE 8 --Any other condition
END

, CASE WHEN Address_Rootword_TX = @Address_Rootword_TX THEN 0 --the record exactly matches the variable
WHEN Address_Rootword_TX LIKE ( @Address_Rootword_TX + '%' ) THEN 2 --the record starts with the variable, i.e. Ash returns Ash, Ashe, & Asheville but NOT Washington, nor Nashville
WHEN DIFFERENCE(Address_Rootword_TX, @Address_Rootword_TX) = 4 THEN 4 --if the Difference of the Soundex of the row and the variable is 4 (They sound the same by soundex rules.)
WHEN DIFFERENCE(Address_Rootword_TX, @Address_Rootword_TX) = 3 THEN 8 --if the Difference of the Soundex of the row and the variable is 3 (They sound similar by soundex rules.)
WHEN Address_Rootword_TX LIKE '%' + @Address_Rootword_TX + '%' THEN 16 --the record contains the variable passed in Ash returns Ash, Ashe, Asheville, Washington, Nashville, & Wash
ELSE 32 --Any other condition
END
, CASE WHEN Address_Number_NBR = @Address_Number_NBR THEN 0 --Address Number exactly matches the one passed in
WHEN Address_High_NBR > @Address_Number_NBR
AND Address_Number_NBR < @Address_Number_NBR THEN 2
WHEN Address_Number_NBR / 10 = ( @Address_Number_NBR / 10 )
AND Address_Rootword_TX = @Address_Rootword_TX THEN 4 --Address Number exactly matches the one passed in (except the Ones' Position)
WHEN Address_Number_NBR / 100 = ( @Address_Number_NBR / 100 )
AND Address_Rootword_TX = @Address_Rootword_TX THEN 8 --Address Number exactly matches the one passed in (before the Tens' Position)
WHEN Address_Number_NBR / 1000 = ( @Address_Number_NBR / 1000 )
AND Address_Rootword_TX = @Address_Rootword_TX THEN 16 --Address Number exactly matches the one passed in (before the Hundreds' Position)
WHEN Address_Number_NBR / 10000 = ( @Address_Number_NBR / 10000 )
AND Address_Rootword_TX = @Address_Rootword_TX THEN 32 --Address Number exactly matches the one passed in (before the Thousands' Position)
WHEN Address_Number_NBR IS NULL THEN 64 --the record is null for this row/column
ELSE 128
END
, Address_Number_NBR
, CASE WHEN Address_Directional_Prefix_TX LIKE @Address_Directional_Prefix_TX THEN 0 --Address Prefix exactly matches
WHEN Address_Directional_Prefix_TX LIKE 'N' THEN 4 --Address Prefix records that are N
WHEN Address_Directional_Prefix_TX LIKE 'NE' THEN 8 --Address Prefix records that are NE
WHEN Address_Directional_Prefix_TX LIKE 'E' THEN 16 --Address Prefix records that are E
WHEN Address_Directional_Prefix_TX LIKE 'SE' THEN 32 --Address Prefix records that are SE
WHEN Address_Directional_Prefix_TX LIKE 'S' THEN 64 --Address Prefix records that are S
WHEN Address_Directional_Prefix_TX LIKE 'SW' THEN 128 --Address Prefix records that are SW
WHEN Address_Directional_Prefix_TX LIKE 'W' THEN 256 --Address Prefix records that are W
WHEN Address_Directional_Prefix_TX LIKE 'NW' THEN 512 --Address Prefix records that are NW (clockwise around compass)
WHEN Address_Directional_Prefix_TX IS NULL THEN 2 --the record is null for this row/column (the two returns these second)
ELSE 1024 --Any other condition
END
, CASE WHEN Address_Directional_Suffix_TX LIKE @Address_Directional_Suffix_TX THEN 0 --Address Suffix exactly matches
WHEN Address_Directional_Suffix_TX LIKE 'N' THEN 4 --Address Suffix records that are N
WHEN Address_Directional_Suffix_TX LIKE 'NE' THEN 8 --Address Suffix records that are NE
WHEN Address_Directional_Suffix_TX LIKE 'E' THEN 16 --Address Suffix records that are E
WHEN Address_Directional_Suffix_TX LIKE 'SE' THEN 32 --Address Suffix records that are SE
WHEN Address_Directional_Suffix_TX LIKE 'S' THEN 64 --Address Suffix records that are S
WHEN Address_Directional_Suffix_TX LIKE 'SW' THEN 128 --Address Suffix records that are SW
WHEN Address_Directional_Suffix_TX LIKE 'W' THEN 256 --Address Suffix records that are W
WHEN Address_Directional_Suffix_TX LIKE 'NW' THEN 512 --Address Suffix records that are NW
WHEN Address_Directional_Suffix_TX IS NULL THEN 2 --the record is null for this row/column (the two returns these second)
ELSE 1024 --Any other condition
END
, CASE WHEN Address_Street_Type_TX LIKE @Address_Street_Type_TX THEN 0 --Address Street Type exactly matches
WHEN Address_Street_Type_TX IS NULL THEN 2 --the record is null for this row/column (the two returns these second)
ELSE 4 --Any other condition (Street Types that have a record, but do not match the Street Type passed in)
END
, Address_Street_Type_TX
, st.state_cd
, Address_County_TX
, Address_City_TX
, Address_Rootword_TX

IF @RC <> 0
BEGIN
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
RETURN(@RC)
END

COMMIT TRANSACTION

RETURN(0)



Please note that some residual code is still in there (@RC, etc.) this is because I am trying to re-write a dynamic sql stored procedure into something (anything) else. The other consideration here is that use of ORDER BY, rather than WHERE allows me to not limit what is returned too much and allow what needs to happen, and that is BAD SPELERS OF THE WORLD UNTIE! What I am saying is that rather than limiting by what the user types in, I want to sort their opportunities in a smarter way, so that if they spell Ashe Avenue as Ash Avenue, then they will still see Ashe Avenue near the top of their result set, and they won't have to go back to the drawing board (search page). They should also see near the top Ash Street, Asheville Lane, and even Washington Drive.

I am open to questions, comments, and rude remarks regarding my logic/illogic in this procedure. If you like or dislike the order I am returning this, please let me know. I want the best possible result set for this one.

Thanks,
v/r

Gooser
Go to Top of Page
   

- Advertisement -