选择包含特定条件的语句



我有一列包含表中的这些值。

Colum1: dhd-29229 Table: Test
dhd-29199
dhd-00011

我的目标是编写一个select sql语句,它将返回以下值:所有数值,不考虑dhd-

使用like:

where column1 like '%29229'

如果您想要这种格式的XXX-29199,您必须使用:

where column1 like '___-29229'

如果只需要29229,可以使用:where column1 like '%29229',但在这种情况下,查询将返回所有以"29229">结尾的值。你可以考虑控制长度。

https://www.w3schools.com/sql/sql_like.asp

在T-SQL(SQL server)中:从测试中选择子字符串(colum1,charindex('-',colum1)+1,5)作为sub_string

如果您想要一个严格的"仅限数字",它不会对格式或不一致进行回复。。。尝试编写UDF:

Create FUNCTION [test].[numOnly]
(@text nvarchar (max))
RETURNS nvarchar (max)
AS
BEGIN
DECLARE 
@char nvarchar(1),
@i_idx int,
@temp int,
@out nvarchar(max)
While len(@text) > 0
Begin
Set @char = left(@text,1)
if ascii(@char) between 49 and 57
set @out = isnull(@out,'') + @char
Set @text = RIGHT(@text,len(@text)-1)
End
Return @out
END

这样使用它。。。

select  [test].[numOnly]('44--(*)"*()£hllk564kkj3') as 'lala'

结果:445643

最新更新