檢查IsNumeric可能會對您有所幫助 - 您可以簡單地返回零值。如果你想返回'N/a'或其他字符串值
我使用查詢中的列創建了下面的示例。
第一個查詢只是返回所有行。
第二個查詢返回一個MONEY值。
第三個用N/A代替非整數值返回一個String值。
set nocount on
drop table #MoneyTest
create table #MoneyTest
(
MoneyTestId Int Identity (1, 1),
coment varchar (100),
member_id int,
eff_date datetime,
term_date datetime
)
insert into #MoneyTest (coment, member_id, eff_date, term_date)
values
(104, 1, '1/1/2008', '1/1/2009'),
(200, 1, '1/1/2008', '1/1/2009'),
(322, 1, '1/1/2008', '1/1/2009'),
(120, 1, '1/1/2008', '1/1/2009')
insert into #MoneyTest (coment, member_id, eff_date, term_date)
values ('XX', 1, '1/1/2008', '1/1/2009')
Select *
FROM #MoneyTest
declare @intMemberId int = 1
declare @dtmBeginDate DateTime = '1/1/2008'
declare @dtmEndDate DateTime = '1/1/2009'
SELECT
CASE WHEN ISNUMERIC (Coment)=1 THEN CAST(#MoneyTest.coment AS money) ELSE cast (0 as money) END MoneyValue
FROM #MoneyTest
WHERE member_id = @intMemberId
AND #MoneyTest.eff_date <= @dtmEndDate
AND
(
#MoneyTest.term_date >= @dtmBeginDate
OR
#MoneyTest.term_date Is Null
)
SELECT
CASE WHEN ISNUMERIC (Coment)=1 THEN CAST (CAST(#MoneyTest.coment AS money) AS VARCHAR) ELSE 'N/a' END StringValue
FROM #MoneyTest
WHERE member_id = @intMemberId
AND #MoneyTest.eff_date <= @dtmEndDate
AND
(
#MoneyTest.term_date >= @dtmBeginDate
OR
#MoneyTest.term_date Is Null
)
美麗!像魅力一樣工作!非常感謝! – 2009-08-04 15:59:26