DateDiff in SQL Query using Table/Field and CurrentDate

I need to subtract a field in my database that is a datetime field from the current date at the time the report is run.
Then I need the answer displayed in days, hours and minutes as shown here:
11d 21h 56m

This is a one time report so I don't need to use declared variables, etc., just something simple will do.
I have seen lots of examples using variables but do not know enough sequel to turn those into a simple query.

Can someone help?

My table/field is op.order_date.
Becky EdwardsEpic Clarity DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
Not sure how to do it in SQL.

Here is a formula to do it.
WhilePrintingRecords;
Local NumberVar myMinutes;
Local NumberVar  myHours;
Local NumberVar myDays;

myMinutes:= DateDiff('n',{op.order_date},CurrentDateTime);
myDays := myMinutes \ 1440;
myMinutes := myMinutes  MOD 1440;
myHours := myMinutes  \ 60;
myMinutes  := myMinutes  MOD 60

CStr(myDays,0) &  'd ' & CStr(myHours,0) & 'h ' & CStr(myMinutes,0) & 'm'

Open in new window


mlmcc
0
Brendt HessSenior DBACommented:
This is sample code that works for me. You may need to modify it to integrate it into your SQL.

SELECT 
	(mins / 60 - ((mins / 60) % 24))/24 as days,
	(Mins / 60) % 24 as hrs,
	hrMins as mins
FROM (
	SELECT mins%60 as hrMins,
		mins - (mins%60) as mins
	FROM (
		SELECT datediff(minute, op.order_date, current_timestamp) as mins
		) as minStep
	) midStep

Open in new window

0
Becky EdwardsEpic Clarity DeveloperAuthor Commented:
mlmcc:  When I try to place the ode above into a formula in Crystal, it comes back with an error message that says the last line is not part of a formula.  

bhess1:
I have no idea how to incorporate those subqueries into my sequel query.  My query is attached.  Perhaps you can give me some idea?
ECG-Orders.sql
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Becky EdwardsEpic Clarity DeveloperAuthor Commented:
This is actually what worked.  The comments were helpful.

select distinct right('0' + cast(datediff(DAY, op.ORDERING_DATE, current_timestamp) as varchar),2)

+ ':' +

right('0' + cast(datediff(hh, op.ORDERING_DATE, current_timestamp) as varchar),2)

+ ':' +

right('0' + cast(datediff(mi, op.ORDERING_DATE, current_timestamp) % 60 as varchar),2) as duration

FROM Order_Proc op

WHERE op.ORDERING_DATE = '08/20/2015'
0
Becky EdwardsEpic Clarity DeveloperAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for bjrhart's comment #a40945058
Assisted answer: 100 points for mlmcc's comment #a40944930
Assisted answer: 200 points for bhess1's comment #a40944994

for the following reason:

Neither comment was exactly what I needed, but your comments helped me to find the correct solution.  Thank you.
0
Mark WillsTopic AdvisorCommented:
Was going to post... but did a refresh before pasting and found the close pending

select left(datepart(day,(getdate() - datefld)),3)+'d '+ left(datepart(hour,(getdate() - datefld)),2)+'h '+ left(datepart(minute,(getdate() - datefld)),2)+'m'
from Your_Date_Data

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark WillsTopic AdvisorCommented:
Oh, and if you have 2012 and .Net framework installed (and who doesn't these days)

then use the FORMAT command (finally in T-SQL)

SELECT FORMAT( (getdate() - datefld), 'dd\d HH\h mm\m', 'en-US' ) AS DayHourMin
from Your_Date_Data

Open in new window

0
mlmccCommented:
Missing a ;

WhilePrintingRecords;
Local NumberVar myMinutes;
Local NumberVar  myHours;
Local NumberVar myDays;

myMinutes:= DateDiff('n',{op.order_date},CurrentDateTime);
myDays := myMinutes \ 1440;
myMinutes := myMinutes  MOD 1440;
myHours := myMinutes  \ 60;
myMinutes  := myMinutes  MOD 60;

CStr(myDays,0) &  'd ' & CStr(myHours,0) & 'h ' & CStr(myMinutes,0) & 'm'

Open in new window

2
PortletPaulfreelancerCommented:
but the formula used is not correct....

It can produce weird results like 25 hours (an actual result shown below)
|                          | duration |
|--------------------------|----------|
| August, 25 2015 05:58:44 | 05:25:58 |

Open in new window


  CREATE TABLE Order_Proc 
        ([ORDERING_DATE] datetime)
    ;
        
    INSERT INTO Order_Proc 
        ([ORDERING_DATE])
    VALUES
        ('2015-08-20 00:00:00')
    ;
    
**Query 1**:

    select
    current_timestamp
    , right('0' + cast(datediff(DAY, op.ORDERING_DATE, current_timestamp) as varchar),2)
    
    + ':' +
    
    right('0' + cast(datediff(hh, op.ORDERING_DATE, current_timestamp) as varchar),2)
    
    + ':' +
    
    right('0' + cast(datediff(mi, op.ORDERING_DATE, current_timestamp) % 60 as varchar),2) as duration
    
    FROM Order_Proc op
    
    WHERE op.ORDERING_DATE = '08/20/2015'

**[Results][2]**:
    |                          | duration |
    |--------------------------|----------|
    | August, 25 2015 05:58:44 | 05:25:58 |

  [1]: http://sqlfiddle.com/#!3/cc13c/2

Open in new window

0
Becky EdwardsEpic Clarity DeveloperAuthor Commented:
This one worked perfectly!  Thank You.
0
Becky EdwardsEpic Clarity DeveloperAuthor Commented:
And thank you mlmcc I am also able to use yours now in a crystal report!
0
Becky EdwardsEpic Clarity DeveloperAuthor Commented:
And thank you Paul for letting me know my solution would not be correct to use.....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Crystal Reports

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.