Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Case statement error

Author  Topic 

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2009-10-08 : 10:45:56
Hi All,
I am using this query

SELECT
CASE WHEN DATEDIFF(MONTH, '2009-01-10','2010-10-01') > 1 THEN 'GREATER THAN 12 MONTHS'
ELSE DATEDIFF(MONTH,'2009-01-10','2010-10-01') END AS 'Age in TES',
CASE WHEN DATEDIFF(MONTH,'2009-01-10','2010-10-01') > 1 THEN 'GREATER THAN 12 MONTHS'
ELSE DATEDIFF(MONTH,'2009-01-10','2010-10-01') END AS 'Age Since Service Date'

but i am getting error "Conversion failed when converting the varchar value 'GREATER THAN 12 MONTHS' to data type int."

Pls help

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-08 : 10:50:45
SELECT
CASE WHEN DATEDIFF(MONTH, '2009-01-10','2010-10-01') > 1 THEN 'GREATER THAN 12 MONTHS'
ELSE cast(DATEDIFF(MONTH,'2009-01-10','2010-10-01') as varchar(10)) END AS 'Age in TES',
CASE WHEN DATEDIFF(MONTH,'2009-01-10','2010-10-01') > 1 THEN 'GREATER THAN 12 MONTHS'
ELSE cast(DATEDIFF(MONTH,'2009-01-10','2010-10-01') as varchar(10)) END AS 'Age Since Service Date'


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2009-10-08 : 11:09:27
Thanks a lot madhavanan..
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-09 : 01:51:04
quote:
Originally posted by gangadhara.ms

Thanks a lot madhavanan..


Note the change I made. All the values should be same in datatype

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -