I have this code:
SELECT idcallhistory3, callid, starttime, answertime, endtime, duration,
is_answ, is_fail, is_compl, is_fromoutside, mediatype, from_no,
to_no, callerid, dialednumber, lastcallerid, lastdialednumber,
group_no, line_no
FROM "public".callhistory3
WHERE (starttime >= ?) AND (endtime <= ?) AND (is_fromoutside = ?)
AND (from_no = ?) AND (to_no = ?)
The problem is I need to pass one value for ? and get all the result without filter, some thing like *
Any help?
WHERE
(@start is null OR starttime >= @start) AND
(@end is null OR endtime <= @end) AND
(@fromOutside is null OR is_fromoutside = @fromOutside) AND
(@fromNo is null OR from_no = @fromNo) AND
(@toNo is null OR to_no = @toNo)
Pass nulls for all parameters (dang sql nulls; thanks GC).
I like COALESCE.
You just have to be careful with null values on the left hand side, if there can be nulls on the left hand side you can do something like the last line so that nulls will match. Typically with anything like this though you will want to make sure your query still performs ok.
SELECT idcallhistory3, callid, starttime, answertime, endtime, duration,
is_answ, is_fail, is_compl, is_fromoutside, mediatype, from_no,
to_no, callerid, dialednumber, lastcallerid, lastdialednumber,
group_no, line_no
FROM "public".callhistory3
WHERE (starttime >= COALESCE(@starttime, starttime ))
AND (endtime <= COALESCE(@endtime, endtime))
AND (is_fromoutside = COALESCE(@is_fromoutside, is_fromoutside))
AND (from_no = COALESCE(@from_no, from_no))
AND (COALESCE(to_no, -1) = COALESCE(@to_no, to_no, -1)) -- make nulls match
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With