2012-04-20 90 views
6

我有一個表Questions以及列Description。它的列值是這樣的:確定列值字符串是否以數字開頭

This is First Heading, 
1 This is Subheading one, 
1.2 This is subheading Question 
This is Second heading 
2 This is subheading Two. 
2.1 This is Subheading Question1 

如何確定,對於每一行,如果其列值以數字0-9開頭?

是否有任何SQL Server 2008+功能?

回答

17
SELECT CASE WHEN ISNUMERIC(SUBSTRING(LTRIM(Description), 1, 1)) = 1 
     THEN 'yes' 
     ELSE 'no' 
     END AS StartsWithNumber 
FROM Questions 
0
select true where cast(substring('1 This is Subheading', 1, 1) as int) between 0 AND 9 
1

您可以使用下面的查詢。它首先從左側移除額外的空間並獲得左起第一個字符。如果該查詢不是數字,則返回0,否則返回1.

Select ISNUMERIC(Left(Ltrim('1 This is Subheading'),1)) As Number 
3
SELECT * FROM Questions WHERE Description LIKE '[0-9]%' 
相關問題