In this question, I have 2 query
1) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '12/31/2013'
This query give proper data from the table.
But in 2 query
2) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '01/10/2014'
This query not display any date from table, how this not display any data, there is some year change problem in mysql server.
Please help me.
You have to convert string to date for comparing two dates otherwise it consider as string. For that you have to use STR_TO_DATE() function
Try this:
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2013-12-31'
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2014-01-10'
Related
I have a dataframe like this
Value.
Date
A
08/08/2009
A
09/12/2021
A
05/10/2022
A
06/09/2022
A
07/08/2022
I need output like
VALUE
DATE
A
05/10/2022
A
06/09/2022
A
07/08/2022
We have to print a latest year with all month data present in the date column .please refer output table.
i used SQL query like
Select Top 10 * from table where
Order by (Date) DESC;
The max() select only one date so that didn't help me
But didn't get expected answer.
Can please someone help me with the query ?
You can just use MAX in a subquery, this will produce the intended outcome you have shown in your question:
SELECT yourcolumn
FROM yourtable
WHERE
YEAR(yourcolumn) = (SELECT MAX(YEAR(yourcolumn)) FROM yourtable);
The latest year is 2022, so MAX in the subquery will find this year and the whole query will select all dates in 2022.
SELECT *
FROM tablename
WHERE datecolumn >= (SELECT DATE_FORMAT(MAX(datecolumn), '%Y-01-01')
FROM tablename)
To improve this query you'd have an index by datecolumn (or where this column is an expression prefix).
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
i trying to do a sql query which i combine de compare operators with substring.
in my column date i have the following value inside : 09-01-2014 12:02:55
what i try to now is to select all rows which is >= 09-01-2014 and for example <=22-01-2014
how can i do it?
i have trying for example with this code:
SELECT * From table Where Name= 'Something'
AND SUBSTRING(date,1,10) = '09-01-2014'
AND SUBSTRING(date,1,10) < '22-01-2014'
You can use the BETWEEN operator
SELECT * FROM table
WHERE Name = 'Something'
AND SUBSTRING(date, 1, 10) BETWEEN '09-01-2014' AND '22-01-2014'
EDIT: I'm still leaving this here, but it is not an error proof solution (as pointed out by oerkelens down in the comments)
The BETWEEN operator will work, like this:
SELECT *
From table
Where Name= 'Something'
AND `date` BETWEEN '2014-01-09' AND '2014-01-23'
Working Demo: http://sqlfiddle.com/#!2/b4d7e
Try this:
SELECT *
FROM tableA a
WHERE a.nme= 'Something' AND
DATE(STR_TO_DATE(a.date, '%d-%m-%Y %H:%i:%s')) >= '2014-01-09' AND
DATE(STR_TO_DATE(a.date, '%d-%m-%Y %H:%i:%s')) <= '2014-01-22';
OR
SELECT *
FROM tableA a
WHERE a.nme= 'Something' AND
DATE(STR_TO_DATE(a.date, '%d-%m-%Y %H:%i:%s')) BETWEEN '2014-01-09' AND '2014-01-22';
Using the following syntax makes your query sargable. It allows query to use any Indexes defined on the date column. for more information SARGable Queries with Datetime Datatype
SELECT * From table
Where Name= 'Something'
AND [DateColumn] >= '20140109'
AND [DateColumn] <= '20140122'
You are converting the date from the table row into a string before comparing to the bookend dates. You need to do the opposite. Convert the bookend dates from strings to dates, then compare each test date.
Some form of the CONVERT or CAST function should do that for you.
The reason your approach won't work is that when SQL server compares strings, it uses alphabetical order. You want ascending date order, which is a different order.
Which Database do you use? Oracle:
SELECT *
FROM table tbl
WHERE 1=1
AND name = 'Something'
AND trim(tbl.column) >= to_date('2014-01-09','DD-MM-YYYY')
AND trim(tbl.column) <= to_date('2014-01-22','DD-MM-YYYY')
or you just convert it into a number/integer like YYYYMMDD then the >= =< operators will work too.
my table and fields are like these:
i must find $sy<year<$ey then it must filter only values by $sm<month<$em at last it must find $sd<day<$ed
i need to find records between dates for example like 2010/10/25 , 2010/10/10
at first i tried :
SELECT SUM(barname) allin,SUM(rooz) allhoghogh,user_id FROM work_result
WHERE (`year`>='$sy' and `month`>='$sm' and `day`>='$sd') and (`year`<='$ey' and `month`<='$em' and `day`<='$ed') group by user_id ;
but it cant find records for dates like e like 2010/10/25 , 2010/10/28
than i tried
SELECT * FROM work_result as t1 join work_result as t2 on t1.year<='$sy' and t2.year>='$ey' and t1.month<='$em' and t2.month>='$sm' and t1.day<='$ed' and t2.day>='$sd' WHERE 1 group by t1.wrid
this isnt usful in my case!
i need some thing like priority select first select all between years than month and than day!!
other way is convert mysql records to timestamp by year and month and day and compare it by input date but UNIX_TIMESTAMP('year-month-day 00:00:00') dont worked correct for me.
i used it like :
SELECT * FROM `work_result` WHERE UNIX_TIMESTAMP('year-month-day 00:00:00')>1238921453
If convert to timestamp didn't work for you what about use date_format to convert:
SELECT *
FROM `work_result`
WHERE date_format(concat(year,'-',month,'-',day), '%Y-%m-%d') >
DATE_FORMAT(FROM_UNIXTIME(`yourDateGoesHere`), '%Y-%m-%d')
How can I get records from my table using month/year? I have a table like this:
Name - varchar
DueDate -datetime
Status -boll
DueDate is project due date, I want record corresponding to month/year, not full date, I mean record for specific month.
How can I do this in mysql?
Simply use MONTH() and YEAR():
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = 2010
You could use a between statement:
select * from Project
where DueDate between '2010-01-01' and '2010-02-01'
Do not use this here recommended solution with MONTH() and YEAR(). It prevents MySQL to use index on column DueDate if you have one and it forces MySQL to examine all rows in table.
Instead, use BETWEEN clause like this:
SELECT * FROM Project
WHERE DueDate BETWEEN '2010-01-01' AND '2010-02-01'
Or another solution with IN clause:
SELECT * FROM Project
WHERE DueDate IN ('2010-01-01', '2010-01-02', '2010-01-03', ..., '2010-01-31')
This two solutions allows MySQL to use index, so performance will be better.
SELECT Name FROM Table Where MONTH(datetime) = 1;
1 = January.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_month
it will select current year's specific month
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = YEAR(NOW())
If you input month format "Y-m" you can use:
SELECT * FROM Project WHERE DATE_FORMAT(DueDate,"%Y-%m") = '2010-01'
You can extract the MONTH() and YEAR() for your DueDate.
SELECT * WHERE MONTH(DueDate) = '5' AND YEAR(DueDate) = '1987';
SELECT * FROM TABLE WHERE DueDate LIKE '2020-01%';
use in case you can substring your date data.