Solved

extracting and displaying year from datatype datetime in sql server 2005

Posted on 2011-09-07
12
246 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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