473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to subtract two DATETIME values

I'm having trouble understanding the MySQL docs on how to subtract two DATETIME values. I have two tables that have a DATETIME column. In my SELECT I am doing a JOIN to bring in both sets of rows. What I want is to subtract the DATETIME values to determine the number of seconds between their time. In the WHERE clause I also want to filter the selected rows if the difference in the DATETIME values is less that 900 (seconds).

Any help?
Mar 19 '07 #1
2 20458
bartonc
6,596 Expert 4TB
I'm having trouble understanding the MySQL docs on how to subtract two DATETIME values. I have two tables that have a DATETIME column. In my SELECT I am doing a JOIN to bring in both sets of rows. What I want is to subtract the DATETIME values to determine the number of seconds between their time. In the WHERE clause I also want to filter the selected rows if the difference in the DATETIME values is less that 900 (seconds).

Any help?
I found one way to make dates subtractable:
Expand|Select|Wrap|Line Numbers
  1. SELECT unix_timestamp(now()) - unix_timestamp('2007-03-19 09:50:00');
Mar 19 '07 #2
code green
1,726 Expert 1GB
You need something like this

To subtract the two DATETIME fields use
TIMESTAMPDIFF(SECOND,field1,field2)

To calculate if you have 900 seconds use
SELECT `columns` FROM `table`
WHERE DATE_SUB(value INTERVAL 900 SECOND)

Put together
SELECT `columns` FROM `table`
WHERE DATE_SUB((TIMESTAMPDIFF(SECOND,field1,field2) INTERVAL 900 SECOND)

Not tested this but it looks on the right lines
Mar 20 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jimbo | last post by:
Anyone know how I can subtract 12 months from the current date?
4
by: Lee | last post by:
Hi All, I have a datetime field and want to subtract 24 (or any other number of) hours from it. How would I go about this? Thanks Lee
5
by: Sandy | last post by:
Hello I need to subtract a pm time from an am time, e.g. 10:00 a.m. from 1:00 p.m. to get 3 I have two textboxes for the time values and next to each is a dropdownlist to click indicating a.m....
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
2
by: Simon Gare | last post by:
Hi all, need to subtract one date from another to show how long a user was logger in for. i.e. <%=(rsDriverLogin.Fields.Item("OFF_DATE").Value)%> minus...
4
by: Manikandan | last post by:
Hi, I'm inserting a datetime values into sql server 2000 from c# SQL server table details Table name:date_test columnname datatype No int date_t DateTime ...
2
by: Mike | last post by:
Hi, I have DateTime filed in db table. I'd like to search my db based on DateTime filed, actually to list all rows with particular date, for e.g. 16.11.2007... I send string to method for....
2
by: barronmo | last post by:
I'm trying to get the difference in dates using the time module rather than datetime because I need to use strptime() to convert a date and then find out how many weeks and days until that date. ...
2
by: JonDavid | last post by:
CODE: DateTime coursestatusdate = DateTime.MinValue; if (courseRow.ToString() != string.Empty) { coursestatusdate = DateTime.Parse(courseRow ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.