Hari Gillala
Hari Gillala

Reputation: 11916

determine if column value string starts with a number

I have a table Questions with a column Description. Its column values are like this:

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

How can I determine, for each row, if its column value starts with a number 0-9?

Upvotes: 17

Views: 42399

Answers (5)

raja a
raja a

Reputation: 19

select * 
from TABLE_NAME 
where (YourColName like '0%' or 
       YourColName like '1%' or 
       YourColName like '2%' or 
       YourColName like '3%' or 
       YourColName like '4%' or 
       YourColName like '5%' or 
       YourColName like '6%' or 
       YourColName like '7%' or 
       YourColName like '8%' or 
       YourColName like '9%')

Upvotes: -1

Ben Curthoys
Ben Curthoys

Reputation: 859

SELECT * FROM Questions WHERE Description LIKE '[0-9]%'

Upvotes: 20

Mayur Desai
Mayur Desai

Reputation: 713

You can use the following query. Which first remove extra space form the left side and get first left character. This query return 0 if it is not numeric other wise it return 1.

Select ISNUMERIC(Left(Ltrim('1 This is Subheading'),1)) As Number

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460158

SELECT CASE WHEN ISNUMERIC(SUBSTRING(LTRIM(Description), 1, 1)) = 1 
         THEN 'yes' 
         ELSE 'no' 
       END AS StartsWithNumber
FROM Questions 

Upvotes: 23

juergen d
juergen d

Reputation: 204766

select true where cast(substring('1 This is Subheading', 1, 1) as int) between 0 AND 9

Upvotes: 0

Related Questions