473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

if-then in select stmnt

Can I determine if a value is numberic then output 'text' else output
'some other text'?

I'm trying to do this in a select statement.
Thanks!

Jun 9 '06 #1
5 9402
anderma8 wrote:
Can I determine if a value is numberic then output 'text' else output
'some other text'?

I'm trying to do this in a select statement.
Thanks!


SELECT CASE WHEN ISNUMERIC(some_column)
THEN 'text'
ELSE 'some other text'
END AS theNumericTest
FROM table_name
.... etc. ...

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
Jun 9 '06 #2
MGFoster (me@privacy.com) writes:
SELECT CASE WHEN ISNUMERIC(some_column)
THEN 'text'
ELSE 'some other text'
END AS theNumericTest
FROM table_name
... etc. ...


1) CASE WHEN isnumeric(some_column) = 1

2) isnumeric is virtually useless because it approves anything that
can be converted a to numeric data type. For test on "all digits",
this is better: "some_column NOT LIKE '%^[0-9]%'"

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 9 '06 #3
Erland Sommarskog wrote:
MGFoster (me@privacy.com) writes:
SELECT CASE WHEN ISNUMERIC(some_column)
THEN 'text'
ELSE 'some other text'
END AS theNumericTest
FROM table_name
... etc. ...

1) CASE WHEN isnumeric(some_column) = 1

2) isnumeric is virtually useless because it approves anything that
can be converted a to numeric data type. For test on "all digits",
this is better: "some_column NOT LIKE '%^[0-9]%'"


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You're correct about the Isnumeric(...) requiring the "=1." You're
incorrect about the NOT LIKE expression. The NOT LIKE expression will
return True for all columns that have both alpha chars and numeric
chars. E.g.:

set nocount on
create table #t (c char(6))
go

insert into #t (c) values ('ab12')
insert into #t (c) values ('1112')
insert into #t (c) values ('cd12')
insert into #t (c) values ('3312')
insert into #t (c) values ('(*)^')
insert into #t (c) values ('$25.10')
insert into #t (c) values ('$25^2')

go

select c,
case when isnumeric(c)=1
then 'T'
else 'F'
end as IsNumericTest,

case when c NOT LIKE '%^[0-9]%'
then 'T'
else 'F'
end as NotLikeTest

from #t

drop table #t

Results:
c IsNumericTest NotLikeTest
------ ------------- -----------
ab12 F T
1112 T T
cd12 F T
3312 T T
(*)^ F T
$25.10 T T
$25^2 F F

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBRIxuWoechKqOuFEgEQLaIQCg/ndIiAGgGGMqbXsOtIrFB9KCWjsAn3kV
3m0xI3cbdPwhahJTlhod1p7S
=2I7M
-----END PGP SIGNATURE-----
Jun 11 '06 #4
On Sun, 11 Jun 2006 19:27:24 GMT, MGFoster wrote:
You're correct about the Isnumeric(...) requiring the "=1." You're
incorrect about the NOT LIKE expression.
Hi MGFoster,

That's right. Erland misplaced the ^ character, He should have typed
NOT LIKE '%[^0-9]%'
set nocount on
create table #t (c char(6))
You should change this to varchar. Or, if you want to keep this as char,
add a call to RTRIM() in the code. Fixed length character strings get
padded with space characters which are, clearly, not numeric.

(snip)select c,
case when isnumeric(c)=1
then 'T'
else 'F'
end as IsNumericTest,

case when c NOT LIKE '%^[0-9]%' Correct the line above to
case when c NOT LIKE '%[^0-9]%' then 'T'
else 'F'
end as NotLikeTest

from #t

drop table #t

Results:

c IsNumericTest NotLikeTest
------ ------------- -----------
ab12 F F
1112 T F
cd12 F F
3312 T F
(*)^ F F
$25.10 T F
$25^2 F F

--
Hugo Kornelis, SQL Server MVP
Jun 11 '06 #5
MGFoster (me@privacy.com) writes:
Erland Sommarskog wrote:
2) isnumeric is virtually useless because it approves anything that
can be converted a to numeric data type. For test on "all digits",
this is better: "some_column NOT LIKE '%^[0-9]%'"


You're correct about the Isnumeric(...) requiring the "=1." You're
incorrect about the NOT LIKE expression. The NOT LIKE expression will
return True for all columns that have both alpha chars and numeric
chars. E.g.:


Sorry, that was a typo, and bad proof-reading. The pattern should
of course be '%[^0-9]%'" as Hugo was kind to fill in for me.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bart Nessux | last post by:
Should an if statement have a corresponding else statement? Or, is it OK to have an if statement by itself. For completeness, it seems the two should be together, but from experience I know that a...
10
by: ale.of.ginger | last post by:
Greetings! I am trying to make a multiplayer (no AI, 2 person) game of tic tac toe in Python. So far it has been pretty simple. My only concern is with the win checking to see if a person has...
14
by: Santi | last post by:
I see in some code, I don´t remember now if it is c# or c++, that the when they perform a comparison they use the value first and then the variable, like: if( null == variable ){} Is there an...
0
by: Benny Ng | last post by:
Hi,All, When i deploy Enterprise library with my application ,i used XCOPY to deploy it into my test server. But when application runs, shown some error related registry. (But actually I haven't...
40
by: Jeff | last post by:
I have a system on a network and want to determine if anyone is currently connected to the back-end files. An interesting twist is that I have noticed that some users can be connected (have the...
5
by: Brie_Manakul | last post by:
Is there a way in javascript to do an if else that shows a script in an iframe? Let me know if that doesn't make sense. We have a portal and in a portlet I need to grab these javascript links to...
4
by: Brie_Manakul | last post by:
I need to set up an if else to show different weather scripts based on the city selection they choose. Any help on this would be great. Thanks! <%@ page language="java" import="java.util.*,...
3
by: Jake Barnes | last post by:
37 Signals has built some awesome software with some features I wish I knew how to imitate. When I'm logged into my page (http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver I'm...
6
by: Kun | last post by:
I am trying to make an if-statement that will not do anything and print 'nothing entered' if there is nothing entered in a form. I have the following code that does that, however, now even if I...
2
by: paul | last post by:
Hi all, I've been handed some code and, unless I've got the numbering of parentheses wrong, one of the functions has a curious if-else statement. The thing compiles but is it right? I know the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.