添加分隔符

时间:2012-08-08 17:08:14

标签: asp.net sql-server-2005 stored-procedures

我目前有一个Storedproc t hat正在返回值,以便我在asp.net中与gridview绑定。我想要做的一件事是将两个字段组合成一个由/分隔的字段,但我似乎无法使其正常工作。

这是我的存储过程

DECLARE @SQL VARCHAR(MAX);

BEGIN
SET @SQL = N'SELECT SYSTEM AS ''System''
            , ENTN AS ''Entity''
            , MED_CTR AS ''Medical Center''
            , MED_CTR_LEVEL AS ''Medical Center Level''
            , DEPT_ID AS ''Department ID''
    FROM V_ROLLUP_SYSTEM_DESC_REV1
    WHERE (DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
        AND SYSTEM IN ('+@SYS+')'
PRINT(@SQL)
EXEC(@SQL)

所以我想将MED_CTR和MED_CTR_LEVEL分开,用“/”分隔并标记为医疗中心/水平。到目前为止我似乎无法将它组合起来,因为我尝试在其中添加“/”的每种方法都没有给我任何回报。

1 个答案:

答案 0 :(得分:1)

这应该有效:

SET @SQL = N'SELECT SYSTEM AS [System]
            , ENTN AS [Entity]
            , (MED_CTR +''/''+ MED_CTR_LEVEL) as [Medical Center/ Level]
            , DEPT_ID AS [Department ID]
    FROM V_ROLLUP_SYSTEM_DESC_REV1
    WHERE (DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
        AND SYSTEM IN ('+@SYS+')'

如果你想确保2列的连续总是成功,你可以这样做:

SET @SQL = N'SELECT SYSTEM AS [System]
            , ENTN AS [Entity]
            , ISNULL(MED_CTR,''N/A'') +''/''+ ISNULL(MED_CTR_LEVEL,''N/A'') ) as [Medical Center/ Level]
            , DEPT_ID AS [Department ID]
    FROM V_ROLLUP_SYSTEM_DESC_REV1
    WHERE (DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
        AND SYSTEM IN ('+@SYS+')'