how to search month with year in mysql - mysql

I have a table. It has a varchar type date column.
I want to search that table using date, but i want to search only from month with year.
example: 12-2016
How can I write the query for that?
I want that part to assign to a variable of jasper
**example:**WHERE ............. =$P{invDate}
example as in my jasper report:
SELECT invoiceheader.inNo AS invoiceheader_inNo, invoiceheader.cusID AS invoiceheader_cusID, invoiceheader.soNo AS invoiceheader_soNo, invoiceheader.total AS invoiceheader_total, invoiceheader.dis AS invoiceheader_dis, invoiceheader.netTotal AS invoiceheader_netTotal, invoiceheader.cash AS invoiceheader_cash, invoiceheader.bal AS invoiceheader_bal, invoiceheader.date AS invoiceheader_date FROM invoiceheader invoiceheader WHERE ...........................................=$P{invDate}
I need a perfect query for the above space

Try this (you have used varchar data type for date so like keyword will perfectly suitable for this situation):
select * from table where date like concat('%' ,$P{invDate});

Try this case:
STR_TO_DATE(date_field, '%d-%m-%Y') < STR_TO_DATE('12-2016', '%m-%Y')

Try following -
select * from Table_name where month(date_column_name)=12 and year(date_column_name)=2016

Related

MySQL - sort by date column which is stored as text in format MMYYYY

I'm trying to sort by a column called date which stores various dates in format MMYYYY (for example, 122020, 102019, etc.).
The SQL query that I have looks like this:
SELECT `date`, `invested` FROM `savings` WHERE `id` = 123 ORDER BY STR_TO_DATE(`date`, "%m%Y") ASC but this query does not sort the output correctly.
Any ideas on how to sort it properly?
Important note: Reclassifying/modifying the column's type is not an option at this point.
EDIT: My current SQL query, which is described above, sorts the dates like this: 102019, 102020, 112019, 112020. But my goal is to have it like this: 102019, 112019, 102020, 112020.
Thank you
Try separating the date string into two parts (year and month), cast each part as integer, and then ordering by that number:
SELECT
date_,
invested
FROM
savings
WHERE
id = 123
ORDER BY
CAST(SUBSTRING(date_, 3, 6) AS UNSIGNED),
CAST(SUBSTRING(date_, 1, 2) AS UNSIGNED)
;
This will do the job :) refer to this DB Fiddle for clarification.

Mysql search month varchar field

I have departure data table:
dep_date
10/01/2018
21/02/2018
14/03/2018
25/04/2018
Where dep_date field is Varchar
How do I search for month=3 in dep_date?
Just use like %/03/%.
Or the complete line is:
select * from table_name where dep_date like '%/03/%';
I would like to suggest you to use date type to store date in database instead of varchar.
Try Following Command : (With date type)
Select * FROM departure WHERE MONTH(dep_date) = 3
:)

SELECT range of date in Text field

Date
9/25/2015
9/26/2015
9/27/2015
9/28/2015
9/29/2015
9/30/2015
10/1/2015
10/2/2015
10/3/2015
10/4/2015
10/5/2015
Can anyone help me in MySQL. I would like to select only date from 9/28/2015 to 10/4/2015.
Please take note, this date is in Text field.
Thank you.
you can use STR_TO_DATE(yourdatefield, '%m/%d/%Y') to convert text to date and you can later use between clause to restrict output data.
Convert first your dates using CONVERT, then use BETWEEN in your WHERE clause.
Try this..
SELECT * FROM TableName
WHERE Date BETWEEN CONVERT(DATE,'9/28/2015') AND CONVERT(DATE,'10/4/2015')
You can try like this:
WHERE `Date` BETWEEN CAST('9/28/2015' AS DATE) AND CAST('10/4/2015' AS DATE)
or
WHERE STR_TO_DATE(`Date`, '%m/%d/%Y') BETWEEN STR_TO_DATE('9/28/2015', '%m/%d/%Y') AND STR_TO_DATE('10/4/2015', '%m/%d/%Y')
DEMO
Also try to avoid storing dates as Text. Instead use Date datatype to store dates.
Try this , in where clause used function str_to_date
SELECT `dateVal`,`id`
FROM `datecheck`
WHERE STR_TO_DATE(`dateVal`,'%m/%d/%Y') between STR_TO_DATE('9/28/2015',
'%m/%d/%Y') AND STR_TO_DATE('10/4/2015', '%m/%d/%Y')
I had the same type of issue but in my case the date stored also contains the time (also in a text field, for instance 2017-09-11 05:07:58 PM). This is for a wordpress site but I want to query the database directly, the date is in a meta_value.
To make this work I ended using a subbstring of the date, i am posting this in case it helps someone:
SELECT ID, display_name, user_email, meta_value FROM bfge_users, bfge_usermeta WHERE (bfge_users.ID = bfge_usermeta.user_id) AND meta_key ='user_login' AND CAST(SUBSTR(meta_value,1,POSITION(' ' IN meta_value)) AS DATE) between '2017-09-11' AND '2017-09-13';
use between, you can read more here:
Select data from date range between two dates
and question is dublicate

Mysql Select with Dates and maybe Case when

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.

Select from MySQL datetime field where the year doesn't matter

I have a MySQL DateTime field which represent opening times.
I want to write a statement that will allow me to select rows from my table independent of the year supplied (Ex. *-12-17 00:00:00)
Try this:
SELECT * FROM table1
WHERE DAYOFMONTH(datecolumn) = 17
AND MONTH(datecolumn) = 12
Reference for DAYOFMONTH.
You did not say what you wanted to choose by if not the year: Go to the MySQL page and choose the correct functions.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html