Solved

sql date quarter

Posted on 2011-02-10
5
346 Views
Last Modified: 2012-06-27
See my code below.. It's producing a record for each month in the QTR.. I only want one record per QTR.. The results set is supposed to look like

Q1 2011
Q2 2011

NOT
Q1 2011
Q1 2011
Q1 2011

and so on
(SELECT 'Q' + DATENAME(Q, @DateListedMLS) + CAST ('  ' AS VARCHAR(1)) + CAST(DATEPART(YY, @DateListedMLS) AS VARCHAR(200)))

Open in new window

0
Comment
Question by:cheryl9063
  • 2
  • 2
5 Comments
 
LVL 1

Author Comment

by:cheryl9063
ID: 34864507
I found the issue.. But I still need to know this:

This gives me the current QTR.. Based on the current date I want the next QTR.. How can I use below to get the next QTR? For example.. The below gives me 1 and I want 2.. In reality I will have a variable with a sale date like 02/11/2010.. I want to use that variable but get the QTR after that… Not QTR1 but QTR2..

select DATENAME(Q, GETDATE())
0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34864858
this gives you the next quarter date:  SELECT DATEADD(Q, 1, GETDATE())
therefore, the answer is:  select DATENAME(Q, DATEADD(Q, 1, GETDATE()))
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34865531
Same result as jason but more brief

select DatePart(Q, GETDATE()) % 4 + 1
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34865578
To get the entire Q + Y..

declare @DateListedMLS datetime set @DateListedMLS = GETDATE()
select 'Q' + Right(DatePart(Q, GETDATE()) % 4 + 1,1) +
	' ' + convert(char(4),DATEADD(M,3,@DateListedMLS),102)

Open in new window

0
 
LVL 1

Author Closing Comment

by:cheryl9063
ID: 34964418
Thanks!!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now