?
Solved

sql date quarter

Posted on 2011-02-10
5
Medium Priority
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

777 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