DATEDIFF() Parameter Issue - mysql

The old DATEDIFF() allowed users to use 3 parameters, and I was trying to do this so I could get hours out of my DATEDIFF rather than days, (I'm trying to show hours since a post). In my database I'm using a TIMESTAMP and this line of code to pull a value, and obviously it doesn't work because I have the extra parameter. Once I remove the 'hour' or 'hh' the query runs and returns a value in days.
SELECT DATEDIFF(hour, CURDATE(), (SELECT Post_Date FROM Post_T WHERE pk_PostID = 1) )
Is there an easy way I can return the hourly value?
Also I'm using MYSQL Version 5.5.20.

Like it says in the documentation:
DATEDIFF(expr1,expr2)
DATEDIFF() returns expr1 – expr2 expressed as a value in days from one
date to the other. expr1 and expr2 are date or date-and-time
expressions. Only the date parts of the values are used in the
calculation.
If you want the result in hours you should use Timestampdiff
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
Returns datetime_expr2 – datetime_expr1, where datetime_expr1 and
datetime_expr2 are date or datetime expressions. One expression may be
a date and the other a datetime; a date value is treated as a datetime
having the time part '00:00:00' where necessary. The unit for the
result (an integer) is given by the unit argument.
The unit argument can be: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.
In your case you can do:
SELECT TIMESTAMPDIFF(hour, CURDATE(), (SELECT Post_Date FROM Post_T WHERE pk_PostID = 1) )

Related

Eliminating the computation of the same value twice in a single SELECT statement

I need to modify the output of a datetime field so that it returns the first day of the week in which that date falls. For example, if the date is 9/2/2016, it should return 8/29/2016 because that's the Monday of that week (I want Monday, not Sunday). However, I also need to convert the timestamp to a different timezone. The result is that I end up having to convert the timezone twice:
CONVERT_TZ(timestamp, '+00:00', '+05:00') - INTERVAL WEEKDAY(CONVERT_TZ(timestamp, '+00:00', '+05:00')) day
I can't simply perform the INTERVAL calculation on the UTC datetime and then convert the timezone on the result because the timezone conversion may affect the result of the WEEKDAY function, e.g. a datetime of 2016-9-5 00:00 UTC will actually fall on 9/4 for EST, thus causing it to be part of a different week.
Is there a way to avoid having to make two calls to CONVERT_TZ in the SELECT statement?
You can put it into a subquery.
SELECT converted - INTERVAL WEEKDAY(converted) AS day
FROM (SELECT CONVERT_TZ(timestamp, '+00:00', '+05:00') AS converted
FROM yourTable) AS x
Another way is to assign a user variable. To be able to use it twice, put the assignment into the condition part of an IF() expression -- that ensures that the assignment will be done before the uses.
IF(#converted := CONVERT_TZ(timestamp, '+00:00', '+05:00'),
#converted - INTERVAL(WEEKDAY(#converted) DAY,
NULL) AS day

day difference between today's date and a specified date in SQL

How to find the day difference between today's date and a specified date in SQL. The specified date column(P.SubscrpEndDate__c) is in Date time format(12/1/2014 12:00 Am). I have used the below query but it do not works
DATEDIFF(day,GETDATE(), P.SubscrpEndDate__c) AS 'SubscriptionDueDate'
In MySQL, DATEDIFF function, takes only two arguments: end date and start date:
DATEDIFF(NOW(), P.SubscrpEndDate__c) AS 'SubscriptionDueDate'
According to the manual:
DATEDIFF(expr1, expr2) returns expr1 − expr2 expressed as a value in days from
one date to the other.
Also, to get the current date and time you have to use NOW() instead of GETDATE.
It should work in SQL Server. I assume date column(SubscrpEndDate__c) in your table would contain lower values than current date, so simply you can use query below. I've just swapped second & third parameters to get positive difference in days. You can also use ABS() function to ignore negative difference.
SELECT DATEDIFF(day,P.SubscrpEndDate__c,GETDATE() ) AS 'SubscriptionDueDate'
This would work, kindly check
SELECT DATEDIFF(NOW(),P.SubscrpEndDate__c) AS SubscriptionDueDate FROM xyz_table WHERE id='123456'

Reverse of DATE_ADD / INTERVAL in MySQL

In mysql, we can do the following for adding months:
SELECT DATE_ADD('2014-08-20', INTERVAL 13 MONTH); //Result: 2015-09-20
Is there any way to do the reversed operation ? Example:
SELECT DIFF_IN_MONTHS('2015-09-20', '2014-08-20') //Result: 13
Roundings due to day differences are not a problem for me.
The function TIMESTAMPDIFF does this:
SELECT TIMESTAMPDIFF(MONTH, '2015-09-20', '2014-08-20');
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
Returns datetime_expr2 – datetime_expr1, where datetime_expr1 and
datetime_expr2 are date or datetime expressions. One expression may be
a date and the other a datetime; a date value is treated as a datetime
having the time part '00:00:00' where necessary. The unit for the
result (an integer) is given by the unit argument
So you are wanting to get the difference in days between the 2 dates?
DATEDIFF('1015-09-20','2014-08-20')

MySQL - Truncating Date/Time and Subtracting from Each Other

I have the following query which subtracts the original purchase time from the expires time to come up with a number of days:
select date(expires_dtime) - date(original_purchase_dtime)
from sl_player_subscription
where player_uuid = '9c61411c-54b4-45bd-8d07-264b1c2e4249'
player_uuid that = '9c61411c-54b4-45bd-8d07-264b1c2e4249' has an original purchase time of 2013-03-06 11:36:46 and an expire time of 2013-04-05 12:36:46.
I want the output to read "30" which is the number of days between the purchase time and the expire time, but instead the output is 99. Any suggestions?
You can use DATEDIFF:
SELECT DATEDIFF('2013-04-05 12:36:46', '2013-03-06 11:36:46')
Result
30
Using your columns:
SELECT DATEDIFF(expires_dtime, original_purchase_dtime)
See the demo
DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
Documentation: Date and Time functions

Mysql datediff for datetime types

I was wondering which of the following is the preferred way to use datediff for datetime types.
datediff(current_date, <datetime type>)
datediff(current_date, date(<datetime type>))
datediff(now(), <datetime type>)
I tried them out on a few cases and they all returned the same answer but I'm worried that they may produce some error down the line.
Is datediff the correct way to find the number of days between two datetime types?
Given:
datediff internally casts parameters to date
now() cast to a date is the same as current_date
There should be no difference in the result between any of those combinations
For your purposes, they are all equivalent.
current_date returns a DATE type, while now() returns DATETIME
This doesn't matter in this case, though, because datediff() ignores the time part of a DATETIME, so using DATE and DATETIME with datediff() are completely equivalent.