The easiest way to handle multiple hardcoded values ​​in SQL?

How do I do this in SQL Server? (I know that this will not work as it is written, but this illustrates the question better than I can explain)

SELECT SQRT(number) WHERE number IN (4,9,16,25) 

It will return several lines of the course.

+4
source share
2 answers

you can use table value constructor

 select sqrt(number) from ( values (4),(9),(16),(25) ) as T(number) 

or use the union of all

 select sqrt(number) from ( select 4 union all select 9 union all select 16 union all select 25 ) as T(number) 

demo sql

+14
source

You can create a view:

 SELECT SQRT(number) FROM ( SELECT 4 AS number UNION ALL SELECT 9 UNION ALL SELECT 16 UNION ALL SELECT 25 ) A 
0
source

Source: https://habr.com/ru/post/1502459/


All Articles