View Full Version : This is odd...using an iif with the datepart function


Myriad_Rocker
01-19-2007, 10:43 AM
Here's the deal, I have a table that has a flag in it. It's either a 1 or a 0.

Expr1: IIf([Is it?]=1,[REQ DATE],IIf([Is it?]=0,(DatePart("yyyy",[REQ DATE])),"ERROR"))

If it's a 1, it displays the full date. If it's zero, it's supposed to display the year of the date.

The first part works fine. If it's a 1, it shows the date. However, if it's a 0...it shows a date not even remotely close...and they're all the same. 06/28/1905.

pbaldy
01-19-2007, 11:46 AM
It's a formatting issue. 06/28/1905 is the formatted date represented by the number 2006.

Moniker
01-19-2007, 04:26 PM
In addition to using the FORMAT function, for clarity, use a SWITCH function there:

Expr1:
SWITCH([Is it?]=1,[REQ DATE]
,[Is it?]=0,Format([REQ DATE],"yyyy")
,TRUE, "ERROR")

Note that this is assuming that [REQ DATE] is already formatted as a date (not text or something else).