Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005
in my query
if the input parameter is numeric i want to check one condition otherwise another condition.
for example
 
if(case 
when (isnumeric(@parameter)) then exist(select name from table where id=@paramter)
else
length(@parameter)>1
end)
Posted 24-Mar-13 20:22pm
josh-jw10.7K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Based on the code that you showed in your question, it does not look like the CASE statement is a good choice. The "exists" function returns a Boolean value (True|False). You are trying to use it in the "Then" clause of a When statement. The Transact-SQL grammar does not allow that.
 
Try something like this:
declare @parameter varchar(10);
declare @x int;
 
if isnumeric(@parameter)=1
    begin
    if exists(select name from [table] where id=@parameter)
        Begin
        Select @x=1
        End
    Else
        Begin
        Select @x=2
        End
    End
Else
    Begin
    if len(@parameter)>1
        Begin
        Select @x=3
        End
    Else
        Begin
        Select @x=4
        End
    End
 

Tested: SQL Server Express 2012
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100