Click here to Skip to main content
12,065,888 members (25,958 online)
Rate this:
 
Please Sign up or sign in to vote.
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-jw15.1K

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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160207.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2016
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