CASE with SELECT * FROM TableName not possible?!

  • it's kinda straightforward i think; a case statement can return only one datatype.

    when the case is constructed, i think it is assuming the first item int he case statement will be the datatype to use.

    since one of the items is a column of a different type, it assumes it needs to convert it, and fails.

    here'sa a more basic example with hardcoded values: it blows up on some conversions.

    a case statement must be used in relation to data; an IF statement does not, so you don't have that problem with an IF statement.

    declare @id int

    set @id=1

    select case @id

    when 1 then 64

    when 2 then 'apples'

    when 3 then getdate() end

    set @id=2

    select case @id

    when 1 then 64

    when 2 then 'apples'

    when 3 then getdate() end

    set @id=3

    select case @id

    when 1 then 64

    when 2 then 'apples'

    when 3 then getdate() end

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • And also comparing an integer with a GUID is not allowed (as integer has higher precedence over GUID and GUID is not a valid integer), which is the case here.

    Its not actually about EXPLICIT comparison, rather EXPLICIT conversion..:cool:

    Too much coffee late in the day:w00t:

    --Ramesh


  • Tnx for answering all.

    Greetz,
    Hans Brouwer

Viewing 3 posts - 16 through 17 (of 17 total)

You must be logged in to reply to this topic. Login to reply