Solved

Convert Access Query into SQL Query

Posted on 2012-04-10
2
336 Views
Last Modified: 2012-05-22
Hi,
I have an access query. How do I convert it in SQl Server query
Access version:

SELECT tblProjectStage.ProjectStageID, tblProjectStage.StageTypeID, tblProjectStage.RevID, tblProjectStage.CEQRNUmber, tblProjectStage.StageDate, tblProjectStage.StageAge, tblProjectStage.StageEndDate, IIf([tblProjectStage]![StageEndDate] Is Null,DateDiff("d",[tblProjectStage]![StageDate],Date()),DateDiff("d",[tblProjectStage]![StageDate],[tblProjectStage]![StageEndDate])) AS ElapsedDay, tblTypeOfStages.StageTypeName
FROM (last_stage_qry INNER JOIN tblProjectStage ON last_stage_qry.MaxOfProjectStageID = tblProjectStage.ProjectStageID) INNER JOIN tblTypeOfStages ON tblProjectStage.StageTypeID = tblTypeOfStages.StageTypeID;

I have a problem converting the part below

IIf([tblProjectStage]![StageEndDate] Is Null,DateDiff("d",[tblProjectStage]![StageDate],Date()),DateDiff("d",[tblProjectStage]![StageDate],[tblProjectStage]![StageEndDate])) AS ElapsedDay,
0
Comment
Question by:maximyshka
2 Comments
 
LVL 3

Accepted Solution

by:
Chris__W earned 300 total points
Comment Utility
Hello,

Use a CASE function for the IIf, and GetDate() instead of Date().

CASE ([tblProjectStage].[StageEndDate] Is Null THEN DateDiff(day,[tblProjectStage].[StageDate],GetDate()) ELSE DateDiff(day,[tblProjectStage].[StageDate],[tblProjectStage].[StageEndDate])) AS ElapsedDay,

If any of those fields are not actually stored as the format datetime, replace table.field with CAST(table.field AS datetime).

You can also CAST the CASE above as a different data type:
CAST(CASE(...)) AS ElapsedDay

Does that cover what you are looking for?

Thanks,
Chris
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 200 total points
Comment Utility
This is how you do it:
SELECT  p.ProjectStageID,
        p.StageTypeID,
        p.RevID,
        p.CEQRNUmber,
        p.StageDate,
        p.StageAge,
        p.StageEndDate, 
--	IIf([tblProjectStage]![StageEndDate] Is Null,
--			DateDiff("d",[tblProjectStage]![StageDate],Date()),
--			DateDiff("d",[tblProjectStage]![StageDate],[tblProjectStage]![StageEndDate])) AS ElapsedDay, 
        CASE WHEN p.StageEndDate IS NULL
             THEN DATEDIFF(DAY, p.StageDate, GETDATE())
             ELSE DATEDIFF(DAY, p.StageDate, p.p.StageEndDate)
        END ElapsedDay,
        t.StageTypeName
FROM    last_stage_qry s
        INNER JOIN tblProjectStage p ON s.MaxOfProjectStageID = p.ProjectStageID
        INNER JOIN tblTypeOfStages t ON p.StageTypeID = t.StageTypeID ;

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 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