日期:2014-05-18  浏览次数:20609 次

sql 数据查询问题
小弟有如下这条 Sql 语句 
F_ISNULLOREMPTY 为 sql 函数函数

ALTER FUNCTION [dbo].[F_ISNULLOREMPTY](@check_expression SYSNAME, @replacement_value SYSNAME) RETURNS SYSNAME
AS
BEGIN
IF(@check_expression IS NULL OR LEN(@check_expression) = 0) RETURN @replacement_value
RETURN @check_expression
END

SELECT dbo.F_ISNULLOREMPTY(COUNT(ID) ,0) AS num FROM V_TG_Client_Success 
WHERE AgentID = 1 AND CONVERT(VARCHAR(4), RegDate, 120) = CONVERT(VARCHAR(4), GETDATE(), 120)
AND MONTH(RegDate) IN (1,2,3,4,5,6,7,8,9,10,11,12)

请问如何显示 12 条数据 如果查询不到 以 0 代替
但为何只能查询一条

------解决方案--------------------
SQL code
SELECT
 sum(case when id is not null or LEN(id)) = 0 then 0 else 1 end) as num
FROM
 V_TG_Client_Success  
WHERE
 AgentID = 1 
AND
 CONVERT(VARCHAR(4), RegDate, 120) = CONVERT(VARCHAR(4), GETDATE(), 120)
AND
 MONTH(RegDate) IN (1,2,3,4,5,6,7,8,9,10,11,12)

------解决方案--------------------
SQL code
SELECT
 sum(case when id is not null or LEN(id)) = 0 then 0 else 1 end) as num
FROM
 V_TG_Client_Success  
WHERE
 AgentID = 1 
AND
 CONVERT(VARCHAR(4), RegDate, 120) = CONVERT(VARCHAR(4), GETDATE(), 120)
AND
 MONTH(RegDate) IN ('1','2','3','4','5','6','7','8','9','10','11','12')