Solved

SQL: IsNumeric

Posted on 2014-10-27
3
161 Views
Last Modified: 2014-10-27
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.
0
Comment
Question by:pzozulka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
ID: 40406927
select 
case when isnumeric(left('15-asdff-b', 2)) =1 and charindex('-', '15-asdff-b') = 3
then 1 else 0 end

Open in new window

replace the 15-asdff-b with your column name.  Will return 1 if the left 2 digits are numeric followed by a dash else 0.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40406939
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.
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 40406957
Figured out the regular expression, but Kyle beat me to it..
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 value  LIKE '[0-9][0-9]-%' THEN 'true' ELSE 'false' END
FROM #tmp

Open in new window

0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question