Calculate expiry date using mysql query.
calculate make_date and expiry_date difference against tblt_name in mysql query.
You can use this below query..
select tblt_name,
(CASE
WHEN datediff(expiry_date,CURDATE()) > 0 then datediff(expiry_date,CURDATE())
ELSE 'Expired'
END) as Remaining_days_expired from tablets;
SQL fiddle
Is this along the lines of what you want:
SELECT tblt_id, tblt_name, make_date, expiry_date, expiry_date - make_date AS shelf_life
FROM medicine
You can use use DATEDIFF() function to do so
SELECT tblt_id , tblt_name , DATEDIFF(s,expiry_date,make_date) FROM TABLE.
First para just put the different date type you wan to compare
s is just display the different in Seconds format.
Related
I made a SQL Statement and I want to use the date but without the time.
My Select is:
SELECT DATEPART(dw, [Order].PerformDate)
And my Group by is:
GROUP BY [Order].PerformDate
Now how can I ignore the time?
You can use CONVERT function of SQL
select datepart(dw, CONVERT(DATE,[Order].PerformDate))
GROUP BY CONVERT(DATE,[Order].PerformDate)
Cast datetime value to date:
select cast(`Order`.PerformDate as date) as PerformDate
GROUP BY says "I want one result row per ____". In your case one row per PerformDate. If PerformDate is a datetime, but you only want to have one result row per date without time, then you must extract the date part:
group by cast(performdate as date)
You also want to display the weekday with datepart(dw, performdate) but this is no longer possible, because PerformDate is no longer available. Only its date part is. So:
select datepart(dw, cast(performdate as date))
from ...
group by cast(performdate as date);
Another one method:
select date_format(`order`.PerformDate, '%Y%m%d')
...
group by 1
im having a problem where i cant think of a solution, maybe im having a bad table-structure or i just dont know enough about mysql select commands to think of a good solution. Maybe you can help me out:
So i got a table that has a Column with the Date-format (yyyy-mm-dd) i wanted to select all upcoming dates so i did:
SELECT * WHERE date >= now.
This worked kinda well but i also got "dates" where only the year is entered (2014-00-00) i also wanted to select these but "now" is already bigger so i made another column with the year only and if the month, date or both arent known i will use 0000-00-00 and the Column "year" now i could select like this:
SELECT * WHERE date >= now AND year >=now(year)
Now all entrys with 0000-00-00 wont be selected. If i use OR the entrys from last year will be shown.
So thats my problem, is there any way i can change my table so i can have entries with only the year or only year and month and of course all together? I already considered get rid of the date-format and use simple INT with seperated columns for year, month and date. But i think i will have the same problem.
Sometimes i just want to do a capsuled select like
SELECT *
WHERE (date >= now AND year >= now(year))
OR date == "0000-00-00" (i know that this doesnt work)
If I understood your problem correctly, you could use this request:
WHERE (date >= now OR year > now(year))
There is probably a simpler way though, that would preserve your design, like initializing at January 1st (01-01) instead of 00-00
I think you can use this code:
$_SESSION['month'] = //set here your selected month
$_SESSION['year'] = //set here your selected year
SELECT * FROM table WHERE DATEPART(m,date) >= '".$_SESSION['month']."' AND DATEPART(yyyy,year) >= '".$_SESSION['year']."' AND date <> '0000-00-00'
Change your table structure format. Actually just allow for that field to have null value when not entered. By default it will be null then. You shouldn't be storing 0000-00-00 as a value for Date type field. I would rather leave it as null , or as suggested in some of previous answers, initialize it with some other date. It would be much easier to manipulate with database then.
the problem is that half of you write is not MySQL and your database schema is terrible...
You have the following problems:
column data date does not have the date data type.
To fix it, you need to add a cast to the select statement eg. cast(datecolumn as date)
select * from table where cast(datecolumn as date) >= '2014-01-10';
the way to use now date is using the now function.
select now(), date(now());
result> 2014-01-10 11:11:36, 2014-01-10
select * from table where cast(datecolumn as date) >= date(now());
Because your datecolumn is not a date (2014-00-00 is not a valid date), you need to use string manipulation to extract the year.
select substring('2014-01-01', 1,4)
result> 2014
select * from table where substring(datecolumn, 1,4) = year(now());
The comparassion operator is = and not ==
the select statement syntax looks like this (pay attention because you are missing the table in your statement)
select * from [Table] where [column] = condition ...
You probably need or instead of ands, therefore your query should look like this:
select * from FooTable where
cast(datecolumn as date) >= date(now())
or substring(datecolumn, 1,4) >= year(now())
or datecolumn = '0000-00-00'
You should use something like phpmyAdmin or mySQL workbench to test your sql queries before try to use them on php, java or whatever is your programing language.
I have this problem if anyone can help.
There is a field (date) in my table (table1) that is a date in the format 3/31/1988 (M/D/y), and my necessity is to define how many days have passed since that date.
I have tried to give this instruction
SELECT DATEDIFF(CURDATE(), date) AS days
FROM table1
But it gives back 'null' and I think this happens because the two date formats are different (CURDATE() is YMD.....
Is it correct? can anyone help me?
Thank you in advance
You can use STR_TO_DATE():
SELECT DATEDIFF(CURDATE(),STR_TO_DATE(date, '%m/%d/%Y')) AS days
FROM table1
SQLFiddle Demo
Your DATE field should have DATE or DATETIME format to be used as DATEDIFF argument correctly.
Also DATE is MySQL keyword and I am not sure that you can use it as valid field name.
You can use this for accurate result
SELECT DATEDIFF(CURDATE(), DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(`date`)), '%Y-%m-%d')) AS days FROM `table1`
If you want to consider results without - signs that you have to follow parameters position as below :
SELECT DATEDIFF(Big_Date,Small_Date) AS days FROM table1.
positive results e.g 5 (with no sign), if you place a Small date as the first parameter then it will results minus sign e.g -5.
I have a mysql database that looks like this:
id | userid | timestamp | activity
Timestamp is a datetime data type, I need to get the data grouped by month, day and hour. I am using mysql and php for my scripts.
I am able to do it by month and day with the following query:
$query = "SELECT COUNT(id) as totals FROM security_transactions WHERE YEAR(timestamp) = 2012 GROUP BY MONTH(timestamp), DAY(timestamp)";
I need to do it by month day and hours.
Please help.
Thanks.
You can add , HOUR(TIME(timestamp)) to your group by query providing your column is of DATETIME format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_hour
Also, from the error messages put in the comments below, it looks like #Aprentice is not using mysql, but I've improved this answer for others looking for mysql.
I have never used mssql, so I can't test this but the following might work to group by nearest hour:
GROUP BY dateadd(hour, datediff(hour, 0, timestamp, 0)
Just take it one step further and use HOUR() as well. You will first need to extract the time portion of the timestamp. But guess what, there is a function for that as well ;)
Hi I was wondering why this statement is working in mySQL
SELECT COUNT(*) AS `numrows`
FROM (`myTable`)
WHERE DATE_FORMAT(creationDateTime, '%m/%d/%Y') BETWEEN '02/21/2011' AND '03/20/2011'
but this is not
SELECT COUNT(*) AS `numrows`
FROM (`myTable`)
WHERE DATE_FORMAT(creationDateTime, '%m/%d/%Y') BETWEEN '12/21/2010' AND '03/20/2011'
The first statement returns 'xx' count of the number of rows while the second one returns '0'
The only difference I see is that the "from" date is in 2010 and the "end" date is in 2011. To test if this was the problem I queried from '12/31/2010' and it still gave me 0 results but when I set the start date as '01/01/2011' it gave me the number of records that were created in that time span.
Is there something I am missing with regards to mySQL's BETWEEN and using dates from different years?
Thanks for the help!
Try using the date format BETWEEN '2010-12-21' AND '2011-03-20'. Also remove the DATE_FORMAT() function.
So, this:
SELECT COUNT(*) AS `numrows`
FROM `myTable`
WHERE creationDateTime BETWEEN '2010-12-21' AND '2011-03-20'
DATE_FORMAT() returns a string, not a date. By using it you're forcing a string comparison instead of a date comparison. You should omit the DATE_FORMAT and use YYYY-MM-DD date strings instead.