Solved

extracting and displaying year from datatype datetime in sql server 2005

Posted on 2011-09-07
12
239 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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.…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now