Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Convert Access Query into SQL Query

Posted on 2012-04-10
Last Modified: 2012-05-22
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,
Question by:maximyshka

Accepted Solution

Chris__W earned 300 total points
ID: 37829560

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?

LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 200 total points
ID: 37830613
This is how you do it:
SELECT  p.ProjectStageID,
--	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,
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


Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

838 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