tags:

views:

33

answers:

2

Hello I'm trying to select only entries in the following format from a varchar field.

[any number of zeros][a hyphen or not][any number of numbers]

some samples of what I want would be...
000000000007975
000000000-58628
123421423890347

But not
00000--18945489
00000000000012B

SELECT Field
FROM table
WHERE Field LIKE ???

+1  A: 
^0*-?[0-9]+$

should do it.

SELECT Field FROM Table WHERE Field REGEXP "^0*-?[0-9]+$";

is what you'd write in MySQL; I hope that's similar in TSQL.

Edit: This allows numbers with leading dashes. If you don't want that, use

^(0+-)?[0-9]+$
Tim Pietzcker
+1  A: 

TSQL does not support regular expressions. You could look into CLR to add this.

http://www.simple-talk.com/sql/t-sql-programming/clr-assembly-regex-functions-for-sql-server-by-example/

Fabian