MySQL Date_Format with Month() in query - mysql

I need to extract the month from the date in the database but I need to format it to became a word-written month.
Sample Date in the Database
2017-11-22
Month Query
SELECT MONTH(createdAt) FROM tbltest
The output of this is the numerical month of the createdAt ex: 11
Month with Date_Format Query <----- not working
SELECT DATE_FORMAT(MONTH(createdAt), '%Y-%b-%d') as title
FROM tbltest
Output that I am looking for
Nov

SELECT DATE_FORMAT(createdAt, '%b') as monthShortName
FROM tbltest;
Pls refer : https://www.w3resource.com/mysql/date-and-time-functions/mysql-date_format-function.php

You may try MONTHNAME function
SELECT MONTHNAME(createdAt) FROM tbltest
For short month name you can use
select DATE_FORMAT(createdAt, '%b') from tbltest

Related

SQL get dates later than specified date

I'm working on a database that stores a date column in a human-readable format. This seems to make it tricky to work out rows where a given date is later than.
The format is stored like 20 November, 2018
I'm trying to return all rows where the collection_date is later than 5 November, 2018
I've tried the following query which throws an error.
SELECT *
FROM `orders`
WHERE collection_date >= CONVERT(datetime, '20181105')
This throws the following error message:
Here's my DB info:
UPDATE 1:
I'm trying the following query. None of the previous queries have worked so far, all producing 0 rows:
SELECT *
FROM `orders`
WHERE STR_TO_DATE('collection_date', '%d %M, %Y') >= 2018-11-05
This also does not work
Actually... you have to apply STR_TO_DATE on the collection_date column because that is where the human readable dates are. As for the input date, just specify it in yyyy-mm-dd format:
SELECT *
FROM `orders`
WHERE STR_TO_DATE(collection_date, '%e %M, %Y') >= '2018-11-05'
In MySQL, you want STR_TO_DATE():
SELECT o.*
FROM `orders` o
WHERE collection_date >= Str_To_Date(datetime, '%e %M, %Y');
CONVERT() with this syntax is a SQL Server function.
The value comes first in the convert() function
WHERE collection_date >= CONVERT('2018-11-05', date)
Demo
You can try this using STR_TO_DATE() function
SELECT *
FROM `orders`
WHERE collection_date >= STR_TO_DATE('20181105', '%Y%m%d')
This query worked for me:
SELECT *
FROM orders
WHERE STR_TO_DATE(collection_date, '%d %M, %Y') >= '2018-11-05'
AND collection_time = '4:00 PM'
ORDER BY `orders`.`collection_date` DESC

How to get 2 periods of dates in sql

I want to subtract 2 periods of dates to compare data of current year vs previous year and my query is not working.
If I remove a period it works, but not sure how to pull 2 periods.
Thanks
SELECT year(date) as year, WEEK(ADDDATE(date, 5-DAYOFWEEK(date)), 3) AS 'Week', DATE,
FROM TABLE
WHERE year(date) in (2015, 2016)
AND (DATE BETWEEN "2016-10-23" AND "2016-11-12")
AND (DATE BETWEEN "2015-11-06" AND "2015-11-12")
ORDER BY DATE, year(date)
Please try this one.
SELECT year(date) as year, WEEK(ADDDATE(date, 5-DAYOFWEEK(date)), 3) AS 'Week', DATE,
FROM TABLE
WHERE
((DATE BETWEEN "2016-10-23" AND "2016-11-12")) OR
((DATE BETWEEN "2015-11-06" AND "2015-11-12"))
ORDER BY DATE, year(date)
I think you want or:
SELECT year(date) as year, WEEK(ADDDATE(date, 5-DAYOFWEEK(date)), 3) AS Week, DATE
FROM TABLE
WHERE DATE BETWEEN '2016-10-23' AND '2016-11-12' OR
DATE BETWEEN '2015-11-06' AND '2015-11-12';
Notes:
You don't need the date() comparison in the where clause.
You should use single quotes for date and string constants.
You should not use single quotes for column aliases.
SELECT year(DATE) AS year
,WEEK(ADDDATE(DATE, 5 - DAYOFWEEK(DATE)), 3) AS 'Week'
,DATE
FROM TABLE
WHERE (
(
DATE BETWEEN '2016-10-23'
AND '2016-11-12'
)
OR (
DATE BETWEEN '2015-11-06'
AND '2015-11-12'
)
)

How do I execute the following query?

I'm using mysql. I have a table called orders with the fields orderid, orderdate, customerid, shippedcity and amount.
I want to show orders for the month of June and year 2011. Please help.
You can use BETWEEN as below:
SELECT *
FROM ORDER
WHERE ODERDATE BETWEEN DATE ('01-06-2011') AND DATE ('30-06-2011');
this one is fairly easy :-)
SELECT *
FROM orders
WHERE MONTH(orderdate) = 6
AND YEAR(orderdate) = 2011
For better performance of the query you could use this one
SELECT *
FROM orders
WHERE orderdate BETWEEN '2011-06-01 00:00:00' AND '2011-06-30 23:59:59'
Cheers -Sven
Try this one,
SELECT *
FROM tableName
WHERE MONTH(orderdate) = 6 AND
YEAR(orderDate) = 2011
for reference,
MONTH()
YEAR()
well, first of all i have to ask, does the orderdate field contains full date (dd/mm/yyyy)? or how is the date stored?
you could try SELECT orderid FROM orders WHETE orderdate = 'your date in the correct format it is stored'

How to group by date regardless of time?

I'm trying to group by Date(). I've got 3 records with the created_at column:
2011-12-03 08:00:24, 2011-12-03 08:12:10, 2011-12-04 09:00:00
I'd like to only group by year, month and day, regardless of time. So for the example above. It should only return two rows:
2011-12-03 and 2011-12-04
How should I go about this?
... group by date(date_time_column)
This should allow you to group by year month and day
SELECT group_by_column
, DATE_FORMAT(created_at, '%Y')
, DATE_FORMAT(created_at, '%m')
, DATE_FORMAT(created_at, '%d')
FROM my_table
GROUP BY group_by_column
or if you want to do them all together.
SELECT group_by_column
, DATE_FORMAT(created_at, '%Y%m%d')
FROM my_table
GROUP BY group_by_column
Did you try the following?
SELECT
DATE(created_at) AS created_date
FROM
my_table
GROUP BY
created_date
MySQL permits GROUP BY DATE(created_at). So that would translate in ActiveRecord to .group(DATE(created_at))
In fact, that exact example is available in the Rails Guides on ActiveRecord querying.
You can use the Date() function.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date
mysql> SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31'
TRY
GROUP BY DATE(`date_column`)
Reference

get all months last days in mysql

For example I have a table with fields:
id date
1 2001-01-01
2 2001-01-05
.................
N 2011-12-31
How get i get all months last days from this table?
example:
if i have dates like 2001-05-31 and 2001-06-01
i need only 2001-05-31 not both
You can do SELECT LAST_DAY for example the below returns Oct. 31st. 2010
SELECT LAST_DAY('2010-10-10');
select max(date_format(date, '%d')) as last_day_of_the_month
from table
group by date_format(date, '%Y%m')
maybe this would work better?
select DISTINCT(LAST_DAY(date)) from table GROUP BY date_format(date, '%Y%m')
SELECT id, date
FROM `table`
WHERE DATE( date ) = LAST_DAY( date )
The DATE function over field is for filter the date without time, use only if you have a datetime column.
The query get all rows with date = last day of month.
select subdate(adddate(subdate(`date`, day(`date`) - 1), interval 1 month), 1)
Note: This is the "hard way". See #harper89's answer - it's better :)
I found a solution. but this query is very slow on large tables. so I am still looking for a better solution
select DISTINCT(LAST_DAY(date)) from table;
select max(date)
from table
group by year(date), month(date)