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'
Related
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) )
I've got two columns (both datetime) startDate and endDate in an events table.
I am retrieving the current day using the the date() function of php.
This results in for example 2013-03-12.
Now there are three possibilities of events combined with dates that occur today:
An event starts and also end on this day
An event has started earlier and ends today
An event starts today but ends in the future (>= 2013-03-13)
Now I'd like to break these all into separate queries as I'm not used to work with dates. I started with the first query, but I am already failing on that one. I've tried the following:
SELECT * FROM events WHERE (startDate= '2013-03-12' AND endDate= '2013-03-12')
aswell as:
SELECT * FROM events WHERE NOT (startDate < '2013-03-12' OR endDate > '2013-03-12')
I've tried to use DATE() aswell and to format dates like '2013-03-12%'.
I don't know why it doesn't work while i am sure there is at least 1 event that is taking place on the 12th. Any help is appreciated.
Try using the MySQL's DATE() function to trim the date columns to the just the date parts:
SELECT *
FROM events
WHERE (DATE(startDate) = '2013-03-12' AND DATE(endDate)= '2013-03-12')
You can use the DATE() function as other answers suggested, but I think this makes it hard to use an index on the columns. Instead, you can include times in your comparisons:
SELECT *
FROM events
WHERE startDate BETWEEN '2013-03-12 00:00:00' AND '2013-03-12 23:59:59'
AND endDate BETWEEN '2013-03-12 00:00:00' AND '2013-03-12 23:59:59'
The DATETIME datatype in MySQL considers the time of the day as well, so, it will not match anything.
If you don't have any use of the time part, then you can simply reduce the datatype to DATE instead of DATETIME. If not, you can use the DATE() function to get rid of the time part and only consider the date part
NOTE THIS WHEN USING between on Mysql
date_column_name between 'startDate' AND 'endDate'
NOTE : you should want to insert +1 date to endDate . Because of when you insert 2015-05-18 date to endDate.you can not get data of 2015-05-18.So you need to plus one date to endDate.
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
Is it posaible to write a query when you don't know any acutal dates? I need to compare transaction dates with funds available dates & update the funds available date by adding a day to the date.
I know MS SQL better than MySQL, but you still should be able to compare them using greater than and less than. If you want to modify the value, you'll want to use the DATE_ADD function (Date and Time functions reference). Comparing transaction date to the funds available date would involve something like WHERE TransTable.TransDate >= CustTable.FundsAvailDate, and then adding one day to the funds available date would be something like SET FundsAvailDate = DATE_ADD(FundsAvailDate, INTERVAL 1 DAY)
I wasn't able to find out (googling, reading mysql reference manual) how to get value of DATETIME in seconds in MySQL.
I dont mean to extract seconds from datetime, but to convert it into seconds.
If by "convert to seconds", you mean "convert to an UNIX Timestamp" (i.e. number of seconds since 1970-01-01), then you can use the UNIX_TIMESTAMP function :
select UNIX_TIMESTAMP(your_datetime_field)
from your_table
where ...
And, for the sake of completness, to convert from an Unix Timestamp to a datetime, you can use the FROM_UNIXTIME function.
If you want to have the difference between two DATETIME values, 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 legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function.
mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
-> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
-> -1
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
-> 128885
unit can also be HOUR which is what you asked for in one of the comments.
The unit argument can be any of the following:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
The level of usefulness of some of the other options will of course be determined by the granularity of the data. For instance, "MICROSECOND" will only have limited use if you are not storing microseconds in your DATETIME values.
Use TIME_TO_SEC in previous versions for mysql
SELECT TIME_TO_SEC(time column) FROM table
i used in mysql
TO_SECONDS(your date goes here) method to convert date to seconds from year 0
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
The function UNIX_TIMESTAMP(datetime) returns the unix time, which happens to be the number of seconds since 1-Jan-1970 0000 UTC. That may be what you need, but not if you're dealing with dates of birth, historical dates, or dates after 2037.
Starting in mysql 5.5.0 you can use to_seconds()
TO_SECONDS(FIELD_NAME)
FIELD_NAME must be DATETIME type
I have created my own query for your problem:
SELECT HOUR(`colname`) * 3600 + MINUTE(`colname`) * 60 + SECOND(`colname`)
FROM widgets
WHERE id = 1;
Use id = 1 if you have to take a specific row.
The output will be in seconds.