Skip to content
Advertisement

What do the quotes mean in this SQL Server snippet? (SELECT ” column1, ” column 2)

I’d like to know the purpose of the two single quotes in front of each column. This is in a stored procedure. Thank you for your help!

INSERT INTO #temptable1
SELECT DISTINCT '' column1
    ,column2
    ,'' column3
    ,'' column4     
FROM table1
WHERE column1 NOT LIKE 'string1%'
    AND column2 <> 'string2'
    AND column3 <> 'string3'

Advertisement

Answer

The expression '' is an empty string. So this is using an empty string instead of NULL for “missing” values.

I would write this code as:

SELECT DISTINCT '' as column1, column2, '' as column3, '' as column4

This might make it clearer. I always use as for column aliases — so it is easier to spot missing commas (there should always be an as for a column alias).

More commonly, this would probably just use default values (which are usually NULL but could be ''):

INSERT INTO #temptable1 (column2)
    SELECT DISTINCT column2
    FROM table1
    WHERE column1 NOT LIKE 'string1%'
        AND column2 <> 'string2'
        AND column3 <> 'string3';
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement