Reputation: 4401
How to write something like this in SQLite ? Like optional param if null don't use.
(@Year IS NULL OR @Year = DATEPART(year, Date)
Upvotes: 0
Views: 396
Reputation: 8994
You could do something like this:
IFNULL(@Year, DATEPART(year, Date)) = DATEPART(year, Date)
IFNULL returns its first non-null argument. In this case, the query will compare @Year
to DATEPART(year, Date)
as long as @Year
is not null. If it IS null, then it'll compare DATEPART(year, Date)
to DATEPART(year, Date)
, which will always be true.
Edit: Note that DATEPART
is not a native SQLite method (the OP referenced it so it may be a custom thing he/she is using). A native solution is to use strftime("%Y", Date)
(Thanks @Jason!)
Upvotes: 3