我有一个像
这样的值的字段 "ABC1234"
"ABC5678/DEF"
"AB1298"
"AB1298/DEF"
我想从每一个中提取数值,例如:
1234
5678
1298
1298
注意:数值始终为“在一起”(1234),并且始终仅由4位数组成。
我试图首先删除双引号以使用RegEx:SELECT REPLACE(model_name,'''','')FROM ProductList 注意:我替换使用'单引号而不是双引号',因为那是保存数据的方式,它的工作原理。然后我尝试使用Patindex来获取数值:SELECT SUBSTRING(field,PATINDEX('%[0-9]%',field),LEN( field))注意:但是,PATINDEX不适用于MySQL
我正在尝试这样做,因为我想通过创建一个临时表来分隔两个不同列中的每个值:
SELECT SUBSTR(t.column_one,1,INSTR(t.column_one,'')-1)AS col_one,SUBSTR(t.column_one,INSTR(t.column_one,'')+ 1)AS col_two FROM YOUR_TABLE t
val1 val2
12 34
56 78
12 98
注意:我在XAMPP中使用PHPMyAdmin。
在PATINDEX之后,我尝试了LOCATION和POSITION。 (您可以看到测试打印屏幕的序列here)SELECT SUBSTRING(model_name,LOCATE('%[^ 0-9]%',model_name),4)FROM ProductList 注意:LOCATE或POSITION函数返回位置0,这就是没有结果的原因。我可以想象问题出在“%[^ 0-9]%”,因为LOCATE,POSITION和MID都不接受RegEx。
我的下一个问题是:我希望在临时表足迹中包含 lval 和 rval ,在查询开头创建。这,因为我想通过获取输入文本值来创建查询,并具有类似的内容:
SELECT * FROM footprint WHERE lval=50;
model_name num_pos lval rval
''ABC1234'' 7 12 34
''ABC1234/DEF'' 7 50 78
''ABDCE1234'' 8 12 98
最后,我想搜索name_model,具体取决于lval和rval的值。所以下一个“查询”对我有用:
CREATE TEMPORARY TABLE IF NOT EXISTS footprint AS
(SELECT model_name,
LEAST (
if (Locate('0',model_name) >0,Locate('0',model_name),999),
if (Locate('1',model_name) >0,Locate('1',model_name),999),
if (Locate('2',model_name) >0,Locate('2',model_name),999),
if (Locate('3',model_name) >0,Locate('3',model_name),999),
if (Locate('4',model_name) >0,Locate('4',model_name),999),
if (Locate('5',model_name) >0,Locate('5',model_name),999),
if (Locate('6',model_name) >0,Locate('6',model_name),999),
if (Locate('7',model_name) >0,Locate('7',model_name),999),
if (Locate('8',model_name) >0,Locate('8',model_name),999),
if (Locate('9',model_name) >0,Locate('9',model_name),999)
) AS num_Pos
FROM ProductList) ;
SELECT name FROM (SELECT name, left(val,2) AS lval, right(val,2) AS rval FROM
(SELECT MID(pl.model_name, fp.num_Pos,4) AS val, pl.model_name AS name FROM ProductList AS pl INNER JOIN footprint AS fp ON fp.model_name=pl.model_name) p) n WHERE lval='50' and rval='50'
如果您对如何完成或改进此流程有任何其他建议,请告知我们。
谢谢你, 最好的问候。
答案 0 :(得分:0)
双引号是一个独特的字符,不仅仅是2个单引号,所以要删除双引号试试。
SELECT REPLACE(model_name,'"','') FROM ProductList
你跟上面的代码太近了。鉴于你的数字部分总是4个字符,你可以使用类似的东西
declare @t as table(field varchar(15))
insert into @t
values
('"ABC1234"'),
('"ABC5678/DEF"'),
('"AB1298"'),
('"AB1298/DEF"')
select left(val,2) as lval, right(val,2) as rval
from
(select SUBSTRING(field, PATINDEX('%[0-9]%', field), 4) val
from @t) t
因此,对于数据库的上下文,它将类似于
select left(val,2) as lval, right(val,2) as rval
from
(select SUBSTRING(model_name, POSITION('%[0-9]%', model_name), 4) val
from ProductList) p
答案 1 :(得分:0)
要分割数值和字符串部分,您可以使用trim
,如下所示:
SELECT trim(field, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ"') AS col0, trim(field, '1234567890"') AS col1 FROM table
希望有所帮助。
<强> P.S。强>
此解决方案也可用于sqlite数据库,其中函数PATINDEX
不存在。
答案 2 :(得分:0)
根据我写的所有描述,下一个代码结束了我的工作。 描述中隐藏了很多提示,但如果您对查询有任何建议,请成为我的访客。
CREATE TEMPORARY TABLE IF NOT EXISTS footprint AS
(SELECT model_name,
LEAST (
if (Locate('0',model_name) >0,Locate('0',model_name),999),
if (Locate('1',model_name) >0,Locate('1',model_name),999),
if (Locate('2',model_name) >0,Locate('2',model_name),999),
if (Locate('3',model_name) >0,Locate('3',model_name),999),
if (Locate('4',model_name) >0,Locate('4',model_name),999),
if (Locate('5',model_name) >0,Locate('5',model_name),999),
if (Locate('6',model_name) >0,Locate('6',model_name),999),
if (Locate('7',model_name) >0,Locate('7',model_name),999),
if (Locate('8',model_name) >0,Locate('8',model_name),999),
if (Locate('9',model_name) >0,Locate('9',model_name),999)
) AS num_Pos
FROM ProductList) ;
SELECT name FROM (SELECT name, left(val,2) AS lval, right(val,2) AS rval FROM
(SELECT MID(pl.model_name, fp.num_Pos,4) AS val, pl.model_name AS name FROM ProductList AS pl INNER JOIN footprint AS fp ON fp.model_name=pl.model_name) p) n WHERE lval='50' and rval='50'