Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to retrieve all the values from tbl_emps which starts from a digit. Can Some One tell me the SQL Query for this puprose?
Posted
Comments
[no name] 10-May-12 9:58am    
Try it with regexp.

SQL
SELECT * FROM table_name WHERE column_name LIKE '[0123456789]%';




Sure, it will work

cheers.
 
Share this answer
 
v2
Comments
Maciej Los 10-May-12 11:28am    
Good answer, my 5!
Try this:
SQL
SELECT * FROM myTable WHERE myField LIKE '[0123456789]%'
The [0123456789] part says "any character in this set" and the % matches any text at all.
 
Share this answer
 
Comments
Maciej Los 10-May-12 11:27am    
Good answer, my 5!
rashidfarooq 10-May-12 14:20pm    
I tried it but it didn't work.
OriginalGriff 10-May-12 14:26pm    
What exactly did your statement look like? Copy and paste if possible.
And another variation is
SQL
SELECT ...
FROM   YourTable 
WHERE  1 = ISNUMERIC( SUBSTRING( FieldName, 1, 1))

However, If you need to find this kind of information in your query it may indicate that you are actually concatenating two separate data items inside a single field. So if that would be the case, then I'd suggest that you store each data in it's own field.
 
Share this answer
 
Comments
Maciej Los 10-May-12 13:15pm    
Good answer. +5
Sorry, but i need to re-vote. The query returns bad results, especially when the first sign is: ',,', '-'

Example to study:

CREATE TABLE #Names (FactoryName NVARCHAR(50) NULL)

INSERT INTO #Names (FactoryName)
VALUES ('-HIID SIIMSON')
INSERT INTO #Names (FactoryName)
VALUES ('-INGA')
INSERT INTO #Names (FactoryName)
VALUES (',,AMPHORA"')
INSERT INTO #Names (FactoryName)
VALUES ('"MP TRANS"')
INSERT INTO #Names (FactoryName)
VALUES ('.NET')
INSERT INTO #Names (FactoryName)
VALUES ('.K.')

SELECT DISTINCT [FactoryName]
FROM #Names
WHERE 1 = ISNUMERIC(SUBSTRING(LTRIM([FactoryName]), 1, 1))

SELECT DISTINCT [FactoryName]
FROM #Names
WHERE [FactoryName] Like '[1234567890]%'

DROP TABLE #Names

Compare results ;)
Wendelius 10-May-12 13:33pm    
That's correct, also for example + sign is considered as numeric. This is because decimal separators and + and - are considered as parts of a number. Depending on the requirements this may/may not be the desired result :)
rashidfarooq 10-May-12 14:21pm    
SELECT DISTINCT [FactoryName]
FROM #Names
WHERE [FactoryName] Like '[1234567890]%'

This pattern doesn't work.
Wendelius 10-May-12 14:41pm    
Could you give an example data?
Maciej Los 10-May-12 15:55pm    
Please, copy all example code, starting from CREATE TABLE... and ending on DROP TABLE. I have MS SQL Server 2005 EE with + Management Studio and it works perfect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900