3

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?

2
  • 4
    Have you tried without the surrounding single quotes ? SELECT * FROM test where text1 LIKE @inputform Commented Aug 6, 2015 at 12:40
  • @ccheneson, yes and still nothing Commented Aug 6, 2015 at 12:41

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.