Reputation: 738
I have a stored procedure that uses the LIKE
operator to search for a truck location among some other parameters
@location nchar(20),
@time time,
@date date
AS
select
DonationsTruck.VechileId, Phone, Location, [Date], [Time]
from
Vechile, DonationsTruck
where
Vechile.VechileId = DonationsTruck.VechileId
and (((Location like '%'+@location+'%') or (Location like '%'+@location) or (Location like @location+'%') ) or [Date]=@date or [Time] = @time)
I null the other parameters and search by location only but it always returns no results even when I used the full name of the location
Upvotes: 26
Views: 155129
Reputation: 1
The above condition for LIKE is wrong, you need to use LIKE command is COL1 like '%@Variable%'
Upvotes: -1
Reputation: 242
for mysql- SELECT * FROM tablename WHERE columnanme LIKE '%'+ @location+'%';
for oracle - SELECT * FROM tablename WHERE columnanme LIKE '%'|| @location ||'%';
Upvotes: 0
Reputation: 11
CREATE PROCEDURE [dbo].[Sp_schemeallotment] (
@SchemeName NVARCHAR (255) = ' ',
@Ind INT = 0
)
AS
BEGIN
IF ( @Ind = 1 )
BEGIN
SELECT *
FROM schemeallotment
END
IF ( @Ind = 2 )
BEGIN
SELECT *
FROM schemeallotment
WHERE schemename LIKE '%' + @SchemeName + '%'
END
END
--exec sp_SchemeAllotment @Ind=2, @SchemeName='shree'
go
It is important to assign these variable like this:
(
@SchemeName nvarchar (255) = ' ',
@Ind int = 0
)
Upvotes: 0
Reputation: 1
CREATE procedure [dbo].[sp_SchemeAllotment]
(
@SchemeName nvarchar (255)='',
@Ind int=0
)
As
Begin
if (@Ind = 1)
begin
SELECT * FROM SchemeAllotment
End
if (@Ind = 2)
begin
SELECT * FROM SchemeAllotment where SchemeName like '%'+ @SchemeName +'%'
end
End
--exec sp_SchemeAllotment @Ind=2, @SchemeName='shree'
GO
Upvotes: -1
Reputation: 500
...
WHERE ...
AND (@Location is null OR (Location like '%' + @Location + '%'))
AND (@Date is null OR (Date = @Date))
This way it is more obvious the parameter is not used when null.
Upvotes: -1
Reputation: 161
I was working on same. Check below statement. Worked for me!!
SELECT * FROM [Schema].[Table] WHERE [Column] LIKE '%' + @Parameter + '%'
Upvotes: 7
Reputation: 13
EG : COMPARE TO VILLAGE NAME
ALTER PROCEDURE POSMAST
(@COLUMN_NAME VARCHAR(50))
AS
SELECT * FROM TABLE_NAME
WHERE
village_name LIKE + @VILLAGE_NAME + '%';
Upvotes: -5
Reputation: 27367
Your datatype for @location nchar(20)
should be @location nvarchar(20)
, since nChar has a fixed length (filled with Spaces).
If Location is nchar too you will have to convert it:
... Cast(Location as nVarchar(200)) like '%'+@location+'%' ...
To enable nullable parameters with and AND
condition just use IsNull or Coalesce for comparison, which is not needed in your example using OR
.
e.g. if you would like to compare for Location AND Date and Time.
@location nchar(20),
@time time,
@date date
as
select DonationsTruck.VechileId, Phone, Location, [Date], [Time]
from Vechile, DonationsTruck
where Vechile.VechileId = DonationsTruck.VechileId
and (((Location like '%'+IsNull(@location,Location)+'%')) and [Date]=IsNUll(@date,date) and [Time] = IsNull(@time,Time))
Upvotes: 46