简体   繁体   English

SQL Server使用正则表达式选择

[英]Sql Server select with regular expression

can we use regular expression to select the item from database? 我们可以使用正则表达式从数据库中选择项目吗? The table item is like below 表格项如下

Table column|name|
10.01.02    | a  |
100.2.03    | b  |
1021.10.04  | c  |

Now my problem is that i need to select the column and get the substring like below 现在我的问题是我需要选择列并获取如下所示的子字符串

Table column|name|
10.01       | a  |
100.2       | b  |
1021.10     | c  |

any suggestion with regular expression or substring? 有正则表达式或子字符串的任何建议吗?

select left(a, datalength(a) - charindex('.', reverse(a))), b
from (
    select '10.01.02', 'a' union all
    select '100.2.03', 'b' union all
    select '1021.10.04', 'c'
) t(a,b)

I think there is no solution with regexp. 我认为regexp无法解决。

UPD UPD

Another solution: 另一个解决方案:

select reverse(parsename(reverse(a), 1)) + '.' + reverse(parsename(reverse(a), 2)), b
from (
    select '10.01.02', 'a' union all
    select '100.2.03', 'b' union all
    select '1021.10.04', 'c'
) t(a,b)

There may simple smarter way. 可能有更简单的方法。 But this is what i can think of. 但这就是我能想到的。

select substr(column, 1,lenght(column)-instr(reverse(column),'.'), name from table;

Try this, 尝试这个,

SELECT SUBSTRING(column1,0,(CHARINDEX('.',column1,CHARINDEX('.',column1)+1))) 
FROM Table1

SQL Fiddle Demo SQL小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM