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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:29:57+00:00 2026-06-04T11:29:57+00:00

I have this query (pseudo code) SELECT a = 1, b = 2, c

  • 0

I have this query (pseudo code)

SELECT 
    a = 1,
    b = 2,
    c = CASE 
            WHEN ISNULL(
                         (SELECT MONTH(GETDATE()) <---long query
                         ), 0) = 0 THEN 'found'
            ELSE 
              SELECT MONTH(GETDATE())     <--- repeated long query
        END

The problem is that the SELECT MONTH(GETDATE()) which in reality is very long query.

Is there any workaround for this “long expression” not to appear twice in the query ?

p.s.

I have a solution of calculating SELECT MONTH(GETDATE()) into an outside variable… but I’m trying to figure out if there’s an inline solution.

  • 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-04T11:29:59+00:00Added an answer on June 4, 2026 at 11:29 am

    The particular pattern of usage of a subquery in your example could be re-written like this:

    …
    c = COALESCE(CAST(NULLIF((subquery), 0) AS varchar(10)), 'found')
    …
    

    As Mikael Eriksson has correctly pointed out in his comment, you may need to use ISNULL instead of COALESCE here, because one of the arguments contains a subquery and ISNULL may (or, in fact, will) be more efficient in this case, as you can see elaborated in this answer.

    so here is the fixed ver :

    …
    c = ISNULL(CAST(NULLIF((subquery), 0) AS varchar(10)), 'found')
    …
    

    Note, though, that if you end up switching to ISNULL, you should pay attention to the length of the second string ('found') if you ever decide to change it too. The thing is, ISNULL will (attempt to) cast the second argument to the exact type of the first argument, including the maximum length of the string specified (10 in this case). If your second argument becomes something like 'NULL or zero is found' instead of just 'found', ISNULL will not return the entire string but only the first 10 characters instead ('NULL or ze'). So you’ll need to remember to change the type of the first argument as well (to varchar(20), for instance).

    • 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 like this (pseudo code) Select ID, TEXT, 1, 2,
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have this query: (SELECT e.IdEvent,e.EventName,e.EventSubtitle,e.EventDescription,l.LocationName,e.EventVenue,EventStartDate,e.EventEndDate,e.EventHost,c.CategoryName,l.LocationCity,l.LocationState,e.isTBA, (SELECT s.status FROM jos_rsevents_subscriptions s WHERE s.IdUser =
I have this query: select BUnit, value from myTable where BUnit in (555,556,557,558) and
I have this query: SELECT * FROM sample INNER JOIN test ON sample.sample_number =
I have the following query: select r.people_code_id [People Code ID], r.resident_commuter [Campus6], c1.udormcom [AG],
I have this PHP code: $result = mysql_query(SELECT distinct om_quote_no from `porders` order by
Given a query (pseudo-code): <cfquery name=myquery>SELECT * FROM stuff</cfquery> How do I get rid
I have an object model that looks like this (pseudo code): class Product {
Consider this code: var query = from groupRole in CurrentItem.MEMGroupRoles select groupRole.MEMRole; this.AvailableRoles =

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.