I have a table called "article"
ID Date
1 3rd June 2017
2 2nd April 2016
3 21st June 2017
4 31st December 2015
and I want to find all the ID's where Date has June in it ,Therefore Result should be
ID
1
3
P.S -I tried doing this but I am stuck at this point , here is my Query
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(Date, ' ', 2), ' ', -1) as month
from article
where article.month='June'
Why would you split the string to do that?
Just do:
where date like '%June%'
You can use date functions in MySQL if you intend to make any more elaborate date comparisons later on:
select id from article where MONTH(STR_TO_DATE(Date,'%D %M %Y'))=6
where 6 corresponds to the 6th month (june).
Related
The column has a start and end date
month_name
23 March, 2018 - 23 April, 2018
23 March, 2018 is the start date and 23 April, 2018 is the end date
How can I use them as to the start date and end date to query?
You would need to use SUBSTRING_INDEX to return a part of the string to the left or right of the hyphen. But this would be terrible in terms of performance. So you need to look at your data structure and redesign accordingly. The start and end dates should be in distinct columns with an INDEX and appropriate DATATYPE set (DATETIME, TIMESTAMP, DATE).
I would suggest that you process the text prior to saving to the database, by using client language regex or split/substring functionality to correctly format the dates into MYSQL format before saving.
set #fromto='23 March, 2018 - 23 April, 2018';
select
str_to_date(left(#fromto,locate('-',#fromto)-1),'%d %M,%Y') datefrom,
str_to_date(right(#fromto,length(#fromto)-locate('-',#fromto)),'%d %M,%Y') dateto;
output:
+------------+------------+
| datefrom | dateto |
+------------+------------+
| 2018-03-23 | 2018-04-23 |
+------------+------------+
short explanation:
set #fromto - defines a variable, which is used as input for this example
locate('-',#fromto) - this will find the length of the part in #fromto before the '-', the -1 is needed because where only want the part before the '-'
length(#fromto)-locate('-',#fromto) - this will find the length of the part in #fromto after the '-'
str_to_date(...) - see STR_TO_DATE
For each and every query that would need to use these dates, you would have to unpick one or both of these dates that are relevant to the query using this kind of text and date manipulation
SELECT .....
WHERE whatever_else
AND STR_TO_DATE(SUBSTRING_INDEX(monthname, ' -', 1 ), '%d %M,%Y') = '2018-03-23'
AND STR_TO_DATE(SUBSTRING(monthname, INSTR(monthname, '- ') +2 ), '%d %M,%Y') = '2018-04-23'
In short your queries would be a complete nightmare to write and maintain and any indexing benefits you might have been able to use had these 2 dates been saved in 2 seperate columns are completely lost.
SUBSTRING_INDEX()
SUBSTRING()
INSTR()
STR_TO_DATE()
I am using MySQL And I have two dates "From date" and "To date", and based on these date i want to get week number and dates of that week between "To" and "From" Dates.
I have tried the following mysql query.
SELECT count(*) as count,
CONCAT(DATE_FORMAT(DATE_ADD(FROM_DAYS(TO_DAYS(FROM_UNIXTIME(`webform_submissions`.`submitted`)) -MOD(TO_DAYS(FROM_UNIXTIME(`webform_submissions`.`submitted`)) -1, 7)),INTERVAL -6 DAY),'%M %d'), ' - ' ,DATE_FORMAT(FROM_DAYS(TO_DAYS(FROM_UNIXTIME(`webform_submissions`.`submitted`)) -MOD(TO_DAYS(FROM_UNIXTIME(`webform_submissions`.`submitted`)) -1, 7)),'%M %d')) as date ,
CONCAT(YEAR(FROM_UNIXTIME(`webform_submissions`.`submitted`)), '/', WEEK(FROM_UNIXTIME(`webform_submissions`.`submitted`))) as week
FROM `webform_submissions`
where `webform_submissions`.`nid` = 121
AND DATE_FORMAT(FROM_UNIXTIME(`webform_submissions`.`submitted`), '%Y-%m-%d') between '2019-11-01' and '2019-12-03'
GROUP BY week
ORDER BY `webform_submissions`.`submitted` ASC
The following result is display according to above query.
But it seems that it gives wrong result because week number 43 lies between 21-27 Oct and i want to get result between between '2019-11-01' and '2019-12-03'.
Expected output should be like the screenshot. Because From date "2019-11-01" lies between Oct 28- Nov 03 (Week 44). so records should be start from 44 week number.
Any Idea how to get correct number of week and dates?
Here's a somewhat easier to read version of your query (using nested subqueries since MySQL 5.6 doesn't support CTEs) and using DATE_FORMAT with the %x/%v format to generate the week to match your expected result (October 28 is the start of week 44). Note I've added a MIN into the generation of date so that the query will still work in MySQL 5.7 with SQL mode ONLY_FULL_GROUP_BY.
SELECT COUNT(*) AS count,
CONCAT(DATE_FORMAT(MIN(startofweek), '%M %d'),
' - ',
DATE_FORMAT(MIN(startofweek) + INTERVAL 6 DAY, '%M %d')) AS date,
week
FROM (SELECT submitted - INTERVAL (dayofweek + 6) % 7 DAY AS startofweek,
week
FROM (SELECT nid,
DATE(FROM_UNIXTIME(submitted)) AS submitted,
DATE_FORMAT(FROM_UNIXTIME(submitted), '%w') AS dayofweek,
DATE_FORMAT(FROM_UNIXTIME(submitted), '%x/%v') AS week
FROM webform_submissions
WHERE nid = 121
AND DATE(FROM_UNIXTIME(submitted)) BETWEEN '2019-11-01' AND '2019-12-03'
) AS dates
) AS ws
GROUP BY week
Output (for my sample data)
count date week
3 October 28 - November 03 2019/44
4 November 04 - November 10 2019/45
Demo on dbfiddle
Try to set the second parameter of WEEK() according to your expected result.
With the second parameter you can set the mode with wich you specify wether the week starts with sunday or monday, the week numbers starts with 0 or 1 and the rule defining the first week of the year.
See https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
I am trying to take a date from one column and convert it to a string into a new column. The capitalization would depend on if the month is June or July. (These are the only two months shown) I tried using the date_format() function but wasn't able to have much success with it.
invoice_date | month_due
2014-07-20 | Due in July 2014
2014-06-30 | DUE IN JUNE 2014
Based on your very little information you are giving, I would suggest the following SQL statement:
SELECT
invoice_date,
CASE MONTH(invoice_date)
WHEN 6 THEN CONCAT("DUE IN JUNE ", CONVERT(YEAR(invoice_date), char))
WHEN 7 THEN CONCAT("Due in July ", CONVERT(YEAR(invoice_date), char))
ELSE "SOME OTHER MONTH"
END month_due
FROM invoice
Here, I suppose the table name is invoice. In any case the month is anything other than 6 or 7 it will display "SOME OTHER MONTH".
I have a year column that contains things string value like 2015,2014, 2013, 2012, etc. A month column that displays string value like 1, 2,3,...12. I need to run a select that sort the table considering the both Year and month column. Can anyone provide some input? Is the any system procedures to do it easily.
For Ex.-
Month Year
12 2015
11 2015
10 2015
9 2015
. .
. .
. .
1 2015
I want the output based on the Year and Month descending Order.
If you are using sql server below query will work.
you just need to concatenate the month and date columns
select * from Table order by cast([month] + '/1/' + [year] as date) desc
If you are using mysql then below query will work.
select * from Table order by STR_TO_DATE(( CONCAT(month,'/1/',year)),'%c/%e/%Y %H:%i') desc
I have mysql table with the following structure. All records are static and only for read, and had been imported from CSV so all year, month, day are in correct sequence as per I created them. (No, ID column in this table because I only access data by year, month and day)
mysql table 'daily'
year month day data
1990 01 01 xxxxxxxxxxxxx
1990 01 02 eeeeeeeeeeeee
1990 01 03 rrrrrrrrrrrrr
1990 01 04 ttttttttttttt
.
.
.
Now, I can access the records by simple select statement as below
select * where year=1990 and month=1 and day=03 limit 1
But how can I select the query row + 3 or any number of adjacent rows (before or after the query row)? And I don't want to use ID if there is better solutions.
I would consider using the DATE type instead of separate columns for year, month, and day. Then you can use DATEADD and related functions to find the day you want.
http://dev.mysql.com/doc/refman/5.1/en/datetime.html
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html