Change Mysql Column date format and Perform Query - mysql

For Example
select date_format(date(credit_date),'%Y-%m-%d') from product where date(credit_date) >= date '2012-11-02' and date(credit_date) <= date '2013-11-02'
How to Fetch the Column From Date1 To Date2
And Db Structure is
+-----------------------------------------+
| credit_date |
+-----------------------------------------+
| 21/09/2013 |
| 22/09/2013 |
| 23/09/2013 |
| 24/09/2013 |
| 25/09/2013 |
| 26/09/2013 |
| 27/09/2013 |
| 28/09/2013 |
+-----------------------------------------+

It look like your credit_date is a varchar type with another date format, if so try this:
select date_format(str_to_date(credit_date,'%d/%m/%Y'), '%Y-%m-%d') as dt
from product
where str_to_date(credit_date,'%d/%m/%Y') >= '2012-11-02'
and str_to_date(credit_date,'%d/%m/%Y') <= '2013-11-02'
The default format for mysql is Y-m-d you don't need to convert it on the where clause. Unless your database is configured to have a specific format. As your credit_date seems to be a varchar you have to convert it first to date then test it.
See it here on fiddle: http://sqlfiddle.com/#!2/8b379/9

Related

How to replace and update all rows of table where varchar date yyyy-dd-mm to dd-mm-yyyy in mysql

I have a varchar column with date format as follows yyyy-mm-dd hh:mm:ss.0
transactions_table
+-----------------------+
| transaction_date |
+-----------------------+
| 2020-04-29 19:09:06.0 |
| 2020-05-13 19:09:06.0 |
How can I convert above records in dd-mm-yyyy and update the same record as follows
+-----------------------+
| transaction_date |
+-----------------------+
| 29-04-2020 |
| 13-05-2020 |
SQL code:
update transactions_table
set transaction_date ='manipulated data'
where transaction_date like '2020%';
How can I convert that string and save the same ?
With date_format() function:
select date_format(transaction_date, '%d-%m-%Y')
from transactions_table
where transaction_date like '2020%'
See the demo.
If you want to update the column:
update transactions_table
set transaction_date = date_format(transaction_date, '%d-%m-%Y')
where transaction_date like '2020%';
See the demo.
Results:
| transaction_date |
| ---------------- |
| 29-04-2020 |
| 13-05-2020 |
There is no need to convert the values of the column transaction_date to date, because this is done implicitly by MySql as the column has the format yyyy-mm-dd hh:mm:ss.0 which is a valid date format.
Simply update using a couple of built in functions
UPDATE transactions_table
SET transactions_date = DATE_FORMAT(DATE(transaction_date),'%d-%m-%Y');

Group by day using the timestamp field

My table schema looks like this:
+--------------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50 | NO | | 0 | |
| modified | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| created | timestamp | NO | | CURRENT_TIMESTAMP | |
+--------------------+--------------+------+-----+-------------------+-----------------------------+
I want to get a count of the names and group by number of names modified by day at the moment I can only group by the full date including the timestamp eg:
SELECT name, count(*) FROM mytable GROUP BY modified
Thanks in advance.
Use MySQL DATE() function to extract the date from the timestamp:
SELECT name, count(*) FROM mytable GROUP BY DATE(mytable.modified);
The DATE() function extracts the date value from a date or datetime expression.
Dependant upon what you mean by day, you have a few options;
Here's your cheat sheet.
DAYNAME(date) for the day of the week (Mon-Sun)
DAYOFMONTH(date) for the day of the month (1-31)
DAYOFWEEK(date) for the day of the week (1-7)
DAYOFYEAR(date) for the day of the year (1-365)
Edit:
If you want to group by the entire date (as opposed to a particular day), ignoring the time you can use
DATE_FORMAT(date, format)
The full list of format specifiers can be found at the above link, but what you'll probably need is:
Date_FORMAT(date, '%Y-%m-%d')
This will format the date as 'YYYY-MM-DD' and you can group by that.
You can use this query or a version of it
SELECT DATE_FORMAT(modified,"%Y-%m-%d") as date_string, count(1) FROM mytable group by date_string;

Add a constant date column in mysql

i have a table in mysql.
+------+---------+
|value | unit |
+------+---------+
| 2 | DAY |
| 3 | MONTH |
+------+---------+
this is just a part of my table. it consists of many rows
i want to add a date column to this table with a constant date. say '2009-01-01'
type of the column should be date.
+------------+------+---------+
| date |value | unit |
+------------+------+---------+
| 2009-01-01 | 2 | DAY |
| 2009-01-01 | 3 | MONTH |
+------------+------+---------+
i want to create table_2
create table table_2 as
select value,unit,`dates` DATE NOT NULL DEFAULT '2009-01-01' from table;
but getting syntax error.
any ideas how to do it?
Try like this:
CREATE TABLE table_2(
dates DATE
) SELECT '2009-01-01' AS dates, value,
unit FROM table_1
for more information Visit here: http://dev.mysql.com/doc/refman/5.5/en/create-table-select.html

varchar to datetime in mysql compare

i have a data like 'ddmmyyyy' like this format in my datebase and it datatype is also varchar(8) now i need to compare the date using between query
my table data is
| id | enquiry_date | dept_name |
|----------------------------------------|
| 1 | 02112004 | LAB |
| 2 | 31122005 | RESEARCH |
| 3 | 26052005 | LAB |
| 4 | 16042006 | RESEARCH |
Now i need to take records from 05-02-2005 to 12-11-2006 like that. here its varchar how to compare it please help me thanks in advance.
You can try using the STR_TO_DATE
STR_TO_DATE(enquiry_date,'%d%m%Y')
So you can compare like
where date_format(STR_TO_DATE(enquiry_date,'%d%m%Y'), '%d-%m-%Y') > '05-02-2005'
and date_format(STR_TO_DATE(enquiry_date,'%d%m%Y'), '%d-%m-%Y') < '12-11-2006'

Where clause containing date in MySQL statement not working

Table Name: DemoTable.
Total Fields: 2
Fields:
id (int, auto increment, primary key)
month_and_year (varchar(10))
month_and_year contains date as '2015-03', '2015-01', '2014-12' and so on...
I am trying to get values from the table between '2014-10' and '2015-03'.
SELECT * FROM DemoTable where month_and_year>='2014-10' AND month_and_year<='2015-03' ORDER BY month_and_year DESC
Query does not give desired output as month_and_year field has varchar data type. Changing varchar to date data type isn't possible as date data type does not accept date in 'yyyy-mm' format.
How can the result be obtained?
PS:Is UNIX_TIMESTAMP() a safe bet in this case?
You should never store date value as varchar and choose mysql native date related data types like date,datetime or timestamp
However in your case you need to do some date related calculations before doing the select query. Consider the following table
mysql> select * from test ;
+------+----------------+
| id | month_and_year |
+------+----------------+
| 1 | 2014-10 |
| 2 | 2014-10 |
| 3 | 2014-09 |
| 4 | 2014-11 |
| 5 | 2015-01 |
| 6 | 2014-08 |
+------+----------------+
Now the approach would as
First convert the varchar to real date
Then for the lower limit always start the comparison from first day of the year month value
The upper limit will be till the end of the month.
So the query becomes
select * from test
where
date_format(
str_to_date(
month_and_year,'%Y-%m'
),'%Y-%m-01'
)
>=
date_format(
str_to_date('2014-10','%Y-%m'
),'%Y-%m-01'
)
and
last_day(
date_format(
str_to_date(month_and_year,'%Y-%m'
),'%Y-%m-01'
)
)
<=
last_day(
date_format(
str_to_date('2015-03','%Y-%m'
),'%Y-%m-01'
)
);
The output will be as
+------+----------------+
| id | month_and_year |
+------+----------------+
| 1 | 2014-10 |
| 2 | 2014-10 |
| 4 | 2014-11 |
| 5 | 2015-01 |
+------+----------------+
Use the function STR_TO_DATE(string,format);
http://www.mysqltutorial.org/mysql-str_to_date/
You should use either mysql date time functions or use int field in mysql and store UNIXTIMESTAMP and compare like you are already doing. I think it is overkill to store unixtimestamp because you only need month and year and you won't benefit a lot from unixtimestamp advantages.