0

I have the following query, can you please suggest me what is wrong with this, its never entering inside the IF block, even though data is present in a table.

 DECLARE @AccountNumber NVARCHAR(50) ='54654654'

 IF Exists( Select TOP 1 1 FROM [dbo].[CheckRecords] where DetailRecord like '% ' + @AccountNumber +' %')
 BEGIN
     Print 'Hello' + @AccountNumber 
 END
3
  • You can try DECLARE @AccountNumber NVARCHAR(50) ='% 54654654 %' Commented Jun 10, 2021 at 2:58
  • Infact I tried that , its still not entering inside IF Block Commented Jun 10, 2021 at 3:04
  • You need to show some sample data from the table because the logic is right... that is assuming your search is surrounded by spaces as shown? You can remove top 1 though because exists doesn't require a single record. Commented Jun 10, 2021 at 3:41

1 Answer 1

1
 DECLARE @AccountNumber NVARCHAR(50) ='54654654'
 IF Exists( Select TOP 1 1 FROM [dbo].[CheckRecords] 
 where DetailRecord like '%'  + @AccountNumber +'%')
     BEGIN
         Print 'Hello ' + @AccountNumber 
     END

try this it will work. it was not working because of extra space.

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

2 Comments

Thank you for the response, it worked but above query taking almost 50 seconds to complete , Can you please suggest me if there is any better way of writing same logic ?
replace of exists , you can use not null function

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.