0

Can I put like expression into SQL statement like this?

select @Count = SUM(cast(Value as int)) 
from tTag
where Name like '[Car],[Truck],[Bike]'
5
  • you could connect each statement with an or and write multple likes Commented Nov 17, 2015 at 7:26
  • 2
    Which database you are using? Commented Nov 17, 2015 at 7:27
  • this is only an example Commented Nov 17, 2015 at 7:29
  • 1
    @M.Ay:- Your example is fine but what you are trying to achieve is not clear.(If you are looking for an exact match of names then it can be achieved using IN and if you are looking for pattern then you can use OR) Also you should tag the database which you are using. Commented Nov 17, 2015 at 7:30
  • Added sql-server and tsql tags based on the non-standard syntax Commented Nov 17, 2015 at 7:47

4 Answers 4

1

If you are searching for whole words use:

where Name = 'Car' or Name = 'Truck' or Name = 'Bike' --or
where Name = '[Car]' or Name = '[Truck]' or Name = '[Bike]' --or
where Name in ('Car', 'Truck', 'Bike') --or
where Name in ('[Car]', '[Truck]', '[Bike]')

If you are searching as parts of words then use:

where Name like '%Car%' or Name like '%Truck%' or Name like '%Bike%'

But if you are searching for strings like some text [car] some text then this won't work:

where Name like '%[Car]%' or Name like '%[Truck]%' or Name like '%[Bike]%'

because %[Car]% this will match for example some text ca some text. You should escape [ and ] symbols. But it depends on database engine. For example for Sql Server:

 where Name like '%\[Car\]%' ESCAPE '\' or 
       Name like '%\[Truck\]%' ESCAPE '\' or 
       Name like '%\[Bike\]%' ESCAPE '\'
Sign up to request clarification or add additional context in comments.

Comments

1

I hope you will add wildcards in like operator else you can use IN operator. Try this.

Select @Count = SUM(cast(Value as int)) 
from tTag
where Name like '[Car]' 
   or Name like '[Truck]'
   or Name like '[Bike]'

Comments

0

If you're seeking exact matches, try IN (...):

 where Name in ('[Car]', '[Truck]', '[Bike]')

or

 where Name in ('Car', 'Truck', 'Bike')

depending on what you need.

Comments

0
Select @Count = SUM(cast(Value as int)) from tTag
 where Name like '[Car]' or Name like '[Truck]' or name like '[Bike]'

1 Comment

you'd better explain why he has to do that

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.