Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

sql date quarter

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
cheryl9063
Asked:
cheryl9063
  • 2
  • 2
1 Solution
 
cheryl9063Author Commented:
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
 
jasonduanCommented:
this gives you the next quarter date:  SELECT DATEADD(Q, 1, GETDATE())
therefore, the answer is:  select DATENAME(Q, DATEADD(Q, 1, GETDATE()))
0
 
cyberkiwiCommented:
Same result as jason but more brief

select DatePart(Q, GETDATE()) % 4 + 1
0
 
cyberkiwiCommented:
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
 
cheryl9063Author Commented:
Thanks!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now