我正在尝试使用UI连接两个表,但是其中一个表中的UI之前带有“ CLIENT_”。尝试从字符串中删除“ CLIENT_”然后比较UI时,出现以下错误。
将数据类型nvarchar转换为float时出错。
当尝试将整个子查询转换或转换为INT或FLOAT时,会发生相同的错误。
有没有人有任何想法或知道我该如何编码才能使它起作用。
SELECT DISTINCT 69 AS 'FIELD_ID',
ENTITY_ID,
AD.WIP AS 'WIP'
FROM [WORKVIEW].[DBO].ENTITY_MASTER EM
JOIN [LIVE].[MHGROUP].PROJECTS P ON P.PRJ_ID = EM.ENTITY_PARENT
JOIN [WORKVIEW].[DBO].adn_NAMEACCOUNTSDATA_STAGING AD ON
CAST((SELECT SUBSTRING(P.CUSTOM1, CHARINDEX ('_', P1.CUSTOM1) -1, LEN(P1.CUSTOM1))
FROM [IMAN-SQL].[LIVE].[MHGROUP].PROJECTS P1
WHERE P.CUSTOM1 LIKE 'CLIENT_%') AS INT) = AD.NAMENO
我希望能够在P.CUSTOM1和AD.NAMENO上加入NAMEACCOUNTSDATA_STAGING,因为这是我可以将这些表一起加入的唯一字段。
进行这项工作的最新尝试。...
SELECT DISTINCT 69 AS 'FIELD_ID',
ENTITY_ID,
AD.WIP AS 'WIP'
FROM [WORKVIEW].[DBO].ENTITY_MASTER EM
JOIN [IMAN-SQL].[LIVE].[MHGROUP].PROJECTS P ON P.PRJ_ID = EM.ENTITY_PARENT
JOIN [WORKVIEW].[DBO].WORKVIEW_NAMEACCOUNTSDATA_STAGING AD ON CONCAT('CLIENT_', AD.NAMENO) = P.CUSTOM1 AND P.CUSTOM1 LIKE 'CLIENT_%'
涉及的所有三个表的架构:
TABLE [dbo].[entity_master](
[entity_id] [int] IDENTITY(1,1) NOT NULL,
[entity_parent] [nvarchar](50) NOT NULL,
[entity_child] [nvarchar](50) NOT NULL,
[entity_status] [int] NOT NULL,
[entity_child_name] [nvarchar](250) NULL,
[entity_parent_name] [nvarchar](250) NULL,
[GUID] [varchar](50) NULL,
TABLE [dbo].[WORKVIEW_NAMEACCOUNTSDATA_STAGING](
[NAMENO] [int] NOT NULL,
[WIP] [decimal](18, 2) NULL,
[BILLED] [decimal](18, 2) NULL,
[WRITTENOFF] [decimal](18, 2) NULL,
[DEBTORRESTRICTION] [nvarchar](max) NULL,
[PAYMENTTERMS] [int] NULL,
[BILLING FREQUENCY] [nvarchar](max) NULL
TABLE [MHGROUP].[PROJECTS](
[PRJ_ID] [float] NOT NULL,
[PRJ_PID] [float] NULL,
[DEFAULT_SECURITY] [char](1) NOT NULL,
[IS_SECURED] [char](1) NOT NULL,
[PRJ_NAME] [nvarchar](254) NULL,
[PRJ_OWNER] [nvarchar](64) NULL,
[PRJ_DESCRIPT] [nvarchar](254) NULL,
[PRJ_STATE] [char](1) NULL,
[PRJ_PUBLIC] [char](1) NULL,
[PRJ_LOCATION] [nvarchar](254) NULL,
[TYPE] [int] NOT NULL,
[SUBTYPE] [nvarchar](64) NULL,
[INHERITS_SECURITY] [char](1) NOT NULL,
[DOCNUM] [float] NULL,
[VERSION] [int] NULL,
[CUSTOM1] [nvarchar](254) NULL,
[CUSTOM2] [nvarchar](254) NULL,
[CUSTOM3] [nvarchar](254) NULL,
[LEFT_VISIT] [int] NULL,
[RIGHT_VISIT] [int] NULL,
[TREE_ID] [int] NULL,
[EMAIL] [nvarchar](254) NULL,
[DOC_SAVED_SEARCH] [int] NULL,
[LAYOUT_NAME] [nvarchar](254) NULL,
[LAYOUT_ORDER] [int] NULL,
[LAYOUT_VIEW] [nvarchar](max) NULL,
[REFERENCE_DATABASE] [nvarchar](32) NULL,
[REFERENCE_PRJ_ID] [float] NULL,
[REFERENCE_TYPE] [int] NULL,
[REFERENCE_SUBTYPE] [nvarchar](64) NULL,
[IS_EXTERNAL] [char](1) NOT NULL,
[EXTRNL_AS_NRML] [char](1) NOT NULL,
[IS_DOC_SVD_SRCH] [char](1) NOT NULL,
[IS_PRJ_SVD_SRCH] [char](1) NOT NULL,
[IS_HIDDEN] [char](1) NOT NULL,
[EDITWHEN] [datetime] NOT NULL,
希望这就是您想要的。
答案 0 :(得分:0)
您是否测试了子字符串?在测试中我得到了
将nvarchar值'T_1456'转换为数据类型int时转换失败。
因此,子字符串将获得下划线的charindex,然后您将删除一个字符以获取无法转换为INT的T_。改为加号而不是减号将获得有效的联接。
CAST((SELECT SUBSTRING(P.CUSTOM1, CHARINDEX ('_', P1.CUSTOM1) +1, LEN(P1.CUSTOM1))
答案 1 :(得分:0)
将数字转换为字符串!
(SELECT SUBSTRING(P.CUSTOM1, CHARINDEX ('_', P1.CUSTOM1) -1, LEN(P1.CUSTOM1))
FROM [IMAN-SQL].[LIVE].[MHGROUP].PROJECTS P1
WHERE P.CUSTOM1 LIKE 'CLIENT_%'
) = CONVERT(NVARCHAR(254), AD.NAMENO)
或者:
(SELECT P.CUSTOM1
FROM [IMAN-SQL].[LIVE].[MHGROUP].PROJECTS P1
WHERE P.CUSTOM1 LIKE 'CLIENT_%'
) = CONCAT('CLIENT_', AD.NAMENO)