[英]How to fetch an ID from the table using replace,substring charindex in SQL Server 2012?
I have two tables ( #temp1
and #temp2
) as shown in the below code and I'm trying to get the matching static ID for the values present in #temp2
table but there is problem with the below code such that it returns null staticID
value. 我有两个表(
#temp1
和#temp2
),如下面的代码所示,并且我试图获取与#temp2
表中存在的值匹配的静态ID,但是下面的代码存在问题,因此它返回null staticID
值。
I'm not sure where its getting wrong can someone let me know whats the mistake I'm doing in the below code ? 我不确定在哪里出错,有人可以让我知道下面代码中发生的错误吗?
BEGIN
CREATE TABLE #temp2
(
staticname VARCHAR(160),
)
INSERT INTO #temp2
VALUES ('Per capita disappearance, carcass weight(456)'),
('Production(286)')
CREATE TABLE #temp1
(
idnum INTEGER IDENTITY(1, 1),
statisticname VARCHAR(256),
staticid INTEGER
)
INSERT INTO #temp1
VALUES ('Per capita disappearance, carcass weight',
'144'),
('Production',
'143')
SELECT
Ltrim (Rtrim (Replace (T2.staticname, Substring(T2.staticname,
Charindex('(', T2.staticname, 0),
Len(T2.staticname) - ( Charindex('(', T2.staticname, 0) - 1 )), ''
))),
T1.staticid
FROM
#temp2 T2
LEFT JOIN
#temp1 T1 ON T2.staticname = T1.statisticname
DROP TABLE #temp1
DROP TABLE #temp2
#temp1
table's STATISTICNAME
values #temp1
表的STATISTICNAME
值
Per capita disappearance, carcass weight
Production
are not matching with #temp2
table's STATISTICNAME
values 与
#temp2
表的STATISTICNAME
值不匹配
Per capita disappearance, carcass weight(456)
Production(286)
Since using the LEFT JOIN
so obviously the left table #temp
data will come, so the STATICID
column from #temp1
is returning NULL
由于使用了
LEFT JOIN
因此显然会出现左表#temp
数据,因此#temp1
的STATICID
列返回NULL
If you want to do the partial search with the columns use the LIKE
operator. 如果要对列进行部分搜索,请使用
LIKE
运算符。 So your LEFT JOIN
block will be: 因此,您的
LEFT JOIN
块将是:
LEFT JOIN #TEMP1 T1 ON T2.STATICNAME LIKE T1.STATISTICNAME + '%'
You have to change the columns that match in the JOIN
您必须更改
JOIN
中匹配的列
SELECT LTRIM (RTRIM (REPLACE (T2.STATICNAME,SUBSTRING(T2.STATICNAME, CHARINDEX('(',T2.STATICNAME,0), LEN(T2.STATICNAME)-(CHARINDEX('(',T2.STATICNAME,0)-1)), '')))
,T1.STATICID
FROM #TEMP2 T2 LEFT JOIN #TEMP1 T1 ON LTRIM (RTRIM (REPLACE (T2.STATICNAME,SUBSTRING(T2.STATICNAME, CHARINDEX('(',T2.STATICNAME,0), LEN(T2.STATICNAME)-(CHARINDEX('(',T2.STATICNAME,0)-1)), '')))
= T1.STATISTICNAME
You are not joining on the parsed value. 您没有加入已解析的值。 Try this:
尝试这个:
BEGIN
CREATE TABLE #TEMP2
(
STATICNAME Varchar(160),
)
INSERT INTO #TEMP2 VALUES ('Per capita disappearance, carcass weight(456)'),('Production(286)')
CREATE TABLE #TEMP1
(
IDNUM INTEGER IDENTITY(1,1),
STATISTICNAME VARCHAR(256),
STATICID INTEGER
)
INSERT INTO #TEMP1 VALUES ('Per capita disappearance, carcass weight','144'),('Production','143')
;With cteParsed As
(Select *,
LTRIM (RTRIM (REPLACE (T2.STATICNAME,SUBSTRING(T2.STATICNAME,
CHARINDEX('(',T2.STATICNAME,0), LEN(T2.STATICNAME)-
(CHARINDEX('(',T2.STATICNAME,0)-1)), ''))) StaticNameParsed
From #Temp2 T2
)
Select *, T1.STATICID
From cteParsed T2
LEFT JOIN #TEMP1 T1 ON T2.StaticNameParsed = T1.STATISTICNAME
DROP TABLE #TEMP1
DROP TABLE #TEMP2
END
Not sure what you are looking for, but your join was failing 不确定您要寻找的内容,但加入失败
Select A.*,B.*
From #Temp1 A
Join #Temp2 B on ( B.STATICNAME Like A.STATISTICNAME+'%' )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.