Date field datatype text in database - mysql

I want to fetch data between two range but in my database Date field as Text.
How Now fetch data between two range
$startdate = 01-01-2020
$enddate = 31-12-2020
and my database field name DATE as text datatype(format 01-12-2019)
Below is the query I am using
SELECT m.id, m.centers, c.BUDGET_ANNUAL_AMOUNT
FROM Cost_centers m INNER JOIN ANNUAL_BUDGET_BUDGET_CENTER c
ON c.BUDGET_ID = 25
where (START_DATE BETWEEN '$startdate' AND '$enddate')
ORDER BY ID DESC
Please help how to get data with text datattype with range. How to convert text to data .
Please help me

have you tried using STR_TO_DATE() function available in mysql ?
I think something like below should work.
SELECT m.id, m.centers, c.BUDGET_ANNUAL_AMOUNT
FROM Cost_centers m INNER JOIN ANNUAL_BUDGET_BUDGET_CENTER c
ON c.BUDGET_ID = 25
where (STR_TO_DATE(START_DATE, "%d-%m-%Y") BETWEEN '$startdate' AND '$enddate')
ORDER BY ID DESC

According to
My SQL Reference - DATE, DATETIME, and TIMESTAMP Types
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format.
I would suggest trying this format.
There is also a worked example where variables are used in date queries on :
TutorialsPoint

Related

Select only date form datetime field in where condition

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.

Mysql correct compare dates

In my db I have table 'leads' with column 'datum_event' (varchar). I need get rows between 'from' to 'to' datum_event. For example, from 30.11.2018 to 31.03.2019. I use str_to_date for convert varchar to date for compare.
My sql:
SELECT DISTINCT(l.lead_id)
FROM leads as l
WHERE STR_TO_DATE(datum_event,'%d.%m.%y') >= STR_TO_DATE('30.11.2018','%d.%m.%y')
AND STR_TO_DATE(datum_event,'%d.%m.%y') <= STR_TO_DATE('31.03.2019','%d.%m.%y')
ORDER BY l.lead_id desc
But now i get empty result. It is wrong. There is row with datum_event '01.12.2018', but I can't see it in result.
I can see result if compare dates are in one year, for example, from 01.01.2019 to 31.03.2019, but if i compare from 31.12.2018 I can't see any result.
Please help me, how can i solve this? Thanks.
You can maybe use SQL BETWEEN condition.
SELECT DISTINCT(l.lead_id)
FROM leads as l
WHERE (STR_TO_DATE(datum_event,'%d.%m.%y') BETWEEN
STR_TO_DATE('30.11.2018','%d.%m.%y') AND STR_TO_DATE('31.03.2019','%d.%m.%y'))
ORDER BY l.lead_id desc`
select STR_TO_DATE('30.11.2018','%d.%m.%y') it will return 2020-11-30
but your need 2018-11-30 this date so you have to change like below
in format
select STR_TO_DATE('30.11.2018','%d.%m.%Y') this will `2018-11-30`
i have added date format as demolink
so in your query you need to correct your format
SELECT l.lead_id
FROM leads as l
WHERE STR_TO_DATE(datum_event,'%d.%m.%Y') >= STR_TO_DATE('30.11.2018','%d.%m.%Y')
AND STR_TO_DATE(datum_event,'%d.%m.%Y') <= STR_TO_DATE('31.03.2019','%d.%m.%Y')
ORDER BY l.lead_id desc

how to assemble date and time from two different table columns

[![enter image description here][1]][1]I have a requirement to return a result of a orders where the condition is that we have a orderDate which is Date field in a table and one orderCloseTime for the same order which is in different table having only time value.
I have to compare the orderDate with now() but the time should compare from orderCLoseTime column from another table.
i.e if the orderCloseTime is having data 2017-07-17 09:40:12 which is today but if it has passed the time orderCloseTIme then that data should not return in the query.
In Below query, if i have date as a future date then also it is not returning the data, it should only compare the today date with time.
EDIT:
select *
from opsorder mbo
where date(mbo.orderDate)>=date(now())
and storeId in
(select storeid from store mbs
INNER JOIN distributioncenter mbd on
mbd.distributionCenterId = mbs.distributionCenterId
where mbd.orderCloseTime > curtime()
and date(mbo.orderDate)>=date(now())
);
From your description of the data structure it seems that this should work. I couldn't see a reason for the use of a subquery instead of a join.
select
mbo.*,
DATE(mbo.orderDate) + ' ' + mbd.orderCloseTime AS OrderCutOffTime
from mb_opsorder mbo
inner join mb_store mbs
on mbo.storeId = mbs.storeid
inner join mb_distributioncenter mbd
on mbd.distributionCenterId = mbs.distributionCenterId
where
addtime(date(mbo.orderDate), mbd.orderCloseTime) >= now()

How to compare dates in sql query?

I am writing a function that brings me all the matches of specific month and year.
I pass the month number and the year value to that function and it should use the following query to get the results:
SELECT tahminler.match_id, tahminler.tahmin_text,matches_of_comments.match_id
FROM tahminler
INNER JOIN matches_of_comments ON tahminler.match_id = matches_of_comments.match_id
WHERE (
matches_of_comments.match_date = 'I DO NOT KNOW WHAT TO WRITE HERE'
)
AND tahminler.user_id =12
The problem is the matches_of_comments.match_date type is varchar and i pass values of the month and year as numbers to make a form like 11.2013 how can i compare this form that i pass to the varchar date ?? can i convert the type into date ?? and if i can do that how to compare part of the date (just month and year)??
Try this one convert to date first then compare month and year ,note it is bad practiceto store date as varchar
SELECT tahminler.match_id, tahminler.tahmin_text,matches_of_comments.match_id
FROM tahminler
INNER JOIN matches_of_comments ON tahminler.match_id = matches_of_comments.match_id
WHERE (
MONTH(STR_TO_DATE(matches_of_comments.match_date , '%m/%d/%Y'))= '07'//month variable
AND YEAR(STR_TO_DATE(matches_of_comments.match_date , '%m/%d/%Y'))= '2013'//year variable
)
AND tahminler.user_id =12
STR_TO_DATE(str,format)
MONTH
YEAR
STR_TO_DATE('11.2013', '%m.%Y')
will convert the string into a datetime datatype.
To compare in any format you need you can reformat it:
DATE_FORMAT(STR_TO_DATE('11.2013', '%m.%Y'), '%Y-%m-%d')

SQL order by and SQL date

I have 2 doubts related to SQL query's
id name update
1 some 2013-05-03
2 som 2013-05-08
3 smee 2013-06-05
How can i list items on a particular month (I want all records,year and date will not be specified I just want to check the month)
How can I arrange name in alphabetic order and arrange it as groups of names such as (limiting number of records =10)
Array A = names starting with A
Array B = names starting with B
The easiest way, to fetch MONTH from a DATE or DATETIME type of fields is to use the MySQL's date-time function MONTH().
For your query, it shall be:
SELECT *
FROM tblName
WHERE MONTH( `update` ) = <month Number such as 5>
The second would need a more complex query. I'd rather use php to do the grouping better(as I've more control over that language).
You can simply use datatype of the field as DATE or you can store any date as unix timestamp and then convert it whenever you want to show it.
Example: 1363979714 (ISO 8601:2013-03-22T19:15:14Z)
If you want list items on a particular date, you can write your query like this:
Month:
Select * from tableName where update like '%-5-%'
day:
Select * from tableName where update like '%-16'
year:
Select * from tableName where update like '2013-%'