Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Swithch case help needed

Author  Topic 

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2011-10-01 : 22:27:32
Need help with the below query please

Table Name -Request_Detail
DDL of Request_Detail
CREATE TABLE [dbo].[Request_Detail](
[requestno] [numeric](18, 0) NOT NULL,
[detid] [int] NOT NULL,
[requireddt] [datetime] NULL,
[finisheddt] [datetime] NULL,
[eligibledt] [datetime] NULL,
[notes] [varchar](1000) NULL


Sample Data for a Request in the Request_Detail Table
requestno detid requireddt finisheddt eligibledt notes
123456 1 2011-01-01 1900-01-01 1900-01-01 testing sample
123456 2 1900-01-01 2011-01-01 1900-01-01
123456 3 1900-01-01 1900-01-01 2011-02-01 reporting need
123456 4 1900-01-01 1900-01-01 2011-02-01 reporting need
123456 5 1900-01-01 1900-01-01 2011-02-01 reporting need
123456 6 1900-01-01 1900-01-01 2011-02-01 reporting need
123456 7 1900-01-01 1900-01-01 2011-02-01 reporting need

From the above table I need to insert into another table called
Event_Log as in the below example

Requestno updateddt comments
123456 currentsystemdatetime item 1 saved as requireddt + notes
123456 currentsystemdatetime item 2 saved as requireddt + notes
123456 currentsystemdatetime item 3 saved as eligibleddt + notes
123456 currentsystemdatetime item 4 saved as eligibleddt + notes
123456 currentsystemdatetime item 5 saved as eligibleddt + notes
123456 currentsystemdatetime item 6 saved as eligibleddt + notes
123456 currentsystemdatetime item 7 saved as eligibleddt + Notes


Note:The comments in the event log should be populated based on all the three dates in the request_detail table based on which date has date other than 1900-01-01








visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-02 : 01:55:31
[code]
INSERT INTO Event_Log (Requestno, updateddt, comments)
SELECT Requestno,GETDATE(),'item ' + cast(detid as varchar(5)) + ' saved as' + convert(varchar(30),coalesce(nullif(requireddt,0),nullif(finisheddt,0),nullif(eligibledt,0)),121) + notes
FROM Request_Detail
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2011-10-02 : 20:24:41
Thank You
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-03 : 01:07:37
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -