我有一个包含以下值的字段:
A12345
AB456
1234
AA 45
无论如何在两个单独的列中选择这些作为数字和字母。
提前致谢
答案 0 :(得分:3)
如果你没有正则表达式,那么也许这样的东西会为你剪掉它。
SQL> with t as ( select 'A12345' as str from dual
2 union all
3 select 'AB456' as str from dual
4 union all
5 select '1234' as str from dual
6 union all
7 select 'AA 45' as str from dual)
8 select str
9 , replace(translate(str, '0123456789'
10 , ' '), ' ', null) as AAA
11 , replace(translate(str, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
12 , ' '), ' ', null) as NNN
13 from t
14 /
STR AAA NNN
------ ------ ------
A12345 A 12345
AB456 AB 456
1234 1234
AA 45 AA 45
SQL>
translate()
函数将数字(或字母)转换为空格,然后replace()
将空格转换为NULL。
答案 1 :(得分:0)
如果您正在使用支持用户定义函数的SQL引擎,您可以编写以解析它并返回唯一值的表。如果你要做很多事情,你可能会更好地将它们存储为单独的字段,这样你就可以使用DML而不是自定义代码来操作它们。
答案 2 :(得分:0)
如果您使用的是SQL Server 2005,则可以通过CLR集成调用.NET代码(例如C#或VB.NET正则表达式功能)。这篇文章是为了帮助您入门,我相信Google会有更多内容:http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
答案 3 :(得分:0)
create table tbl(data varchar(200))
insert into tbl(data)
select 'A12345' data union all
select 'AB456' union all
select '1234' union all
select 'AA 45'
-------------
select LEFT(data, PATINDEX('%[0-9]%', data)-1) as Letters,
CAST(SUBSTRING(data, PATINDEX('%[0-9]%', data), 10000) AS INT) as Numbers
from tbl