2016-10-13 48 views
-1

之前获得所有数字在一列中的结果是:SQL字符串某个字符

14800:DATAB Unload 

我试图让之前的所有数字的:所以我只接收数量。

我应该在我的选择中做些什么来完成这项工作?

+2

mysql或postgresql ???? – scaisEdge

+0

postgresql它是即时通讯使用 –

回答

0

您可以使用该正则表达式:

select regexp_replace(the_column, '(^[0-9]+):.*', '\1') 
from the_table; 

或者使用left()功能:

select left(the_column, strpos(col, ':') - 1) 
from the_table; 

的正则表达式是针对不包含数字或:弦更稳健。如果您确定值将有一个:在其中,第二个解决方案应该是首选,因为它是更快。

以下示例:

with data (col) as (
    values ('14800:DATAB Unload'), ('Another Unload') 
) 
select regexp_replace(col, '(^[0-9]+):.*', '\1'), 
     left(col, strpos(col, ':') - 1) 
from data; 

回报:

regexp_replace | left  
----------------+------------ 
14800   | 14800  
Another Unload | Another Unloa 

第二个值将被留下的正则表达式溶液不变,但使用时left()将切断的最后一个字符。

1

为MySQL

SELECT LEFT(you_column,LOCATE(':',your_column) - 1) from your_table 

PostgreSQL的

SELECT substr(you_column , 1, position(':' in your_column) - 1) from your_table 

SELECT left(you_column , position(':' in your_column) - 1) from your_table 
+0

我正在使用postgresql,即时通讯得到的错误:

position(':',your_column)

+0

它错误在',' –

+0

@MarkieDoe答案更新。 – scaisEdge