Home > Software design >  Anything like template literals while writing a search query in SQL?
Anything like template literals while writing a search query in SQL?

Time:11-06

I am writing a stored procedure to get a particular value.

declare @num int

set @num = (SELECT Id
FROM [sometable]
WHERE Name like '%today%')

-- returns @num = 1

Select Value
  FROM [anothertable]
  where name like 'days1'

In the last line of the query I want to add "1" or any other number after 'days', depending on the variable @num.

How can I do it, sort of like how we use template literals in Javascript, using the ${} syntax but in SQL?

CodePudding user response:

You can just use the first query as a sub-query of the second:

select [Value]
from anothertable
where [name] = Concat('days', (select Id from sometable where [Name] like '%today%'));
  • Related