使用 CHARINDEX 和 LEN 在 SUBSTRING 之后将 NVARCHAR 转换为 INT
Converting NVARCHAR to INT after SUBSTRING using CHARINDEX and LEN
我正在尝试使用 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,
希望这就是您要找的。
你测试你的子字符串了吗?在我的测试中,我得到
将 nvarchar 值 'T_1456' 转换为数据类型 int 时转换失败。
所以子字符串正在获取下划线的字符索引,然后您将删除一个字符以获取无法转换为 INT 的 T_。更改为加号而不是减号可获得有效连接。
CAST((SELECT SUBSTRING(P.CUSTOM1, CHARINDEX ('_', P1.CUSTOM1) +1, LEN(P1.CUSTOM1))
将数字转换为字符串!
(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)
我正在尝试使用 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,
希望这就是您要找的。
你测试你的子字符串了吗?在我的测试中,我得到
将 nvarchar 值 'T_1456' 转换为数据类型 int 时转换失败。
所以子字符串正在获取下划线的字符索引,然后您将删除一个字符以获取无法转换为 INT 的 T_。更改为加号而不是减号可获得有效连接。
CAST((SELECT SUBSTRING(P.CUSTOM1, CHARINDEX ('_', P1.CUSTOM1) +1, LEN(P1.CUSTOM1))
将数字转换为字符串!
(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)