I plan to make a selection of multiple database tables.
The problem is that I want the current date is equal to current date, only that it is of type varchar, I can't make a direct comparison with the CURDATE () ... Is there any way to compare the current date with the String type?
Code:
select records.date , records.hour, records.Temp, records.Hum, sensors.idSensor, sensors.idLocalization
from records, sensors
where records.idSensor=sensors.idSensor
and records.date = curdade() // Here is where I wanted to make the comparison date
order by records.date desc;
------------------EDIT (Solved)--------------------------
Thank you all for your help.
I switched CURDATE by curdade.
He made the comparison correctly.
Convert the curdate() to a string of the appropriate format. Something like:
select r.date, r.hour, r.Temp, r.Hum, s.idSensor, s.idLocalization
from records r join
sensors s
on r.idSensor = s.idSensor
where r.date = date_format(curdate(), '%Y-%m-%d') // Here is where I wanted to make the comparison date
order by r.date desc;
You need to use the appropriate format for your date column. I just used the "right" format, the ISO standard date format that is the best to use when representing a date as a string. You can read about the formats in the documentation. And, it is better to turn the current date into the right format than to convert the string to a date. By putting the function on the "constant" part of the comparison, MySQL can still take advantage of an index on date.
Try this. It may work. But this is a date-to-date data type comparison, not a string-to-string comparison.
select STR_TO_DATE(records.date, '%Y-%m-%d') AS date , records.hour, records.Temp, records.Hum, sensors.idSensor, sensors.idLocalization
from records, sensors
where records.idSensor=sensors.idSensor
and date = curdate() // Here is where I wanted to make the comparison date
order by date desc;
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.
i have a huge data with dates as string.
column name date1
datatype varchar
the stored data is in this format:14-Mar-2016 05:44:38pm
Now I have split only date from this string like this: 14-03-2016
By using this: DATE_FORMAT(STR_TO_DATE(gr.date1, '%d-%M-%Y'),'%d-%m-%Y')
Now I am trying to compare the date with this query:
SELECT * FROM
( SELECT date1,DATE_FORMAT(STR_TO_DATE(date1, '%d-%M-%Y'),'%d-%m-%Y') as dateFormatted
FROM `grabt` ) as mTbl WHERE mTbl.dateFormatted >= '19-01-2016'
AND mTbl.dateFormatted <= '25-01-2016'
but it is not working what could be the possible error.?
The timestamp string 14-Mar-2016 05:44:38pm can be converted to a datetime using the STR_TO_DATE() along with the format string %d-%b-%Y %r. We can then obtain only the date portion by wrapping that with DATE(). Have a look here for a demo to see that this works.
SELECT *
FROM
(
SELECT DATE(STR_TO_DATE(date1, '%d-%b-%Y %r')) AS dateFormatted
FROM grabt
) AS mTbl
WHERE mTbl.dateFormatted BETWEEN '2016-01-19' AND '2016-01-25'
As Gordon already pointed out, you should ideally be using date types not strings for your date calculations. And by the way, use a valid date string when comparing in your WHERE clause. YYYY-MM-DD is a valid format, e.g. 2016-01-19, but 19-01-2016 is not.
Learn to use the right types for columns. Perhaps you are stuck with someone else's really bad decision to store date/times as strings. Perhaps you cannot change that. But, within a query, use the right types!
SELECT mTbl.*,
LEFT(date1, 10) as FormattedDate -- Is this really necessary?
FROM (SELECT date1,
STR_TO_DATE(LEFT(date1, 10), '%d-%M-%Y') as thedate
FROM `grabt`
) mTbl
WHERE mTbl.thedate >= '2016-01-19' AND
mTbl.thedate <= '2016-01-25';
This will do the comparison as dates not as strings.
I am trying to run the query;
SELECT PO_PurchaseOrderDetail.ItemCodeDesc,
PO_PurchaseOrderDetail.QuantityOrdered,
PO_PurchaseOrderDetail.QuantityReceived,
PO_PurchaseOrderDetail.UnitCost,
PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo,
PO_PurchaseOrderHeader.PurchaseName,
PO_PurchaseOrderHeader.PurchaseOrderDate
FROM PO_PurchaseOrderDetail PO_PurchaseOrderDetail, PO_PurchaseOrderHeader PO_PurchaseOrderHeader
WHERE (PO_PurchaseOrderDetail.PurchaseOrderNo=PO_PurchaseOrderHeader.PurchaseOrderNo)
AND ***(PO_PurchaseOrderHeader.PurchaseOrderDate > '2013-12-31')***
ORDER BY PO_PurchaseOrderDetail.JT158_WTSalesOrderNo, PO_PurchaseOrderDetail.PurchaseOrderNo
My problem is, that the date is stored in the YYYY-MM-DD format. I need to only show data after a certain date. So, because 2013-12-31 is a string rather than a number, the operator > doesn't work. Any ideas?
Try to CAST string as DATE in following:
P.S. if PurchaseOrderDate is not DATE datatype you could also CAST It: CAST(PO_PurchaseOrderHeader.PurchaseOrderDate AS DATE)
SELECT PO_PurchaseOrderDetail.ItemCodeDesc,
PO_PurchaseOrderDetail.QuantityOrdered,
PO_PurchaseOrderDetail.QuantityReceived,
PO_PurchaseOrderDetail.UnitCost,
PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo,
PO_PurchaseOrderHeader.PurchaseName,
PO_PurchaseOrderHeader.PurchaseOrderDate
FROM PO_PurchaseOrderDetail PO_PurchaseOrderDetail,
PO_PurchaseOrderHeader PO_PurchaseOrderHeader
WHERE PO_PurchaseOrderDetail.PurchaseOrderNo = PO_PurchaseOrderHeader.PurchaseOrderNo AND
PO_PurchaseOrderHeader.PurchaseOrderDate > CAST('2013-12-31' AS DATE)
ORDER BY PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo
im using a query to get data between dates but for some reason it does not pull the data of the last date selected here is my query:
SELECT * FROM order WHERE status = "completed" AND orderdate >= ? AND orderdate <= ? ORDER BY orderid DESC
Im using is equal to or less then... but still?
what am i doing wrong ?
SELECT * FROM order WHERE status = "completed" AND date(orderdate) >= date(?) AND date(orderdate) <= date(?) ORDER BY orderid DESC
It happened with me also, but in my case instead of passing a date I was querying using a datetime variable, Please make sure you are querying with date variable only.
Make sure that orderdate is date as well as your query parameter is also date, or use appropriate function to convert them in date, than query.
Your dates are actually datetimes - so you are actually, in the case of the upperbound, saying "12 midnight" on whichever date you choose. Hence, if it tries to test a value at say 10am in the morning, it fails as being outside the range.
Either set the upperbound date one day forward, or explicitly only test the date part of the datetime...
I know calculating age from DOB is relatively simple but I have an issue with different data entry formats in the database. Also, I know this can be easier using PHP, but I don't know PHP and only have MySQL to work with.
The DOB entered into the DB is entered as "month/day/year" or "00/00/0000". But when calculating against today's date, the date would be formatted as "year-month-day" or "0000-00-00". Furthermore, the month placed in the DOB field can have either a one number month (1/01/1999) or a two number month (01/01/1999), so it's not consistent.
I am trying to use the below to utilize CONCAT, SUBSTRING and LOCATE to output the DOB in a better suited format for the age calculation. I think I'm close but not quite there. Any help would be very much appreciated.
SELECT
CONCAT(SUBSTRING(APPU_DOB,-4,4),'-', SUBSTRING(APPU_DOB,LOCATE('/', APPU_DOB),1),'-',SUBSTRING(APPU_DOB,4,2))
FROM APPU_APP_USER
JOIN APPL_APP ON APPU_APPL_ID = APPL_ID
WHERE DATE_FORMAT(APPL_CREATE_DT, '%Y-%M-%D') >= '2014-01-01';
Instead of Concat use str_to_date function.
select str_to_date( appu_dob, '%m/%d/%Y' ) as 'dob';
on 1/01/1999 it returns a valid date formatted object with value 1999-01-01.
You can use it on other date strings that have single or two digit day or month numbers.
Note: To represent or refer a month, use small case m but not capital M, in the format pattern string.
And you should better redefine the data type of appu_dob field to date. So that you can easily apply date specific functions on it for any calculations.