Solved

extracting and displaying year from datatype datetime in sql server 2005

Posted on 2011-09-07
12
244 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
  • 7
  • 3
  • 2
12 Comments
 
LVL 33

Accepted Solution

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

DATEPART(year, RESOLVED_DATE)
0
 
LVL 33

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 33

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 40

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 40

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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