I have different dates in a column. For example:
20080102 20070821
I want to convert these dates in Year and calendar quarter. E.g.,
Year Quarter 2008 2008-Q1 2007 2007-Q3
I can get the first column with:
select left(date,4) as year from table
How can I produce the second column?
QUARTER() function in MySQL is used to return the quarter of the year for a given date value. It returns a number from 1 to 4. date : The date or DateTime from which we want to extract the quarter.
CREATE TABLE Quarter(ID INT, [Desc] varchar(100), QuarterStart DATETIME, QuarterEnd DATETIME); INSERT INTO Quarter(QuarterStart, QuarterEnd, [Desc]) VALUES ('Jan 1 2012', 'Mar 31 2012', 'Q1 2012'); INSERT INTO Quarter(QuarterStart, QuarterEnd, [Desc]) VALUES ('Apr 1 2012', 'Jun 30 2012', 'Q2 2012');
SELECT DATEPART(QUARTER, @date)
This returns the quarter of the @date
, assuming @date
is a DATETIME
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With