I have this query, i need to select all records between two dates, the mysql table is a datetime format.
I tried this but it didn't work.
select * from cdr
WHERE calldate BETWEEN '2012-12-01' AND '2012-12-03';
Try this instead:
select * from cdr
WHERE DATE(calldate) BETWEEN '20121201' AND '20121203';
Try this query -
SELECT * FROM cdr WHERE calldate BETWEEN str_to_date('2012-12-01','%Y-%m-%d') AND str_to_date('2012-12-03','%Y-%m-%d');
The above will work great. If you wanted to add extra conditions, normal Boolean logic works with date/time as well so you could do something like
...
where DATE(calldate) < '20121201' AND DATE(calldate) >= '20121203' OR DATE(calldate) = '20121205'
Just a simple example.
Related
As you show in the below images I want to fetch result in between date from date range.
I have try this query but it doesn't work.
I want bigger date then DTT_EVT_start start table date.
Can anyone suggest me how can I use date(not time)column in where clause ?
SELECT *
FROM `dwssgv_esp_datetime`
WHERE (DATE_FORMAT(DTT_EVT_start, '%Y-%m-%d') < '2015-05-10')
AND (DATE_FORMAT(DTT_EVT_end, '%Y-%m-%d') > '2015-05-10')
Something like this?
SELECT * FROM dwssgv_esp_datetime WHERE DTT_EVT_start < '2015-05-10' AND DTT_EVT_end > ''2015-05-10'
You can avoid DATE_FORMAT function,
as it would affect on the performance if its a huge table
you may simply do like,
SELECT
*
FROM
dwssgv_esp_datetime
WHERE
DTT_EVT_start < CAST('2015-05-10 00:00:00' AS DATETIME) AND
DTT_EVT_end > CAST('2015-05-10 00:00:00' AS DATETIME)
Here, < 2015-05-10 > is your variable date.
OR
SELECT
*
FROM
dwssgv_esp_datetime
WHERE
DATE(DTT_EVT_start) < CAST('2015-05-10' AS DATE) AND
DATE(DTT_EVT_end) > CAST('2015-05-10' AS DATE)
You can convert dates in UNIX_TIMESTAMP format and compare
Try this
SELECT
*
FROM
`dwssgv_esp_datetime`
WHERE (UNIX_TIMESTAMP(DTT_EVT_start) < UNIX_TIMESTAMP('2015-05-10')
AND (UNIX_TIMESTAMP(DTT_EVT_end) > UNIX_TIMESTAMP('2015-05-10')
Here is a simple solution:
SELECT *
FROM dwssgv_esp_datetime
WHERE
CAST('2015-05-10' AS DATE)
BETWEEN DATE(DTT_EVT_start)
AND DATE(DTT_EVT_end)
BETWEEN AND allows you to check if a value is in a particular range or not. By using this you do not have to write the value you want to compare twice.
I have a datetime column that i'm trying to compare with a date value in the DD/MM/YYYY format.
I'm trying this:
SELECT * FROM TABLE WHERE STR_TO_DATE(DATETIME_COLUMN, '%d/%m/%Y') = '04/04/2014'.
It doesn't work.
Even tried this:
SELECT * FROM TABLE WHERE DATETIME_COLUMN = '04/04/2014'
Which seems to be working on the rest of my code, but it doesn't.
Is there another function?
You can achieve your goal using this query which will keep you predicate sargable.
SELECT * FROM TABLE WHERE DATETIME_COLUMN = STR_TO_DATE('04/04/2014', '%d/%m/%Y')
if your DATETIME_COLUMN column has time part and you want to get all dates within given date, use this:
SELECT * FROM TABLE
WHERE DATETIME_COLUMN >= STR_TO_DATE('04/04/2014', '%d/%m/%Y')
AND DATETIME_COLUMN < STR_TO_DATE('05/04/2014', '%d/%m/%Y')
Use like this
SELECT * FROM TABLE WHERE DATE_FORMAT(DATETIME_COLUMN,'%d/%m/%Y') = '04/04/2014';
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.
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'
No idea what is going on here. Here is the query, right from phpMyAdmin:
SELECT * FROM `la_schedule` WHERE 'start_date' >'2012-11-18';
But I consistently get all records in the table returned, including those with start date 2012-11-01. What gives?
you have enlosed start_date with single quote causing it to become string, use backtick instead
SELECT * FROM `la_schedule` WHERE `start_date` > '2012-11-18';
SQLFiddle Demo
In your statement, you are comparing a string called start_date with the time.
If start_date is a column, it should either be
SELECT * FROM `la_schedule` WHERE start_date >'2012-11-18';
(no apostrophe)
or
SELECT * FROM `la_schedule` WHERE `start_date` >'2012-11-18';
(with backticks).
Hope this helps.
Try this.
SELECT * FROM la_schedule WHERE `start_date` > '2012-11-18';
I have tried but above not working after research found below the solution.
SELECT * FROM my_table where DATE(start_date) > '2011-01-01';
Ref
Adding this since this was not mentioned.
SELECT * FROM `la_schedule` WHERE date(start_date) > date('2012-11-18');
Because that's what actually works for me. Adding date() function on both comparison values.
In my case my column was a datetime it kept giving me all records. What I did is to include time, see below example
SELECT * FROM my_table where start_date > '2011-01-01 01:01:01';
If you are comparing timestamp - you could try following
select * from table where columnInTimestamp > ((UNIX_TIMESTAMP() * 1000) - (1*24*60*60*1000))
Here UNIX_TIMESTAMP()gives current timestamp where as "12460601000" is the timestamp for 1 day -- With this you can find data just got created in 1 day or 2 days etc.