我在表中有一个名为DayShift
的列,其数据类型为Yes/No
(布尔值)。我想要的结果是:如果值为true,则显示“Day”,否则显示night。
我尝试了以下内容:
SELECT iif(DayShift=Yes,"Day","Night") as Shift FROM table1;
SELECT iif(DayShift,"Day","Night") as Shift FROM table1;
SELECT iif(DayShift=True,"Day","Night") as Shift FROM table1;
SELECT iif(DayShift=1,"Day","Night") as Shift FROM table1;
但上述工作都没有。它只是在输出数据表窗口中给出了一个空白复选框列表。我正在使用Access 2003.任何帮助表示赞赏。
更新
经过一番研究后,Access 2003中的yes / no数据类型无法正确处理空值。因此,错误。 Check this link for details.
更新2
使用连接进行实际查询。没有提及它,因为我虽然上面提供的信息可行。
SELECT tblovertime.contfirstname AS [First Name],
tblovertime.contlastname AS [Last Name],
tblovertime.employeenumber AS [Employee Number],
tblsignup.thedate AS [Sign Up Date],
Iif([tblOvertime.DayShift] =- 1, "Day", "Night") AS shift,
(SELECT Mid(MIN(Iif(sector = 1, "," & sector, NULL)) & MIN(
Iif(sector = 2, "," & sector, NULL)) & MIN(
Iif(sector = 3, "," & sector, NULL)) & MIN(
Iif(sector = 4, "," & sector, NULL)), 2) AS concat
FROM tblempsectorlist
WHERE tblempsectorlist.empnum = tblsignup.employeenumber
GROUP BY empnum) AS sectors,
tblovertime.timedatecontact AS [Date Contacted],
tblovertimestatus.name AS status
FROM (tblsignup
INNER JOIN tblovertime
ON ( tblsignup.thedate = tblovertime.otdate )
AND ( tblsignup.employeenumber = tblovertime.employeenumber ))
INNER JOIN tblovertimestatus
ON Clng(tblovertime.statusid) = tblovertimestatus.statusid
WHERE (( ( tblsignup.thedate ) ># 1 / 1 / 2011 # ))
ORDER BY tblsignup.thedate;
答案 0 :(得分:3)
你的第二个是正确的
SELECT iif(DayShift,"Day","Night") as Shift FROM table1;
我建议尝试以下方法来查看实际评估的内容
SELECT iif(DayShift,"Day","Night") as Shift, DayShift FROM table1;
你也可以这样做
SELECT iif(DayShift = -1,"Day","Night") as Shift FROM table1;
由于MS Access将true存储为-1,将false存储为0(它不像true那样直观= 1,但在二元赞美中评估可能更快)
- 编辑 -
由于您似乎正在使用连接,可以导致Nul的是/否的连接,请使用nz()函数。
select iff(nz(DayShift, 0), "Day","Night") as Shift FROM table1;
当DayShift出现null时,结果会返回0(false / no)。
答案 1 :(得分:1)
这个可能是愚蠢的,但是......
如果DayShift字段中有一些Null值,Access将无法评估公式。你可以用这种方式编写测试:
iif(Nz(DayShift,0)=-1,"Day","Night")
答案 2 :(得分:0)
我成功运行了以下查询:
SELECT IIf([Table1]![isDayShift]=True,"Day","Night") AS Shift
FROM Table1;
我在Access 2007中构建了这个(对不起,我没有2003年的副本)。我看到的唯一区别是它给了字段的完整路径而不仅仅是字段名称。但是,这应该不是问题。如果它给你复选框,它似乎没有看到这个字段作为文本字段,而是作为复选框字段。
底线是上面的查询应该有效。
答案 3 :(得分:0)
你绑定到“DayShift”而不是表格中派生的“Shift”。
第三个查询(=True
)显示何时单独运行应显示日/夜。
答案 4 :(得分:0)
Access数据库引擎(ACE,Jet,等等)有一个聪明/愚蠢的功能,允许SELECT
子句中的表达式引用AS
子句(“列别名”)如果SELECT
子句位于表达式的左侧,则为AS
子句。这使得使用测试数据测试表达式变得容易,而根本不使用表格(假设ANSI-92 Query Mode,我认为),例如尝试单独执行以下任何语句:所有语句都应该起作用并产生预期结果:
SELECT CBOOL(TRUE) AS DayShift,
IIF(DayShift = TRUE, 'Day', 'Night') AS Shift;
SELECT CBOOL(FALSE) AS DayShift,
IIF(DayShift = TRUE, 'Day', 'Night') AS Shift;
SELECT NULL AS DayShift,
IIF(DayShift = TRUE, 'Day', 'Night') AS Shift;
SELECT CBOOL(TRUE) AS DayShift,
IIF(DayShift, 'Day', 'Night') AS Shift;
SELECT CBOOL(FALSE) AS DayShift,
IIF(DayShift, 'Day', 'Night') AS Shift;
SELECT NULL AS DayShift,
IIF(DayShift, 'Day', 'Night') AS Shift;