Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace returned null values in LEFT OUTER JOIN

SELECT        WO_BreakerRail.ID, indRailType.RailType, indRailType.RailCode, WO_BreakerRail.CreatedPieces, WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop, WO_BreakerRail.Date
FROM            indRailType LEFT OUTER JOIN
                         WO_BreakerRail ON indRailType.RailCode = WO_BreakerRail.RailCode AND WO_BreakerRail.Date = @date

When this returns, there are NULL values in the Date column where there are no matching rows from WO_BreakerRail. Is there a way to replace just those NULL values with @date?

like image 616
Wesley Avatar asked Mar 24 '10 11:03

Wesley


2 Answers

In oracle and sql server, you can use the COALESCE (oracle version) function

SELECT ...., COALESCE(WO_BreakerRail.Date, @date)
like image 159
thecoop Avatar answered Sep 18 '22 00:09

thecoop


You can use COALESCE function (go here for mysql documentation)

COALESCE(WO_BreakerRail.Date, @date)

or you can use a simple IF:

IF(WO_BreakerRail.Date IS NULL, @date, WO_BreakerRail.Date)
like image 37
Nicolò Martini Avatar answered Sep 21 '22 00:09

Nicolò Martini