Link to home
Start Free TrialLog in
Avatar of desiredforsome
desiredforsome

asked on

SQL If NUll on Unpivot

So wondering if this sql code can have a statemet put in it that will do the following.

It runs the query as printed below but it checks to see if a specific feild is null.

So for instance this query currently does not have the field [appraisefinal]. I want it to while running this query do some if statements and say if appraisefinal is null then insert [appraisal ordered] into the item of outlookreport.dbo.calendar.

;WITH MyCTE AS
(
    SELECT    * 
    FROM      (
                  SELECT   *
                  FROM      emdb.dbo.calendarview 
              )p
    UNPIVOT 
    ( 
        EventDate FOR DateDescription in ([Appraisal Ordered],[Closing Date],[Application Signed],[Appraisal Recieved] ,[Approval To Close],[Insurance Ordered],[Title Ordered])
    ) as unpvt
)
insert into outlookreport.dbo.calendar (eventdate,item,xrefid)
SELECT    
          M.EventDate,
          M.DateDescription,
          T.xrefid
FROM      emdb.dbo.calendarview T 
          JOIN MyCTE M
              ON T.xrefid = M.xrefid
WHERE NOT EXISTS(SELECT 1 FROM outlookreport.dbo.calendar WHERE eventdate = M.EventDate AND xrefid = T.xrefid);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial