mysql从字符串中删除字母

时间:2017-07-19 09:05:25

标签: mysql

我想删除字符串中的字母:

select CAST(dstring as number), * from myTable

dstring可以是M-St2,DE2,M12,

结果应为2,2,12

由于

4 个答案:

答案 0 :(得分:0)

替换列中的数字。例如' John12'删除' 12'从专栏然后返回' John',然后替换' John'。你可以得到' 12'

SELECT 
REPLACE(dstring, REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE (
    REPLACE( REPLACE( REPLACE( REPLACE(dstring ,'0','')
    ,'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''), '')
FROM myTable;

答案 1 :(得分:0)

似乎没有这样的功能。

我真正的问题如下:我想通过dstring正确地订购我的表。

我试过这段代码:

select dstring from myTable order by dstring

并得到以下内容: 圣 ST1 ST10 ST11 ST12 ST2 ST20 ST21 ST3

但我希望它像那样

ST ST1 ST2 ST3 ST10 ST11 ST12 ST20 ST21

有谁知道如何解决这个问题? 谢谢!

答案 2 :(得分:0)

钉了它。...这将删除a-z和A-Z,将所有其他字符留在后面...

UPDATE some_table SET some_field = REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(some_field,'a','') ,'b','') ,'c','') ,'d','') ,'e','') ,'f','') ,'g','') ,'h','') ,'i','') ,'j','') ,'k','') ,'l','') ,'m','') ,'n','') ,'o','') ,'p','') ,'q','') ,'r','') ,'s','') ,'t','') ,'u','') ,'v','') ,'w','') ,'x','') ,'y','') ,'z','') ,'A','') ,'B','') ,'C','') ,'D','') ,'E','') ,'F','') ,'G','') ,'H','') ,'I','') ,'J','') ,'K','') ,'L','') ,'M','') ,'N','') ,'O','') ,'P','') ,'Q','') ,'R','') ,'S','') ,'T','') ,'U','') ,'V','') ,'W','') ,'X','') ,'Y','') ,'Z','');

这是我的PHP生成该代码(在$ to_remove字符串中添加您需要的其他任何字符:

$to_remove = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$removals = str_split($to_remove,1);
$stmt = "some_field";
foreach($removals as $x) {
    $stmt = " REPLACE(".$stmt.",'".$x."','') ";
}
$stmt = "update some_table set some_field = ".$stmt." ";
echo($stmt);

答案 3 :(得分:-1)

尝试执行此代码,它可能对您有所帮助。

dstring可以是M-St2,DE2,M12,

结果应为2,2,12

仅显示数值。

将NumericOnly(dstring)作为myTable中的数字