Put in where sql only the date from datetime - mysql

I have a table in my database like this :
id date origine
1 2015-12-04 16:54:38 1
Now I want to get only data witch have the date = 2015-12-04. So I tried like this :
select * from table where id = 1 and date = "2014-12-04"
But I have no data. Can you help me please ?

You can use the date function:
where id = 1 and date(date) = '2015-12-04'
However, for performance reasons, it is often better to use inequalities. This allows MySQL to use an index on id, date for the query:
where id = 1 and
(date >= '2015-12-04' and date < date_add('2014-12-04', interval 1 day))

you can use Date Function of mysql which returns date from DateTime or truncate the Time part
select * from table where id = 1 and Date(date) = "2014-12-04"

There are several date related function out there you can use, take the following:
select *
from table
where id = 1
and date_format(date, '%Y-%m-%d') = '2015-12-04';
date_format will format your date column to a particular format.

In MSSQL you can simply say
SELECT *
FROM Table
WHERE ID = 1
AND Date > '2015-12-04'
I'm not familiar with mysql, but I assume something similar would work here. This date gets formatted as 2015-12-04 00:00:00 so in effect it matches everything with a date of 2015-12-04 and a time greater than 00:00:00.
If you happen to have rows with time of 00:00:00, just use >= instead.

Related

DateTime operation with MySQL

I'm pretty bad with dates.
I have a mysql table with one field, which is OF DateTime type, called HoraRegistratBBDD.
What I want to do is to select data (any kind of data) from a specific day. So far I was doing this:
SELECT COUNT(*)
FROM
(
SELECT mydata
FROM mytable
WHERE DATE(`HoraRegistratBBDD`) = '".$fecha."' AND
FetOPerdutIMotiu = '1'
GROUP BY Partit,
mydata
) AS Col;
Where $fecha is something like "2016-09-03". THIS WORKS.
But I have a problem. When my HoraRegistratBBDD has (for example) this value:
2016-09-02 10:28:41
I would like to substract 15 hours from it. Meaning that I would like to treat this value like it's actually
2016-09-01 19:28:41
How can I do my query considering that I want to substract hours from it (therefore, day will change sometimes)?
If you want to subtract 15 hours from the HoraRegistratBBDD column, then you can use DATE_SUB:
SELECT mydata FROM mytable
WHERE DATE_SUB(HoraRegistratBBDD, INTERVAL 15 HOUR) = ...
The function that you are looking for is DATE_SUB.
Here are a few links:
http://www.w3schools.com/sql/func_date_sub.asp
How to subtract 3 hours from a datetime in MySQL?
The first one shows you how it works and the other one is a similar question and it has been answered.
SELECT COUNT(*)
FROM
(
SELECT mydata, DATE_FORMAT(HoraRegistratBBDD,'%Y-%m-%d') AS niceDate
FROM mytable
WHERE
FetOPerdutIMotiu = '1'
HAVING niceDate = '".$fecha."'
GROUP BY Partit,
mydata
) AS Col;

get recent most Row specific to date in mysql

I have a table
id |value |date
-------------------
1 |2.8 |28-3-14
2 |2.9 |28-7-14
3 |3.9 |20-1-14
in this table i need to get the value of 21-3-14.
but if value or object is not present for that then query get output of 20-1-14 directly without one by one search object by minus date by 1 day.
if any one know about this please give me suggestion.
You just need to sort by date
SELECT value FROM table WHERE date<='21-3-14' ORDER BY date DESC LIMIT 1;
Based on your table it should print:
2.8
Assuming the date 21-3-14 wasn't there, it should print:
3.9
try this,
SELECT
*
FROM
<tablename>
WHERE
STR_TO_DATE(`date`,'%d-%m-%y') <= STR_TO_DATE('YOUR_DATE','%d-%m-%y')
ORDER BY
`date` DESC < LIMIT 1 >
It is recommended to store date in date format i.e. < yyyy-mm-dd >
you may refer,
PHP mysql insert date format

sql condition as date and db date format is in datetime

I am trying to pass the date as a where condition in select query.but in the database the field is in datetime format.I don't know how to pass the condition?
SELECT * FROM (`scl_students`) WHERE `std_added_on` = '2015-03-03'
i got it.
SELECT * FROM (`scl_students`) WHERE DATE(std_added_on) = '2015-03-06'
I guess the problem is that the std_added_on contains time portion which could be non-zero. In order to select all rows for a given date you would write:
SELECT *
FROM `scl_students`
WHERE `std_added_on` >= '2015-03-03'
AND `std_added_on` < '2015-03-03' + INTERVAL 1 DAY
This is better than DATE(`std_added_on`) = ... performance wise.
You convert datetime to date after u try to run query
Use that query as this.
SELECT * FROM (scl_students) WHERE std_added_on = ('2015-03-03');
The datetime format expects that you parse time value also.
Try doing the following code.
SELECT * FROM (scl_students) WHEREstd_added_on= '2015-03-03 00:00:00'
Above query will only if your std_added_on value stored in database is = 2015-03-03 00:00:00

Mysql Select with Dates and maybe Case when

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.

How to select from a DATETIME column using only a date?

I have a DATETIME column on my table which stores when a record was created. I want to select only the records created on a particular date. If I try:
SELECT *
FROM myTable
WHERE postedOn = '2012-06-06'
It returns no rows even though there are many rows in the table with the postedOn set as
2012-06-06 21:42:02, 2012-06-06 07:55:17 , and so forth.
Any ideas?
Use the DATE scalar:
SELECT *
FROM myTable
WHERE date(postedOn) = '2012-06-06'
SELECT *
FROM myTable
WHERE DATE(postedOn) = '2012-06-06'
DATE() returns the date part of a datetime field.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
Create a time range by adding a day to the date:
SELECT *
FROM myTable
WHERE postedOn >= '2012-06-06' and postedOn < '2012-06-07'
This is the most efficient way, as the query can use an index on the field.