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.
Related
I have stored this kind of format 2022-02-06 18:40:00 in my trans_reminder_date. I want to use only date in where condition but with this condition i am not able to fetch data
$today = date('Y-m-d');
SELECT * FROM sales_detail
WHERE trans_reminder_date = '".$today."'
AND trans_reminder_date != ''
ORDER BY sales_detail_id DESC";
If when your filter parameter is a string:
select * from sales_detail
where cast(trans_reminder_date as date) = cast('2020-03-22' as date)
if you want to use the current date for filtering then MySQL has a function that getting only the current date without time.
select * from sales_detail
where cast(trans_reminder_date as date) = curdate()
On MySQL for converting other types to another, you can use a cast
P.S.
Starting with MySQL 8.0.13 we have now an easiest way to create functional indexes. When you are using cast(updated_at as date) then DB will not use index for column updated_at. You must create a functional index for best performance.
The following query will give you all the result for the current date. By doing this there won't be any need to cast values.
SELECT
*
FROM
sales_detail
WHERE
trans_reminder_date >= curdate()
AND trans_reminder_date < curdate() + INTERVAL '1' DAY
ORDER BY
sales_detail_id DESC;
Using Cast function to change datetime type to date example
Cast(column_name as date)
Or
You using convert function change datetime type to date
CONVERT(column_name, date);
CONVERT(expression, datatype);
OR,
CONVERT(expression USING character_set);
character_set: It specifies the desired character set in which we want to be converted.
Data type : It specifies the desired data type in which we want to be converted.
Expression : It is a specified value going to be converted into another specific datatype.
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 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
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 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.