Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select multiple static values

Tags:

sql-server

As we can write in SQL to return single static value

select "Jan" as StartMonth

In the similar way, SQL should allow to write to return multiple values in same column

select {"Jan", "Feb", "Mar"} as Qtr1, {"Apr", "May", "Jun"} as Qtr2

When we need data as table to perform join and data is unknown at Sp level, i.e., it is coming from parameter, it appears unnecessary tasks to create a temp table and insert split values in the temp table. Correct me if such feature exist in MS SQL server. Or better way to have it other than pivot and temp table.

like image 386
hungryMind Avatar asked Jun 03 '11 10:06

hungryMind


People also ask

How do I set a static value in a select query?

Selecting Static Values Static values can be inserted into a resultset returned from a SELECT query as another column. Simply use the static value as a column to select, and the query will return a column where the name of the column is the static value, and every row in that column will return that same static value.

How do I select multiple values in SQL?

To select multiple values, you can use where clause with OR and IN operator.

How do I select multiple values in one column in SQL?

Note – Use of IN for matching multiple values i.e. TOYOTA and HONDA in the same column i.e. COMPANY. Syntax: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN (MATCHING_VALUE1,MATCHING_VALUE2);


1 Answers

Will this give you what you want?

SELECT 'Jan' AS Qtr1, 'Apr' AS Qtr2
UNION ALL SELECT 'Feb' AS Qtr1, 'May' AS Qtr2
UNION ALL SELECT 'Mar' AS Qtr1, 'Jun' AS Qtr2
like image 144
Tim Rogers Avatar answered Sep 19 '22 19:09

Tim Rogers