Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set parameter in sql, query not working

Tags:

sql

t-sql

I have this code here:

DECLARE @inputform varchar
SET @inputform = 'J61807017B'

SELECT * FROM test where text1 LIKE '@inputform'

This won't give me the desired output, but when i do like this it works:

DECLARE @inputform varchar
SET @inputform = 'J61807017B'

SELECT * FROM test where text1 LIKE 'J61807017B'

Any idea?

like image 703
Oz Magician Avatar asked Mar 18 '26 03:03

Oz Magician


1 Answers

You need to specify the size of the variable and drop the quotes when you use it:

DECLARE @inputform varchar(20)
SET @inputform = 'J61807017B'

SELECT * FROM test where text1 = @inputform
-- or text1 like '%'+ @inputform +'%' if you want to do partial matching

If you don't specify the size for a char/varchar value the size will be 1.

From the manual:

When n is not specified in a data definition or variable declaration statement, the default length is 1.

like image 63
jpw Avatar answered Mar 19 '26 20:03

jpw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!