Set a static column value in a SQL Server query


I needed a quick way to put a default value in a select query for MS SQL Server 2000. Sure there are plenty of ways I can do it programmatically after I have the resultset, but there wasn’t going to be any intermediate step. I was querying directly to an export file so I needed to have everything in one place. There were some columnar values that were required but we didn’t have any good data to put into the column. Here is what I did, ‘MyDefaultValue’ would be the static column value, in case you were wondering:

DECLARE @x CHAR(1)
SET @x=1
 
SELECT
        myColumn1,
        myColumn2,
        CASE @x WHEN 1 THEN 'MyDefaultValue' END AS [myColumn3]
FROM 
        myDatabase

Comments are closed.