SQL子字符串获取特定字符前的所有数字

SQL子字符串获取特定字符前的所有数字,sql,postgresql,Sql,Postgresql,列中的结果为: 14800:DATAB Unload 我试图得到:之前的所有数字,所以我只收到数字 我应该在select中输入什么来完成此操作?对于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 或 您

列中的结果为:

14800:DATAB Unload
我试图得到:之前的所有数字,所以我只收到数字

我应该在select中输入什么来完成此操作?

对于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 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|u替换|左
----------------+------------
14800           | 14800      
又一次卸载|又一次卸载

第二个值将由正则表达式解决方案保持不变,但在使用
left()
时将截断最后一个字符

mysql还是postgresql???postgresql正在使用吗?我正在使用postgresql,并且我在:位置(“:”,您的_列)处收到一个错误@scaisEdge postgresql还支持
left
函数。@Abelisto。谢谢回答更新。。(更通用的方法的substr i)。。
with data (col) as (
  values ('14800:DATAB Unload'), ('Another Unload')
)
select regexp_replace(col, '(^[0-9]+):.*', '\1'), 
       left(col, strpos(col, ':') - 1)
from data;