Link to home
Start Free TrialLog in
Avatar of pzozulka
pzozulka

asked on

SQL: IsNumeric

I have a column called TaxID which is a VARCHAR.
I need to test if it starts with 2 digits and a dash. What is the best way to do that?

SELECT CASE WHEN CI.TaxID like '##-%' THEN 1 ELSE 2 END

This would be great, if there are wildcards for numbers.
SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This worked for me.
CREATE TABLE #tmp (value varchar(10))

INSERT INTO #tmp (value) 
VALUES('09-banana'), ('58 Mustang'), ('BR-549'), ('67-omaha'), ('R2-D2')

SELECT value, 
	CASE WHEN ISNUMERIC(LEFT(value, 1)) = 1 
		AND ISNUMERIC(SUBSTRING(value, 2, 1)) = 1 
		AND SUBSTRING(value, 3, 1) = '-' THEN 'true' ELSE 'false' END
FROM #tmp

Open in new window

I tried using Regular Expressions, which would probably be less code, but no love.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial