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 9279567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:39:58+00:00 2026-06-18T17:39:58+00:00

I have a SQL query which includes the following statement SELECT (CASE WHEN field1

  • 0

I have a SQL query which includes the following statement

SELECT
    (CASE 
        WHEN field1 > 0 THEN field1 
        ELSE (field2 * field3) 
    END) AS result
FROM table

Which is supposed to return the value of field1, unless that is ‘0’, in which case the statement returns the product of two other fields.

The issue I am having is this statement returns NULL if field1 is NULL.

I want to revise my SQL so that the product of fields 2 & 3 is returned when field1 is NULL, just as it does when field1 is ‘0’.

I have tried the following:

SELECT
    (CASE 
        WHEN field1 > 0 THEN field1 
        WHEN field1 IS NULL THEN (field2 * field3)
        ELSE (field2 * field3) 
    END) AS result
FROM table

But NULL still seems to permeate through the statement and NULL is still being returned whenever field1 is NULL.

  • 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-06-18T17:39:59+00:00Added an answer on June 18, 2026 at 5:39 pm

    Your query does already what you want. When field1 is NULL, it returns the product field2 * field3. If either of field2 or field3 though is NULL, the (returned) product will be NULL.

    If you want to be returning some value (0 perhaps) when either of them is NULL, you can change the CASE:

    SELECT
        (CASE 
            WHEN field1 > 0                         -- when field1 > 0
                THEN field1 
            WHEN field2 IS NULL OR field3 IS NULL   -- when field1 = 0 or NULL
                THEN 0
            ELSE field2 * field3 
        END) AS result
    FROM table ;
    

    or simply:

    SELECT
        (CASE 
            WHEN field1 > 0  
                THEN field1 
            ELSE COALESCE(field2 * field3, 0) 
        END) AS result
    FROM table ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql query which looks like this - NSString *createSQL = @SELECT
I have following somewhat complex sql query which has horrible performance, 'certainly' due to
I have an old FoxPro program that does a SQL query which includes the
I have the following SQL query: SELECT * FROM table WHERE field_1 <> field_2
I currently have a prepared statement in Java which uses the following SQL statement
Right now I have this SQL query which is valid but always times out:
using php, i have a sql query which works fine, dateentry only has the
I have a SQL Server query which runs just fine -- until I add
In my Java code I have embedded a SQL query which fetches data from
i've got a SQL query which returns multiple rows, and i have : $data

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.