Solved

extracting and displaying year from datatype datetime in sql server 2005

Posted on 2011-09-07
12
247 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 34

Accepted Solution

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

DATEPART(year, RESOLVED_DATE)
0
 
LVL 34

Assisted Solution

by:Norie
Norie earned 300 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 34

Assisted Solution

by:Norie
Norie earned 300 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 200 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 200 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

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.​
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.
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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