gopi nath pandraju
gopi nath pandraju

Reputation: 79

sql declare string and use in where clause

DECLARE @Symb varchar
SET @Symb = 'Lara'
SELECT     Names
FROM         Cricket
where Names like '@Symb%'

I need all the names starting with 'Lara'

Upvotes: 2

Views: 7008

Answers (4)

Darren
Darren

Reputation: 70718

DECLARE @Symb varchar(10)
SET @Symb = 'Lara'
SELECT     Names
FROM         Cricket
WHERE Names LIKE @Symb + '%'

Upvotes: 2

Elias Hossain
Elias Hossain

Reputation: 4469

DECLARE @Symb varchar(10)
SELECT @Symb = 'Lara' + '%'
SELECT     Names
  FROM         Cricket
  WHERE Names like @Symb

Upvotes: 1

Francis P
Francis P

Reputation: 13655

You could use the % when you set your variable :

SET @Symb = 'Lara%'

or use a CONCAT function to add your % symbol to your variable.

Else, that way you, you'll be looking for every names starting by '@Symb'

Upvotes: 1

Royi Namir
Royi Namir

Reputation: 148524

SELECT     Names
FROM         Cricket
where Symbol like @Symb+'%'

Upvotes: 3

Related Questions