Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

extracting and displaying year from datatype datetime in sql server 2005

Posted on 2011-09-07
12
Medium Priority
?
250 Views
Last Modified: 2012-05-12
Hi,
I have a piece of code  in sql server 2005
SELECT Name,
count(case when month(RESOLVED_DATE) = 1
AND PROD_CAT1='Hardware'
then Server_Name end) Jan_Hw,
count(case when month(RESOLVED_DATE) = 1
AND PROD_CAT1='Software'
then Server_Name end) Jan_Sw,

count(case when month(RESOLVED_DATE) = 2
AND PROD_CAT1='Hardware'
then Server_Name end) Feb_Hw,
count(case when month(RESOLVED_DATE) = 2
AND PROD_CAT1='Software'
then Server_Name end) Feb_Sw,

that returns Table A
I would like it to display the year as well as the month
As in Table A2

RESOLVED_DATE is of data type DATETIME

any advice appreciated...thanks
 cast
0
Comment
Question by:blossompark
[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
  • 7
  • 3
  • 2
12 Comments
 
LVL 35

Accepted Solution

by:
Norie earned 1200 total points
ID: 36496758
You could use DATEPART.

DATEPART(year, RESOLVED_DATE)
0
 
LVL 35

Assisted Solution

by:Norie
Norie earned 1200 total points
ID: 36496776
Or you could of course use YEAR.
0
 

Author Comment

by:blossompark
ID: 36496808

Hi Imnorie, thanks for that

have tried the following
AND PROD_CAT1='Software'
then Server_Name end) Jan_Sw +'('+ year(RESOLVED_DATE)' )',

but getting syntax error,
Incorrect syntax near '+'.



0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 35

Assisted Solution

by:Norie
Norie earned 1200 total points
ID: 36496886
You've got a missing:

Jan_Sw +'('+ year(RESOLVED_DATE) +  ')',
0
 

Author Comment

by:blossompark
ID: 36496981
still
incorrect syntax near '+'
0
 

Author Comment

by:blossompark
ID: 36497136
this code giving the error;

count(case when month(RESOLVED_DATE) = 1
AND PROD_CAT1='Software'
then Server_Name end)
Jan_Sw +'('+ year(RESOLVED_DATE) +  ')',
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 800 total points
ID: 36497859
Do you want to change the column names only from Jan_Hw to Jan-11_Hw? Do you have data for 2011 only in your table?
If yes, you can try this.
count(case when month(RESOLVED_DATE) = 1 
AND PROD_CAT1='Hardware' 
then Server_Name end) [Jan-11_Hw],
count(case when month(RESOLVED_DATE) = 1 
AND PROD_CAT1='Software' 
then Server_Name end) [Jan-11_Sw],

count(case when month(RESOLVED_DATE) = 2 
AND PROD_CAT1='Hardware' 
then Server_Name end) [Feb-11_Hw],
count(case when month(RESOLVED_DATE) = 2 
AND PROD_CAT1='Software' 
then Server_Name end) [Feb-11_Sw],

Open in new window

0
 

Author Comment

by:blossompark
ID: 36498654
Hi Sharath,
thanks for that,
                 the data is not only from 2011, it can be 12 months previous to when the query is run
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 800 total points
ID: 36499048
Can you provide some sample data with expected result?
0
 

Author Comment

by:blossompark
ID: 36501735
Hi Sharath 123,
Hi,
    The table below is an extract from the output of a local table  as displayed in sql server management studio...

The existing code  below is an extract of the code fired against this  local table..
The attached excel file has two tabs, the existing output I get from the query below
and the output i am trying to achieve...
Please let me know if you require any further information...
thanks

SELECT       
 Server_Name,
count(case when month(RESOLVED_DATE) = 1
AND PROD_CAT1='Hardware'
then Server_Name end) January_Hardware,
count(case when month(RESOLVED_DATE) = 1
AND PROD_CAT1='Software'
then Server_Name end) January_Software,
  localTabl beAftxls.xls
0
 

Author Comment

by:blossompark
ID: 36503410
Hi Sharath 123,
I think i may have solved this using pivot .....just running now...will update later
0
 

Author Closing Comment

by:blossompark
ID: 36509565
Hi sharath123 and imnorie,
i'm using the pivot option currently to resolve this... its not 100% working so i may open  a question regarding the outstanding issue if i cannot resolve it...
thanks for your help, much appreciated
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.

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.​
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

609 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