Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass variable in a LIKE statement. 1

Status
Not open for further replies.

Colt21

IS-IT--Management
Mar 18, 2008
2
US
Can you please help with something I thought I could simply find, but have not. How can I use a variable in a LIKE statement?

I tried:

SELECT * FROM TABLE WHERE COLUMN LIKE '%@VARIABLE%'

but it seems it is looking at the variable as text instead of variable.

I am using MS SQL Express

Thanks
 
Code:
SELECT * FROM TABLE WHERE COLUMN LIKE '%' + @VARIABLE + '%'

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
Microsoft MVP VFP
 
Thank you. Worked perfectly. Once I got that I just had to change my data type from int to varchar. Thanks again.
 
I am having a similar issue. The where clause will not accept the variable I am feeding it. If I hard code the value in the where clause everything is ok.

declare @employer nvarchar

set @employer = 'ACME'

select *
from #BOP_Member
where employer = @employer

Thanks much for your time and help!
 
Well I'll be if I didn't use like and it worked fine.

Is there a way to use the '=' and not use like?
 
You should start a new thread.
BTW I never had any troubles in variables in WHERE clause.

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
Microsoft MVP VFP
 
cghoga, that should work, what error are you getting?

"NOTHING is more important in a database than integrity." ESquared
 
Thanks SQL Sister,

Sorry my initial post was a bit cryptic. I am not getting an error. No records are being returned.

If I need to start a new post I will. Since SQL Sister replied I am continuuing this thread.

Please let me know.

Thanks again for your help.

 
if you get no records then nothing in your temp table meets the criteria inteh where clause. If you think this is not correct, then I would look to see how you get the records in the temp table.

"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top