Solved

sql date quarter

Posted on 2011-02-10
5
348 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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