Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2010
    Posts
    11

    Unanswered: Problem in executing query in CASE statement

    Hello All,

    I am facing a problem in executing queries with CASE statement.
    Based on my condition,(for eg. length), I want to execute different SQL statement.

    Problematic sample query is as follows:

    Code:
    select case when char_length('19480821')=8
                    then select count(1) from Patient
                when char_length('19480821')=10
                    then select count(1) from Doctor 
        end
    Exception:
    >[Error] Script lines: 1-5 --------------------------
    Incorrect syntax near the keyword 'select'.
    Msg: 156, Level: 15, State: 2
    Server: sunsrv4z7, Line: 2


    I am not able to correct the syntax. I am getting the string for char_length as input from the user.
    How can I fire queries based on certain condition?
    Is CASE the right choice ? Or do I have to use any other thing.

    Please advice me the right way.

    Thanks in advance

  2. #2
    Join Date
    May 2010
    Location
    Hyderabad, India
    Posts
    16

    Use following Query

    select case when char_length('19480821')=8
    then (select count(1) from Patient)
    when char_length('19480821')=10
    then (select count(1) from Doctor)
    end


    I think this will work fine.
    -PavanKumar M Reddy

  3. #3
    Join Date
    Jun 2010
    Posts
    11
    Thanks,
    It worked.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •