How to execute various CASE-based SELECT statements

I ran into a problem while executing queries using the CASE statement. Based on my condition (e.g. length), I want to execute a different SQL statement.

A problematic query example is as follows:

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

An exception:

[Error] Script line: 1-5 --------------------------
Invalid syntax next to the keyword "select".
Msg: 156, Level: 15, State: 2
Server: sunsrv4z7, Line: 2

I can not fix the syntax. I get a string for char_length as input from the user. How can I run queries based on certain conditions? Is CASE the right choice? Or should I use any other thing.

+4
4

, .

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
+4
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

, "" :)

+2

, , , EXPRESSION. , () .

, " , = ( max () )"

+2

  ,       LEN ('1948082100') = 8           ( "")              LEN ('194808210') = 10           ( " ")   

Change values ​​to test results.

-1
source

Source: https://habr.com/ru/post/1752769/


All Articles