Solved

SQL: IsNumeric

Posted on 2014-10-27
3
149 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 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

770 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