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
:)
Related
I have a table were the date required to be fetched is of type character. I tried using CONVERT function but it isn't working.
The query works fine in postgres but not in db2.
select * FROM abc
where (to_date(from_month_year,'MM/YYYY') between '01/04/2017' and '01/04/2018')
OR (to_date(to_month_year,'MM/YYYY')between '01/04/2017' and '01/04/2018')
union
select * FROM abc
where ('01/04/2017' between to_date(from_month_year,'MM/YYYY')
and to_date(to_month_year,'MM/YYYY'))
OR ('01/04/2018' between to_date(from_month_year,'MM/YYYY')
and to_date(to_month_year,'MM/YYYY'))
try to use Date Format str_to_date.
DATE_FORMAT(STR_TO_DATE(StartDate,'%d-%m-%Y'), '%m-%Y')
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
I have created MySQL table :
CREATE TABLE EMP(
EMPID INTEGER NOT NULL (5),
SURNAME VARCHAR(25),
SAL INTEGER(5),
JON VARCHAR(25),
START_DATE DATE,
END_DATE DATE,
DEPNO INTEGER(5)
);
with following records:
INSERT INTO EMP
(EMPID,SURNAME,SALARY,JOB,START_DATE,END_DATE,DEPNO)
VALUES
('1','Vorosila','500000','COO','20150101',null,'1');
however I need to change date format from 2015 01 01 to 01 01 2015
Can anybody show me or tell me how to do that ?
THANK YOU IN ADVANCE
DATE values do not have a "format", they are objects that represent instants in time (or entire days, but still independent of formatting).
Formats are applied on input and output, so you just need to apply the correct format, which you can find in the MySQL manual, to the SELECT statement.
You cannot change the default date format in mysql.
I once hoped for the default date to be editable so I wouldn't have to jump through these hoops to get the date I actually wanted, mysql even has a date format system variable, but it is unused. Date Format Mysql - link
What you should really do is store it as the default format Year-Month-Date and then convert it on select.
The first thing I'd suggest is having your date columns as date types, which would give your dates the following format '2015-01-01'.
If you do this then you can use DATE_FORMAT - link - the second value in the DATE_FORMAT function allows you to customise the returned date, and there are many different thing you can do with this if you look at the link:
SELECT
DATE_FORMAT(`START_DATE`,'%d-%m-%Y')
AS `START_DATE`
FROM ...
The other option you have is to store your dates in the format that you already want as a char or varchar column.
HOWEVER, as should be obvious, this column will not be treated as storing dates, and so will not give you the correct comparisons in a where clause when using > < BETWEEN or the correct ordering in an order by clause. It is after all just a string of numbers in this case.
However you can then use STR_TO_DATE - link if you did need to use a where or order by on this column to change it back to a date within the query - in this case the second value is the custom format of your 'dates' in the column. Keep in mind with a where you will need to compare it with the correct mysql format as shown below:
SELECT
`START_DATE`
FROM table
WHERE STR_TO_DATE(`START_DATE`,'%d-%m-%Y') BETWEEN '2015-01-01' and '2016-01-01'
In MySQL you can change the format of a date using DATE_FORMAT method which is similar to to_char in Oracle.
DATE_FORMAT(SYSDATE(), '%DD-%MM-%YYYY');
For more information about specifiers check this thread http://www.sqlines.com/oracle-to-mysql/to_char_datetime
You can do what you probably want by creating a view and referring to that instead of the (underlying) table.
CREATE VIEW emp_view AS
SELECT empid,
surname,
sal,
jon,
date_format(start_date, '%d-%m-%Y') as start_date,
date_format(end_date, '%d-%m-%Y') as end_date,
depno
FROM emp;
Note that this changes the type of the date columns to varchar, so comparisons will no longer work as expected:
SELECT * FROM emp_view WHERE start_date > '01-12-1924'; // fails!
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
Im trying to query some data and one of them is a datetime format. I want that is shows dd/mm/yy with no time on it directly form the select. Is this possible?
My query is and the join_date is datetime that i need to be changed to short date:
$query = mysql_query("SELECT id,username,join_date,is_active FROM members",$con)
or trigger_error(mysql_error());
This query goes directy in a Json output array. So i want to convert it directy form the query.
Use the MySQL DATE_FORMAT function for this:
SELECT id, username, DATE_FORMAT(join_date, '%d/%m/%y') AS join_formatted, is_active
FROM members
In this example, the column name for the formatted date will be join_formatted, and its type will be VARCHAR.
The format string returns the date as dd/mm/yy as requested, but I'm personally more comfortable when the date includes the full century. To get the full century, use uppercase Y in the format string: %d/%m/%Y.
try this :
"SELECT id, username, DATE_FORMAT(join_date,'%d/%m/%y'), is_active FROM members"
Use DATE_FORMAT:
SELECT id, username, DATE_FORMAT(join_date, "%d/%m/%y") AS date FROM members;
%Y Year, numeric, four digits
%y Year, numeric (two digits)
For details about date format link