The following query produces a date that looks like this 2016, 01, 02. How do I get it to remove trailing zeros from the month and day so that it looks like this 2016, 1, 2?
SELECT DATE_FORMAT(earning_created, '%Y, %m, %d') AS day, SUM(earning_amount) AS amount
FROM earnings
WHERE earning_account_id = ?
GROUP BY DATE(earning_created)
ORDER BY earning_created
You can use %c to format the month without the leading zero and %e to format the day of the month:
SELECT DATE_FORMAT(earning_created, '%Y, %c, %e') AS day, -- Here!
SUM(earning_amount) AS amount
FROM earnings
WHERE earning_account_id = ?
GROUP BY DATE(earning_created)
ORDER BY earning_created
Related
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
I am trying to SELECT the date from my database in the date_format '%e %M %Y':
SELECT date_format(date, '%e %M %Y') AS date FROM table
The output of the date will be like: 1 january 2016.
When I add an ORDER BY to the query I the date will not sort on the date, but it sorts on alphabetical order.
Does someone know how I can fix this problem and sort column: date on the date order?
Here is my full statement:
SELECT date_format(date, '%e %M %Y') AS date FROM table ORDER BY date DESC
You are using a date as alias for column name. Just use another one:
SELECT date_format(date, '%e %M %Y') AS formatted_date
FROM table
ORDER BY date DESC;
I want to get count of all assessments in past three months. So this should count for current and previous two months as separate
like Count1 50 for this month
Count2 100 for one month back
Count2 50 for two month back
SELECT Count(AssessmentID)
from AssessmentListing
WHERE (STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p') >=
DATE_FORMAT(NOW(), '%Y' ))
You should not be storing dates as strings. If you want the current month and two months back, then something like this:
SELECT DATE_FORMAT(STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p'), '%Y-%m') as yyyymm,
Count(*)
from AssessmentListing
WHERE STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p') >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 1 - DAY(CURDATE()), INTERVAL 2 MONTH)
GROUP BY yyyymm;
I am not sure what your WHERE clause actually does. But comparing a string and a date is probably not what you really want.
Like #Jarlh mentioned in comment you can use Extract function to extract the month from the date column and group the result using that.
select EXTRACT(MONTH FROM AssessmentSubmittedDatetime) as _month,
Count(AssessmentID)
from AssessmentListing
WHERE (STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p') >=
DATE_FORMAT(NOW(), '%Y' ))
group by _month
I have a column 'birthDate' which returns a string value of [Day of the Week], [Month] [Day], [Year]
(e.g. Monday, December 30, 2013)
I am using SELECT STR_TO_DATE(birthDate, '%m/%d/%Y') but it returns a null value.
Any ideas?
SOLUTION
SELECT STR_TO_DATE(birthDate, '%W, %M %d, %Y')
You're showing one format of your date ([Day of the Week], [Month] [Day], [Year]) but using another in STR_TO_DATE ([Month]/[Day]/[Year]). You need to provide STR_TO_DATE with the format your column is currently in:
SELECT STR_TO_DATE(birthDate, '%W %m %d %Y')
If you want that result to be in a new format you can then use DATE_FORMAT():
SELECT DATE_FORMAT(STR_TO_DATE(birthDate, '%W %m %d %Y'), '%m/%d/%Y')
Here's the table structure and some sample data:
pID.....month.....year
27 .....3 .....2008
27 .....12 .....2012
31 .....6 .....2008
99 .....1 .....2006
42 .....1 .....2009
pID is the practiceID and month and year represent the date period they've entered data for. I need to grab the number of practices that have entered data for the first time in Oct 2012, Nov 2012, Dec 2012 and so on.
I tried the following query for Oct 2012:
SELECT *
FROM
IPIPKDIS
where
practiceID NOT IN (
SELECT practiceID
from
IPIPKDIS
where
year < 2012 and month < 10
)
and year = 2012
and month = 10
and measureCatRecID = 2
ORDER BY year, month;
but it's grabbing months and year less than 10/2012.
If I run the queries isolated (not as subquery) they both work fine.
Any ideas?
This summary query will yield the first (smallest) date in the table for each value of practiceID.
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
If you want to retrieve then the whole row for the first reported month, you'd do a nested query like this:
SELECT *
FROM IPIPKDIS I
JOIN (
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
) first ON ( first.practiceID = I.practiceID
AND STR_TO_DATE( CONCAT(I.year, ' ', I.month), '%Y %m') = first.first_date)
The trick to the second query is to use the JOIN to extract just the first-month rows from your table. We use date arithmetic to do the date comparisons.