How to list static values in a query

Received an interesting question recently from a friend:

~~~

Currently this query does not work. Basically all I want is this query to return all of the data within the {} as one row for each.


Select * from
From {'Unknown date', '30 Days', '60 Days', '90 Days', '6 Months', '< 1 Year', '< 2 Years', '< 3 Years', '> 3 Years')

So my question is how can I fix this query to get this to work?

~~~

Probably the easiest way would be use of a look up table, insert these values into a table, then use a

select * from LookupTable

Option 1

However, without using a lookup table, you could create a temporary table that is created as part of a batch. Values get inserted into the temp table, then you can select from that temporary table.

-- Batch statement

DROP TABLE #T1
GO
CREATE TABLE #T1
(
    column_1 varchar(30)
);
GO
SET NOCOUNT ON;
INSERT INTO #T1 (column_1)
    VALUES ('Unknown date');
INSERT INTO #T1 (column_1)
    VALUES ('30 Days');
INSERT INTO #T1 (column_1)
    VALUES ('60 Days');
INSERT INTO #T1 (column_1)
    VALUES ('90 Days');

-- add any additional values
GO
SELECT column_1 FROM #T1;
GO

-- results

image

Option 2

A second method that would also accomplish the same thing without using a temporary table would be use of a UNION clause.

 

SELECT 'Unknown date' as [Date Range]
UNION
SELECT '30 Days' as [Date Range]
UNION
SELECT '60 Days' as [Date Range]
UNION
SELECT '90 Days' as [Date Range]
UNION
SELECT '6 Months' as [Date Range]

 

image
Published Sunday, November 30, 2008 11:01 AM by sthompson
Filed under:

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems