Solved

SQL If NUll on Unpivot

Posted on 2014-12-19
1
79 Views
Last Modified: 2015-01-15
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

0
Comment
Question by:desiredforsome
1 Comment
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40510753
hmm..

 
WITH Data AS 
	(
		SELECT	[Appraisal Ordered],
				[Closing Date],
				[Application Signed],
				[Appraisal Recieved] ,
				[Approval To Close],
				[Insurance Ordered],
				[Title Ordered],
				COALESCE(appraisefinal, [Appraisal Ordered]) AS appraisefinal
		FROM	emdb.dbo.calendarview 
	)
SELECT	* 
FROM	Data
UNPIVOT ( EventDate FOR DateDescription IN ( appraisefinal, [Appraisal Ordered], [Closing Date], [Application Signed], [Appraisal Recieved], [Approval To Close], [Insurance Ordered], [Title Ordered] ) ) U;

Open in new window

0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now