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')
Related
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
I'm working on a task where I need to find the expected date to resolve a ticket using createdAt and sla_name fields values. After that I need to compare the this expected date with the dates in holidays table.
If the expected date falls in holidays, I need to extend the sla_name field value.
This is the query am using.
SELECT t.sla_meet, t.tid, t.ticket_id, t.ticket_name,t.createdAt,t.updatedAt,t.status, dw.dropdown_name
as ticket_priority,p.project_name, dw3.dropdown_name as ticket_status,t.sla as sla_name,
isn.issue_name as issue_type,inn.incidentName as incident_type,t.ticket_accepted_date,
t.asset_id,t.ticket_closed_date,t.contact_number,
IF(NOW() <= DATE_ADD(t.createdAt,INTERVAL (t.sla)+1 DAY),'YES','NO') AS slaMeetData
from tickets t
JOIN assets ast ON t.asset_id=ast.asset_id
JOIN projects p ON p.project_id=ast.project_id
JOIN admin_dropdowns dw ON t.ticket_priority=dw.id
JOIN admin_dropdowns dw3 ON t.ticket_status=dw3.id
JOIN issues isn ON t.issue_type=isn.issue_id
JOIN incident_names inn ON t.incident_type=inn.incidentId
order by t.tid DESC
This is the resultant data of the above query.
Now I need to compare the holidays in above query. And the sample data is,
If the expected date that am getting in IF condition of above query is falls in this holidays, I need to update the sla_name value with COUNT OF HOLIDAYS(If startdata and enddate are there, need to count the days between them) + sla_name.
If expected date is falls on dates range(start and end dates of holidays), need to calculate the count of days from expected date to end date and update that count in sla_name field
Is it possible to do this functionality in SQL? I've used the above query as VIEWS.
Instead of t.sla AS sla_name, use this expression to determine whether to add the length of the overlapping holiday to the number of days:
(
t.sla +
IF(
DATE_ADD(t.createdAt,INTERVAL (t.sla)+1 DAY) BETWEEN holidays.holiday_date AND holidays.end_date,
DATEDIFF( holidays.end_date, holidays.holiday_date ), /* add holiday length number of days */
0 /* no holiday overlap so don't add any days */
)
) as sla_name
You'll also have to join on the holidays table to find the holiday (if any) which overlaps the date in question:
JOIN holidays ON ( DATE_ADD(t.createdAt,INTERVAL (t.sla)+1 DAY) BETWEEN holidays.holiday_date and holidays.end_date )
I want to make a query where I get people birthday's in a current month, I have problems with MONTH() it does not work for me, the date format in the database is: 04/18/1990, and I want to compare if the month current date('m') is equal to the month of the database.
Any suggestions?
you can try this one
SELECT * FROM table_name where extract(month from BirthDate) = date("m");
where date("m") return current month and BirthDate is column name in which you have stored birthdate.
Try this
"select month(str_to_date(birthdate), '%Y/%m/%d')
from users
where month(str_to_date(birthdate), '%Y/%m/%d') = '$month'"
Here, i m assuming your delimeter is '/', Please set your delimeter according to your string date i.e 2013/05/04 or 2013,05,04 or 2013-05-04
could be you have some convertion issue try using
select month(str_to_date(my_col, '%m/%d/%Y'))
from my_table
where month(str_to_date(my_col, '%m/%d/%Y')) = month(now())
How I make to get back login and nb_match over the year and the current month of my table?
this is my query for all matching without the month ....
SELECT login, SUM(column1)
FROM 'my_table'
NATURAL JOIN 'join_table'
GROUP BY login
(at_limitation_admin is the natural join on 'login' column). 'month' is varchar column. (example 2009/03)
To pull the month out of the varchar field use:
MONTH(STR_TO_DATE(`month`,'%Y/%m'))
I don't fully understand your end goal, I can add info if needed.
Edit:
You can use this WHERE clause get results for current month and year
WHERE MONTH(STR_TO_DATE(`month`,'%Y/%m')) = MONTH(NOW())
AND YEAR(STR_TO_DATE(`month`,'%Y/%m')) = YEAR(NOW())
my dates in my table are strings in the format:
"10/12/2009"
Now how would one get all the records from a month, lets say June (number "6" being provided)?
Check the MySQL function STR_TO_DATE.
You should not store dates as string, however. Use the type DATE.
The short answer to your question is that you can use the STR_TO_DATE and MONTH functions to 1) convert the string representation into a DATE, and 2) extract the month component from the date:
SELECT t.*
FROM mytable t
WHERE MONTH(STR_TO_DATE(t.dateasstringcol,'%M/%d/%Y')) = 6
(This is assuming here that by '10/12/2009', you are specifying Oct 12, and not Dec 10. You'd need to adjust the format string if that's not the case.)
Alternatively, if month is indeed the leading part of the date, you could do a simple string comparison, if the month is the leading component:
SELECT t.*
FROM mytable t
WHERE t.dateasstringcol LIKE '6/%'
OR t.dateasstringcol LIKE '06/%'
(You could eliminate one of those predicates, if you have an exact format specified for the striing value representing the date: either if month is always stored as two digits -OR- the month is never stored with a leading zero.)
If you are passing in an argument for the month, e.g. '6', then you could construct your statement something like this:
WHERE t.dateasstringcol LIKE '6' + '/%'
If month is the second component of the date, then you could use:
SELECT t.*
FROM mytable t
WHERE t.dateasstringcol LIKE '%/' + '6' + '/%'
OR t.dateasstringcol LIKE '%/' + '06' + /%'
NOTE:
All of the previous example queries will return rows for June of any year (2009, 2010, 2011)
You can extend those examples, and do something similar with the year, using the YEAR function in place of the MONTH function, or for string comparison
AND t.dateasstringcol LIKE '%/%/2011'
Normally, we'd extract rows for a particular month for a particular year, using a date range, for example:
SELECT t.*
FROM mytable t
WHERE MONTH(STR_TO_DATE(t.dateasstring,'%M/%d/%Y')) >= '2011-06-01'
AND MONTH(STR_TO_DATE(t.dateasstring,'%M/%d/%Y')) < '2011-07-01'
Of course, when the date value is stored as a DATE datatype rather than as a VARCHAR, this means we don't need the STR_TO_DATE and MONTH functions, we can specify a comparison to the native DATE column. This approach allows us to make use of an index on the date column, which can improve query performance on large tables.
SELECT t.*
FROM mytable t
WHERE t.realdatecol >= '2011-06-01'
AND t.realdatecol < '2011-07-01'
The STR_TO_DATE function is your friend here:
SELECT * FROM my_table
WHERE STR_TO_DATE('10/12/2009','%M/%d/%Y') >= '2012-06-01';
MONTH should help here if we want current month or particular month data. e.g:
$month = date('m'); OR particular month.
SELECT * FROM users WHERE MONTH(str_to_date("10/12/2009",'%e/%m/%Y')) = $month;