Demonstrate NULLable column with a default

0

Please login or register to vote for this query.

(click on this box to dismiss)

Academia Meta

Q&A about the site for academics and those enrolled in higher education

-- Demonstrate NULLable column with a default

CREATE TABLE #temp (
    [Key] int IDENTITY(1, 1) NOT NULL
    ,[Value] varchar(1) DEFAULT ('Y') NULL
    ,[OtherValue] varchar(1) DEFAULT ('A') NOT NULL
) 

INSERT INTO #temp DEFAULT VALUES
INSERT INTO #temp (Value) VALUES (NULL)
INSERT INTO #temp (Value) VALUES ('Y')
INSERT INTO #temp (Value) VALUES ('N')
INSERT INTO #temp (Value, OtherValue) VALUES ('Y', 'B')
INSERT INTO #temp (Value, OtherValue) VALUES ('N', 'C')

SELECT [Key]
    ,Value
    ,CASE WHEN Value IS NULL THEN '<NULL>' ELSE '' END AS ValueIsNull
    ,OtherValue
FROM #temp

Enter Parameters

Options:
Switch to main site
loading Hold tight while we fetch your results