如何确保select语句返回特定列的空值?

时间:2022-01-24 04:30:28

This might sound kind of weird, but I have a query that joins two tables. I'm using an IF statements that dictates what to return. One path runs the query/join as is, the other needs to return all of the data from the first column, but only return column names with null values. Here's the query i have now:

这可能听起来有点奇怪,但是我有一个连接两个表的查询。我使用IF语句来指定返回的内容。一个路径运行查询/连接,另一个需要返回第一列的所有数据,但只返回带有空值的列名称。下面是我现在的问题:

declare @Date DATE = '06/07/2012'
IF @DATE >= GETDATE()    
    BEGIN
        SELECT DisplayName, '' [RegularHours], ''[OvertimeHours] 
        FROM Sites
        ORDER BY DisplayName
    END
    ELSE 
        SELECT sites.DisplayName, hrs.SiteDate, hrs.RegularHrs, hrs.OverTimeHrs
        FROM Sites sites
        left join SiteHours hrs on sites.SiteID = hrs.SiteID
        ORDER BY DisplayName

What's making me nervous is that the second and third columns do not have values at all, not even NULL. I'm worried that this will pose a problem later. Any ideas?

让我紧张的是第二和第三列根本没有值,甚至都不是空的。我担心这以后会造成问题。什么好主意吗?

1 个解决方案

#1


5  

If I understand the question correctly, I think you can do:

如果我理解正确,我认为你可以做到:

SELECT DisplayName, NULL as 'RegularHours', NULL as 'OvertimeHours'

#1


5  

If I understand the question correctly, I think you can do:

如果我理解正确,我认为你可以做到:

SELECT DisplayName, NULL as 'RegularHours', NULL as 'OvertimeHours'