Question: I tried to retrieve data from the table using a temp variable. The temp variable returns correct data but when tried to use it in Query it is not returning correct data set.
I have tried retrieving data using hard coded temp value in the query, it works fine. Can anyone help me out to find the issue here.?

Below is my code I have tried

```
Declare @tempwordFinal varchar(50)                                     
select @tempwordFinal = ''''+'%'+'The Big Bang'+'%'+''''               
select @tempwordFinal --here the output is - '%The Big Bang%'

SELECT * from MasterProgram where ProgramTitle like @tempwordFinal --not working  
SELECT * from MasterProgram where ProgramTitle like '%The Big Bang%' -- working

```

Answer: 
Because `@tempwordFinal` variable has single quotes at the start and end. So it expects the data in `ProgramTitle` column to have single quotes at the start and end. Except the wildcards whatever present inside the variable will be considered as data that's why it is failing.

```
select @tempwordFinal --here the output is - '%The Big Bang%'
                                             ^              ^

```

Try this way

```
Declare @tempwordFinal varchar(50)                                     
select @tempwordFinal = '%The Big Bang%'

select 1 where 'The Big Bang' like @tempwordFinal 

```

When you use variables for `varchar` datatype we don't need single quotes. Single quotes are required only when you hard code the string constants
The problem was with your (escaped) extra apostrophes. Like this `''''`. You don't need those unless you are looking for words in the database which explicitly contain apostrophes. The corrected code looks like this:

```
Declare @tempwordFinal As Varchar(50);

Set @tempwordFinal = '%The Big Bang%';

Select @tempwordFinal;  -- %The Big Bang%

SELECT * from MasterProgram where ProgramTitle like @tempwordFinal;

```