Query to check whether a column is nullable

Sql ServerSql Server-2008Nullable

Sql Server Problem Overview


Query to check whether a column is nullable (null values are allowed in the column or not). It should preferably return yes/no or 1/0 or true/false.

Sql Server Solutions


Solution 1 - Sql Server

You could retrieve that from sys.columns:

select  is_nullable 
from    sys.columns 
where   object_id = object_id('Schema.TheTable') 
        and name = 'TheColumn'

Solution 2 - Sql Server

You could also use the COLUMNPROPERTY and OBJECT_ID metadata functions:

SELECT COLUMNPROPERTY(OBJECT_ID('SchemaName.TableName', 'U'), 'ColumnName', 'AllowsNull');

Solution 3 - Sql Server

You can also check all columns in a table for 'nullable' property or any other property that you want, for example table named Bank.Table we need to query

column name, data type, Character Max Length, is nullable

Use SQL Information_Schema like this example:

SELECT 
COL.COLUMN_NAME, 
COL.DATA_TYPE, 
COL.CHARACTER_MAXIMUM_LENGTH, 
COL.IS_NULLABLE 
FROM INFORMATION_SCHEMA.COLUMNS COL 
WHERE COL.TABLE_NAME = 'Bank'

The result should be like this:

Query Results

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser646093View Question on Stackoverflow
Solution 1 - Sql ServerAndomarView Answer on Stackoverflow
Solution 2 - Sql Serveruser440595View Answer on Stackoverflow
Solution 3 - Sql ServerAshraf SadaView Answer on Stackoverflow