Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6147947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:11:34+00:00 2026-05-23T19:11:34+00:00

The following (simplified and obfuscated) query appears to (in my mind) return a varchar

  • 0

The following (simplified and obfuscated) query appears to (in my
mind) return a varchar type from each possible return branch in the
nested case expression. I went to the trouble of casting the first
two possible return branches to varchar to ensure their congruence
with the last two possible return types as represented in the last
(most deeply nested) case statement which returns strings. However,
when run, it complains about trying to convert the string ‘foo’ to an
int. Everything worked fine until I added that bottommost, most
deeply nested, case statement that always returs strings (and indeed,
before I added that, there was no need for any conversion of dates
since dates were all that could ever be returned). QUESTION: Any
idea why sql server is trying to convert the ‘foo’ string (and
probably ‘bar …’ string if it could get that far) into an int? More
importantly, any ideas on a solution to solve the casting/conversion
issue? Thanks in advance.

select case
         when t1.flubbitz - t1.dubbitz <= 0
         then 
           case
             when (select min(t3.[duedate])
                   from blatz t3  
                     inner join dort t4 on t3.anumerickey = t4.anumerickey 
                   where t4.[somenumbercol] = t2.somenumbercol
                     and t4.somecode = '01'
                       and t3.duedate > getdate()) is not null
             then (select left(convert(varchar, (select min(t3.[duedate])
                   from blatz t3  
                     inner join dort t4 on t3.anumerickey = t4.anumerickey 
                   where t4.[somenumbercol] = t2.somenumbercol
                     and t4.somecode = '01'
                       and t3.duedate > getdate()), 101), 10))
             else 
               case 
                 when (select min(t0.[duedate])
                       from sabtz t0  
                             inner join quux t1 on t0.anumerickey = t1.anumerickey 
                       where t0.somenumbercol = t2.somenumbercol
                         and t0.duedate > getdate()) is not null
                 then (select left(convert(varchar, (select min(t0.[duedate])
                       from sabtz t0  
                             inner join quux t1 on t0.anumerickey = t1.anumerickey 
                       where t0.somenumbercol = t2.somenumbercol
                         and t0.duedate > getdate()), 101), 10))
                     else 
                   case
                     when t2.somenumbercol like 'blub%' then 'foo'
                     when t2.somenumbercol like 'batz%' then 'bar ' + left(convert(varchar, getdate(), 101), 10) 
                   end    
               end 
           end       
         else (select null) 
       end as [Foobar]

from yyy t1 
     inner join xxx t2 on t1.somenumbercol = t2.somenumbercol

where ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:11:35+00:00Added an answer on May 23, 2026 at 7:11 pm

    It’s the “(SELECT NULL)”.

    In the examples below, “(SELECT NULL)” throws the error and “NULL” does not. The “()” casts the NULL as an INT.

    SELECT CASE -- error
    WHEN 3 < 2 THEN 'Hello'
    ELSE (SELECT NULL)
    END
    UNION
    SELECT 'Goodbye'
    
    SELECT CASE -- works
    WHEN 3 < 2 THEN 'Hello'
    ELSE NULL
    END
    UNION
    SELECT 'Goodbye'
    
    SELECT CASE -- works
    WHEN 3 < 2 THEN 'Hello'
    ELSE CAST(NULL AS VARCHAR)
    END
    UNION
    SELECT 'Goodbye'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following (simplified) query select P.peopleID, P.peopleName, ED.DataNumber from peopleTable P left
The intention of following (simplified) code fragment is to return one random row .
I've got the following (simplified) XML... <root type=object> <user type=object> <id type=number>123</id> </user> <id
I have the following (simplified) result from SELECT * FROM table ORDER BY tick,refid
Consider the following simplified case: lol = [['John','Polak',5,3,7,9], ['John','Polak',7,9,2,3], ['Mark','Eden' ,0,3,3,1], ['Mark','Eden' ,5,1,2,9]] What
I have the following (simplified) code in my android MainActivity.java class case R.id.menu_stop: stopPlaying();
Suppose I have the following (simplified case): class Color; class IColor { public: virtual
I have a table with the following (simplified) structure: INT id, INT type, INT
I have the following (simplified) code from a generic factory class: - (id) invokeSetup:
Following simplified code snippet: #include <QtGui> int main(int argc, char **argv) { QApplication app(argc,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.