007
007

Reputation: 2186

T-SQL Derived Column in SELECT

SELECT
INT_VALUE = NULL -- Result Data Type = INT
,STR_VALUE = NULL -- Result Data Type = INT

I want to make sure that STR_VALUE is a varchar NULL and not int NULL. I need this to be done in SELECT statement and in derived column.

How can I achieve that?

Thank you

UPDATE: Thanks guys for really quick feedback. just "fyi" kind of thing, I am trying to create a lookup query for an ETL app and wanted to see if I could create NULL string column in t-sql rather than creating an ETL app based NULL string derived column.

Thx

Upvotes: 1

Views: 441

Answers (3)

suff trek
suff trek

Reputation: 39777

If you're creating a table based on your SELECT (e.g. SELECT INTO) you can also use cast/convert:

SELECT NULL as INT_VALUE, CAST(NULL as Varchar(10)) AS STR_VALUE
INTO MyNewTable

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269753

NULL defaults to an int. You can change it to any other type using cast():

SELECT INT_VALUE = NULL, -- Result Data Type = INT
       STR_VALUE = cast(NULL as varchar(255)) -- Result Data Type = varchar(255)

Upvotes: 3

D Stanley
D Stanley

Reputation: 152556

Just CAST it:

SELECT
INT_VALUE = CAST(NULL AS int)
,STR_VALUE = CAST(NULL as VARCHAR(10)) 

Upvotes: 1

Related Questions