[英]Extract integers from formula(string) using SQL
I have a column with formulas. 我有一列公式。 I need to extract only the integers present in the formula and put them in the next row.
我只需要提取公式中存在的整数并将它们放在下一行中。 For this formula,
对于这个公式,
Column A
(((23-22)/23)*100)
The output expected is as below without 100 预期的输出如下,不带100
Column A
23
22
I want to implement this in sql. 我想在sql中实现。 Please help.
请帮忙。 Thanks in advance.
提前致谢。
Edit1: The integers in the formula are actually the ids from another table. Edit1:公式中的整数实际上是另一个表的ID。 I need to join the tables on the IDs present in this formula
我需要将表格加入此公式中存在的ID上
Table 1 Table 2
Tbl1ID Tabl2Id Formula
22 1 (((23-22)/23)*100)
23 2 24 + 25
24
25
I have seen to it that Table 1 does not have Tbl1ID as 100. 我已经看到,表1的Tbl1ID不为100。
Edit2: Probably my approach is not right. Edit2:可能我的方法不正确。 The actual requirement is I need to replace the integers with a name associated to the id.
实际要求是我需要将整数替换为与ID关联的名称。
Table 1 Table 2 Table 3(Required)
Tbl1ID Name Tbl2Id Formula Col1
23 a 1 (((23-22)/23)*100) (((a-b)/a)*100)
22 b
Try this: 尝试这个:
DECLARE @TEST TABLE (TBL1ID INT)
INSERT INTO @TEST VALUES ('22')
INSERT INTO @TEST VALUES ('23')
INSERT INTO @TEST VALUES ('24')
INSERT INTO @TEST VALUES ('25')
DECLARE @NUM TABLE (INTE INT)
DECLARE @FORM VARCHAR(100)='(((23-22)/23)*100)',@VAL VARCHAR(MAX)
WHILE( PATINDEX('%[0-9]%',@FORM))>0
BEGIN
SET @VAL=(SELECT LEFT(VAL,PATINDEX('%[^0-9]%', VAL+'A')-1) FROM(
SELECT SUBSTRING(@FORM, PATINDEX('%[0-9]%', @FORM), LEN(@FORM)) VAL )X)
INSERT INTO @NUM VALUES( @VAL)
SET @FORM= SUBSTRING(@FORM, PATINDEX('%[0-9]%', @FORM)+LEN(@VAL),LEN(@FORM))
END
SELECT DISTINCT * FROM @NUM WHERE INTE IN (SELECT * FROM @TEST)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.