Wildcard of Number in SQL Server

2020-06-08 13:52发布

How to match numbers in SQL Server 'LIKE'.

SpaceName
------------ 
| New_Space_1
| .
| .
| New_Space_8 
| New_Space_9
| New_Space_10
| New_Space_11
| New_Space_SomeString
| New_Space_SomeString1

Above is my table contents. I want to get only records ending with Numeric chars, ie I want the records from New_Space_1 to New_Space_11.

Don't want New_Space_SomeString and New_Space_SomeString1

I have some query like this.

SELECT SpaceName FROM SpaceTable
    WHERE SpaceName LIKE 'New_Space_%' 

But this returns all records.

2条回答
beautiful°
2楼-- · 2020-06-08 14:22

This solution from @SteveKass works perfect.

SELECT SpaceName FROM SpaceTable WHERE SpaceName LIKE 'New[_]Space[_]%' AND SpaceName NOT LIKE 'New[_]Space[_]%[^0-9]%' 
查看更多
Evening l夕情丶
3楼-- · 2020-06-08 14:27

what about

SELECT SpaceName FROM SpaceTable
    WHERE SpaceName LIKE 'New[_]Space[_][0-9]%' 

The reason I put underscore in brackets is because in a regular expression _ means Any single character. Read up on like here http://msdn.microsoft.com/en-us/library/ms179859.aspx

查看更多
登录 后发表回答