带有LEFT和LEN的SQL SELECT

时间:2014-11-06 19:05:39

标签: sql sql-server-2008-r2 sql-function

我的PRJ_NAME结果为“ Authoriazations - XXXXXX-XXXXXX ”,但我想要的只是“授权”,因此我使用此查询并取得圆满成功。< / p>

SELECT  ISNULL(LEFT(PRJ_NAME, LEN(PRJ_NAME)-16),'') AS 'Folder Name'
FROM    MHGROUP.PROJECTS 
WHERE   PRJ_ID = '3626747'

现在,当我将以下查询与基本联接一起使用时,我无法在此查询中使用此逻辑。

SELECT  m.[DOCNAME], 
        m.[SUBCLASS_ALIAS],
        u.FULLNAME,
        m.[CDATE3] as 'File Date',
        m.[DECLAREWHEN] as 'Declared',
        ISNULL(LEFT(p.PRJ_NAME, LEN(p.PRJ_NAME)-16),'') as 'Folder Name',
        m.ENTRYWHEN
FROM    ([Imanage].[MHGROUP].[DOCMASTER] as m (nolock)
JOIN    [Imanage].[MHGROUP].[DOCUSERS] as u (nolock) on u.USERID = m.OPERATOR), 
        [Imanage].[MHGROUP].[PROJECTS] as p (nolock), 
        [Imanage].[MHGROUP].[PROJECT_ITEMS] as i (nolock)
WHERE   i.ITEMTYPE = 'D' and
        i.ITEM_ID = m.DOCNUM and 
        i.PRJ_ID = p.PRJ_ID
ORDER BY u.FULLNAME, p.PRJ_NAME 

我收到以下错误...

  

消息537,级别16,状态2,行2传递给的长度参数无效   LEFT或SUBSTRING功能。

我似乎无法想到可能导致这种情况的原因。任何协助或指导将不胜感激。

1 个答案:

答案 0 :(得分:3)

PRJ_NAME列中的值小于16个字符。

与以下简单查询相同的错误

select LEFT('abc', LEN('abc')-16)

检查是否有任何此类名称并使用案例陈述处理

case when len(PRJ_NAME) > 16 
     then LEFT(p.PRJ_NAME, LEN(p.PRJ_NAME)-16) 
     ELSE prj_name 
END