Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
4.40/5 (3 votes)
See more:
I just like to know if it is possible to set a column to null if column doesn't exists.

I know that I can do something like:

SQL
Select
Column1,
NULL as Column2
FROM Table



But what I want to do is NULL the column only if column doesn't exists.
Posted
Comments
King Fisher 22-Apr-14 1:06am    
yes ,you can..

You can do something like this:
SQL
SELECT Column1,
        CASE WHEN exists (select null from information_schema.columns where column_name='Column2'and table_name='TableName') THEN Column2
        ELSE NULL END AS Column2
FROM TableName

EDIT:
The above query won't compile as the column name do not exist. You will need dynamic SQL to execute such queries. Below is a very basic syntax how you would do it.
SQL
DECLARE @query nvarchar(max) = '' + 'SELECT ' +
        (CASE WHEN EXISTS (SELECT NULL FROM information_schema.columns WHERE column_name='Column2' AND table_name='Table2')
            THEN 'Column2'
            ELSE 'NULL AS Column2' END) +
        ' FROM Table2'
--PRINT @query
EXEC sp_executesql @query
 
Share this answer
 
v2
Comments
krisedison 22-Apr-14 2:02am    
It still says
Invalid column name 'Column2'
on line with THEN Column2
Ankur\m/ 22-Apr-14 2:04am    
I tested it, it works. Please check you have specified correct table name.
krisedison 22-Apr-14 2:09am    
Yes it works if Column2 exists but if not it doesn't. I am now looking to use try and catch.
Ankur\m/ 22-Apr-14 2:13am    
It doesn't require try catch block. The query I have given checks if a column exists in a table, return that else return null. Check the below query and tell me what it returns
select null from information_schema.columns where column_name='Column2'and table_name='TableName'.
Replace Column2 and TableName with correct values. It will either return null or nothing.
It seems you may need to add schema [dbo.] to your table name.
krisedison 22-Apr-14 2:20am    
It says Invalid column name.
I didn't changed column_name because I am trying to look for Column2 in the table but I changed table_name value to the table that it will look for. But still, I have the invalid column error.
Your query will be......

SQL
Select Column1, ISNULL(Column2,NULL) AS Column2 FROM Table
 
Share this answer
 
Comments
VICK 22-Apr-14 6:33am    
ISNULL(Column2,NULL)

will check for the null value in Column2 and not if Column2 doest not exist. so off the topic. :(
Hi,

Use ISNULL function available in SQL.

SQL
Select
Column1,
ISNULL(Column2,NULL) AS 'Column2'
FROM Table
 
Share this answer
 
Comments
krisedison 22-Apr-14 1:00am    
It still says
Invalid column name 'Column2'
BTW, I am on SQL Server 2012
Bh@gyesh 22-Apr-14 1:31am    
ok, For that you need to check for every column exists of not in table.

SELECT CASE WHEN exists (SELECT * FROM sys.columns
WHERE [name] = N'column1' AND [object_id] = OBJECT_ID(N'table') THEN Column1 ELSE NULL END) AS 'Column1',
CASE WHEN exists (SELECT * FROM sys.columns
WHERE [name] = N'column2' AND [object_id] = OBJECT_ID(N'table') THEN Column2 ELSE NULL END) AS 'Column2'

FROM TableName
VICK 22-Apr-14 6:34am    
SNULL(Column2,NULL)

will check for the null value in Column2 and not if Column2 doest not exist. so off the topic. :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900