Click here to Skip to main content
11,581,510 members (60,503 online)
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 19:22pm
josh-jw13.3K

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 340
1 Sergey Alexandrovich Kryukov 160
2 virusstorm 114
3 Frankie-C 105
4 RyanDev 84
0 OriginalGriff 1,273
1 Sergey Alexandrovich Kryukov 895
2 Abhinav S 597
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2015
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