Solved

SQL: IsNumeric

Posted on 2014-10-27
3
144 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
  • 2
3 Comments
 
LVL 39

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 65

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 65

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql query 7 35
.htaccess file settings 4 35
separate column 24 20
How to query date ranges with SQL 6 24
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now