JefB
Programmer
- Dec 29, 2000
- 56
I have an Airline table in SQL2K.
The fields, all varchar(50), are: AirlineID, AirlineName and AirportsServed.
AirportsServed contains 1 or more airport codes: i.e. "ORD,CLE,DFW,DCA,LAX"
I need an SP that will return all airlines serving a given airport. In other words; show all airlines with "DCA" somewhere in the AirportsServed field.
I have tried this:
CREATE PROCEDURE [dbo].[AirlineLookup]
@ASrvd varchar
AS
SELECT DISTINCT AirlineID, AirlineName, AirportsServed
FROM dbo.Airlines
WHERE patindex('%' + @ASrvd + '%', AirportsServed) >0
GO
The results are every record in which the letter "C" is anywhere in the AirportsServed field.
I also tried:
CREATE PROCEDURE [dbo].[AirlineLookup]
@ASrvd varchar
AS
SELECT DISTINCT AirlineID, AirlineName, AirportsServed
FROM dbo.Airlines
WHERE (AirportsServed like '%' + @ASrvd +'%')
GO
The same result.
I also tried using "charindex" (instead of "patindex"
.
How do I get a variable (@ASrvd) to work with wildcards ('%') or look at a complete pattern?
JefB
The fields, all varchar(50), are: AirlineID, AirlineName and AirportsServed.
AirportsServed contains 1 or more airport codes: i.e. "ORD,CLE,DFW,DCA,LAX"
I need an SP that will return all airlines serving a given airport. In other words; show all airlines with "DCA" somewhere in the AirportsServed field.
I have tried this:
CREATE PROCEDURE [dbo].[AirlineLookup]
@ASrvd varchar
AS
SELECT DISTINCT AirlineID, AirlineName, AirportsServed
FROM dbo.Airlines
WHERE patindex('%' + @ASrvd + '%', AirportsServed) >0
GO
The results are every record in which the letter "C" is anywhere in the AirportsServed field.
I also tried:
CREATE PROCEDURE [dbo].[AirlineLookup]
@ASrvd varchar
AS
SELECT DISTINCT AirlineID, AirlineName, AirportsServed
FROM dbo.Airlines
WHERE (AirportsServed like '%' + @ASrvd +'%')
GO
The same result.
I also tried using "charindex" (instead of "patindex"
How do I get a variable (@ASrvd) to work with wildcards ('%') or look at a complete pattern?
JefB