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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:05:00+00:00 2026-05-27T22:05:00+00:00

This is a legacy query that I am cleaning up and I am getting

  • 0

This is a legacy query that I am cleaning up and I am getting confused with this case statement so any help would be greatly appreciated.

SELECT CASE
  WHEN bitdelivered = 1 THEN
    '' + CONVERT(CHAR(10), deliv.dtmdeliverydate, 101)
    + '                '
    + 'þ' + '
' -- this is a carriage return, do not remove it
  WHEN bitdelivered = 0 AND deliv.dtmdeliverydate < Getdate() THEN
    '' + CONVERT(CHAR(10), deliv.dtmdeliverydate, 101)
    + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    + '&#168;' + '
'
  ELSE
    '' + CONVERT(CHAR(10), deliv.dtmdeliverydate, 101)
    + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    + '&#168;' + '
'
END

Like I said it is very dirty, however when I run the entire query, if I get a 0 for bitdelivered, the casestatement works as it should, however if I get a 1 returned, then I return a null instead of the else block. Any ideas?

EDIT I have edited the code as shown below for easier readability and I still get null…

select
CONVERT(CHAR(10), deliv.dtmdeliverydate, 101) +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
CASE 
    WHEN bitdelivered = 1 THEN '&#254;'
    ELSE '&#168;'
END + '
'

EDIT x 2 Here is the entire (very nasty I know) sql query, I did not write this I am just trying to clean it up…

SELECT dbo.tblrequirements.guidrequirementid,
       tblcontracting.strdescription                             AS
       strcontracting,
       dbo.viewawardnumbers.strcontractnumber                    AS
       strawardnumber,
       Ltrim(Rtrim(dbo.viewawardnumbers.strtonumber))            AS strtonumber,
       dbo.tblrequirements.strdescription,
       dbo.tblezquerycontractvalue.curtotalvalue,
       dbo.tblcodescontractvehicles.strdescription               AS
       strcontractvehicle,
       tblcustomer.stracronym                                    AS strcustomer,
       dbo.tblcodesrequirementstatuses.strdescription            AS strstatus,
       Substring(dbo.tblrequirements.strnotes, 0, 512)           AS strnotes,
       Coalesce (dbo.tblrequirements.guidfromid,
       '00000000-0000-0000-0000-000000000000'
       )                                                         AS guidfromid,
       Coalesce (dbo.viewteammembers.guidpersonid,
       '00000000-0000-0000-0000-000000000000')                   AS guidpersonid
       ,
       dbo.viewcurrentoptions.dtmstart,
       dbo.viewcurrentoptions.dtmend,
       Dateadd(d, -60, dbo.viewcurrentoptions.dtmend)            AS
       dtm1stnoticedue,
       Dateadd(d, -30, dbo.viewcurrentoptions.dtmend)            AS
       dtm2ndnoticedue,
       dbo.tblcontractdates.dtmcontractstart,
       dbo.tblcontractdates.dtmcontractend,
       Isnull(dbo.viewteammembers.strshortname, ' Not Assigned') AS strshortname
       ,
       dbo.tblezqueryfunding.curtotalfunded,
       CASE
         WHEN ( dbo.tblcodesrequirementstatuses.strdescription =
                'Pre-Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Source Selection'
              ) THEN 'P'
         WHEN ( Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'IT'
                AND Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'OPS'
              ) THEN
         'S'
         ELSE 'C'
       END                                                       AS strproctype,
       dbo.tblcodesprocurementtypes.strcode,
       dbo.tblcodesprocurementtypes.strdescription               AS strprocdesc,
       deliveries.dtmdeliverydate,
       deliveries.bitdelivered,
       CASE
         WHEN ( dbo.tblcodesrequirementstatuses.strdescription =
                'Pre-Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Source Selection'
              ) THEN 'Initial Start Date'
         WHEN ( Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'IT'
                AND Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'OPS'
              ) THEN
         'POP'
         ELSE 'Delivery Date'
       END                                                       AS
       colpopdelivdt,
       CASE
         WHEN ( dbo.tblcodesrequirementstatuses.strdescription =
                'Pre-Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Source Selection'
              ) THEN 'Req Award Date'
         WHEN ( Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'IT'
                AND Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'OPS'
              ) THEN
         'Option Notice'
         ELSE 'Delivered'
       END                                                       AS
       coloptnotdeliv,
       tblpresolicitations.dtmcontacted,
       tblpresolicitations.dtmrequiredby,
       tblpresolicitations.dtmawardnotice,
       strjobid,
       bitprimary
FROM   dbo.tblrequirements
       INNER JOIN dbo.tblcodesrequirementstatuses
         ON dbo.tblcodesrequirementstatuses.strcode =
            dbo.tblrequirements.strstatusid
       LEFT OUTER JOIN dbo.tblezquerycontractvalue
         ON dbo.tblezquerycontractvalue.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.viewawardnumbers
         ON dbo.viewawardnumbers.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblezqueryfunding
         ON dbo.tblezqueryfunding.strawardnumber =
            dbo.viewawardnumbers.strawardnumber
       LEFT OUTER JOIN dbo.viewcurrentoptions
         ON dbo.tblrequirements.guidrequirementid =
            dbo.viewcurrentoptions.guidrequirementid
       LEFT OUTER JOIN dbo.viewteammembers
         ON dbo.viewteammembers.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblcontracts
         ON dbo.tblcontracts.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblcontractdates
         ON dbo.tblcontractdates.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblcodescontractvehicles
         ON dbo.tblcodescontractvehicles.strcode = dbo.tblcontracts.strvehicleid
       LEFT OUTER JOIN dbo.tblorganizations AS tblcontracting
         ON dbo.tblrequirements.guidfromid = tblcontracting.guidorgid
       LEFT OUTER JOIN dbo.tblorganizations AS tblcustomer
         ON dbo.tblrequirements.guidtoid = tblcustomer.guidorgid
       LEFT OUTER JOIN dbo.tblpresolicitations
         ON dbo.tblpresolicitations.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblcodesprocurementtypes
         ON dbo.tblcodesprocurementtypes.strcode =
            dbo.tblpresolicitations.strprocurementtypeid
       LEFT OUTER JOIN (SELECT guidrequirementid,
                               REPLACE(REPLACE(REPLACE (
(select
ISNULL(CONVERT(CHAR(10), deliv.dtmdeliverydate, 101), '(no date)') +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
CASE 
    WHEN bitdelivered = 1 THEN '&#254;'
    ELSE '&#168;'
END + '
'



                                       /*(SELECT CASE
                                                 WHEN bitdelivered = 1
                                               THEN
                                                 ''
                                                 +
                                                 CONVERT(CHAR(10),
       deliv.dtmdeliverydate,
       101) +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
+ '&#254;' + '

'
WHEN bitdelivered = 0
AND deliv.dtmdeliverydate < Getdate() THEN
'' +
CONVERT(CHAR(10), deliv.dtmdeliverydate, 101) +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
+ '&#168;' + '

'
ELSE 
'' + CONVERT(CHAR(10), deliv.dtmdeliverydate, 101) +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
+ '&#168;' + '

'
END*/
FROM   tblclins clins
INNER JOIN tblcommodities commod
ON commod.guidclinid = clins.guidclinid
INNER JOIN tbldeliveries deliv
ON deliv.guidcommodityid = commod.guidcommodityid
WHERE  clins.guidrequirementid = req.guidrequirementid
AND deliv.bitdelivered = 0
ORDER  BY guidrequirementid,
deliv.dtmdeliverydate
FOR XML PATH('')), '&lt;', '<'), '&gt;', '>'), '&amp;', '&') AS dtmdeliverydate
,
Stuff ((SELECT '|' + CAST(bitdelivered AS CHAR(1))
FROM   tblclins clins
INNER JOIN tblcommodities commod
ON commod.guidclinid = clins.guidclinid
INNER JOIN tbldeliveries deliv
ON deliv.guidcommodityid = commod.guidcommodityid
WHERE  clins.guidrequirementid = req.guidrequirementid
ORDER  BY guidrequirementid
FOR XML PATH('')), 1, 1, '')                          AS bitdelivered
FROM   tblrequirements req
GROUP  BY guidrequirementid) deliveries
  ON deliveries.guidrequirementid = dbo.tblrequirements.guidrequirementid
  • 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-27T22:05:00+00:00Added an answer on May 27, 2026 at 10:05 pm

    The clause of the CASE Statement is this

    WHEN bitdelivered = 1
    

    So it will never go into the ELSE Statement which you seem to think it should by this statement "however if I get a 1 returned, then I return a null instead of the else block"

    If you’re getting nulls from this case statement its because deliv.dtmdeliverydate is NULL and CONCAT_NULL_YIELDS_NULL is set to ON

    You should fix this using COALESCE or ISNULL. You could change the setting CONCAT_NULL_YIELDS_NULL to OFF, but I don’t recommend this as this setting surprises people.

    However as we figured out during our discussion the WHERE Clause on the derived table

    WHERE clins.guidrequirementid = req.guidrequirementid AND deliv.bitdelivered = 0 means that

    CASE WHEN bitdelivered = 1

    will never be evaluated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Warning: This is inherited legacy code that was initially put together in the early
I got some legacy code that has this: <?PHP if(isset($_GET['pagina'])==homepage) { ?> HtmlCode1 <?php
I came across some legacy code that contains a function like this: LPCTSTR returnString()
I need help re-factoring this legacy LINQ-SQL code which is generating around 100 update
SELECT ... WHERE expr1 and expr2 or expr3 or expr4 this query executes in
I need to run a query against a legacy table that stores URL encoded
So, my problem is this. I have a legacy MySQL database that I'm building
I am working with a legacy database schema that looks like this: product_table table
I got an email today saying: In every case that we have examined, this
I'm working on a sheet that accesses a legacy data source via MS Query.

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.