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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:35:54+00:00 2026-06-18T14:35:54+00:00

I am working on this SQL function below, but couldn’t figure out how to

  • 0

I am working on this SQL function below, but couldn’t figure out how to make the code condition WHERE works based on the flag (@PastDueFlag, either 0 or 1 passing).

Please assume that entire query inside this function is working correctly. The only line I like to change is right under WHERE 1 = 1 “AND DATEDIFF(“d”, InvoiceDueDate, GETDATE()) > 0″.

I like to include this condition in the query when the parameter @PastDueFlag = 1, else don’t include this line in the WHERE query.

Is there a way to do this?

Thanks,

ALTER FUNCTION [dbo].[TotalARDueByCompanyID]
(
    -- Add the parameters for the function here
    @CompanyID AS INTEGER,
    @PastDueFlag AS INTEGER
)
RETURNS Float
AS
BEGIN
    DECLARE @TotalARDue AS Float

     SET @TotalARDue = (
        SELECT SUM(CAST(GrandTotal AS NUMERIC(9,2)) + CAST(ISNULL(LinkedTxnAmount, 0) AS NUMERIC(9,2))) AS TotalARDue
        FROM (
            select i.OrderID, InvoiceID, cp.CustomerPO, o.CompanyID, i.CreditMemo
            , CONVERT(VARCHAR(10),(InvoiceDate + (SELECT TermDays FROM dbo.ERP_CompanyTerms WHERE TermID = i.TermID)), 101) AS InvoiceDueDate
            , (isnull(FreightCost,0) + isnull(TaxState,0) + isnull(TaxCounty, 0) + isnull(TaxCity, 0) )
            +
            (SELECT SUM(ExtQty * UnitSell) FROM ERP_InvoiceDetail WHERE invoiceID = i.InvoiceID) as GrandTotal
                --, isnull(i.FreightCost, 0) as FreightCost, i.ShipVia, i.WhoPays, i.Status
            , CONVERT(VARCHAR(10), i.CreatedDate, 101) AS CreatedDate
            , CONVERT(VARCHAR(10), i.InvoiceDate, 101) AS InvoiceDate
            , (select Name from ERP_Company c where c.CompanyID = o.CompanyID) as CompanyName
            , (select first_name + ' ' + last_name from employee where tech_id = i.createdBy) as CreatedBy
            , (SELECT em2.first_name + ' ' + em2.last_name) AS SalesRep
            , (SELECT em2.tech_id) AS SalesRepID
            , o.SOTypeInt AS SOType, em2.WH_RegionID AS Region, i.Status, qv.SFOppt
            , (
                SELECT 
                    (SELECT CASE
                        WHEN CreditMemo = 1 THEN
                            SUM(qi.LinkedTxnAmount) * -1
                        ELSE
                            SUM(qi.LinkedTxnAmount)
                      END
                ) 
                FROM dbo.ERP_Invoice i2
                LEFT JOIN dbo.QuickBooks_InvoiceLinked qi ON RefNumber = CAST(i2.InvoiceID AS VARCHAR(8))
                WHERE 1 = 1
                AND InvoiceID = i.invoiceID --   305763, 304990
                AND LinkedTxnTxnType <> 
                    CASE
                        WHEN i2.CreditMemo = 1 THEN
                            'CreditMemo'
                        ELSE
                            ''
                    END
                GROUP BY CreditMemo
            ) AS LinkedTxnAmount
            from ERP_Invoice i
            LEFT JOIN ERP_Order o on o.OrderID = i.OrderID
            INNER JOIN ERP_Company c on o.CompanyID = c.CompanyID  
            LEFT JOIN Employee em on em.Tech_ID = i.CreatedBy
            LEFT JOIN Employee em2 ON em2.Tech_ID = o.SalesPersonID
            LEFT JOIN ERP_QuoteVer qv ON qv.QuoteVerID = o.QuoteVerID
            LEFT JOIN dbo.ERP_CustomerPO cp ON cp.CustomerPOID = i.CustomerPOID
            where 1 = 1
        ) a
        WHERE
        1 = 1
        AND DATEDIFF("d", InvoiceDueDate, GETDATE()) > 0
        AND CAST(GrandTotal AS NUMERIC(9,2)) + CAST(ISNULL(LinkedTxnAmount, 0) AS numeric(9,2)) > 0
        AND CompanyID = @CompanyID
        AND Status in ('New', 'Completed')
        AND SOType = 'Production'
    )

    -- Return the result of the function
    RETURN @TotalARDue

END
  • 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-18T14:35:55+00:00Added an answer on June 18, 2026 at 2:35 pm

    I think all you have to do is integrate your parameter value into your where clause:

    WHERE ( (@PastDueFlag =1 and DATEDIFF("d", InvoiceDueDate, GETDATE()) > 0) or 
            (@PastDueFlag = 0) ) and ...
    

    also, you shouldn’t need the 1=1 criteria in your WHERE clause

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

Sidebar

Related Questions

This sql works in phpmyadmin but not working in codeigniter php function getProgramsHomepage(){ $data
I'm working with SQL, and I can't seem to figure this out for the
I'm ripping my noob hair out here. Can't understand why below code isn't working.
I'm not sure why this SQL query is not working. I'm new to SQL/PHP
Im working in an SQL Query like this: (sorted by the station visits) TRAIN_ID
I am trying to get this awesome JQuery plugin working with SQL Server. I
I'm working on a SQL Server 2008 database. This database has two tables. Book
I am tired of working this out myself , i cannot understand the way
i am new in programming under linux and trying to get working this code:
this SQL clause is working OK: SELECT * FROM `sf_guard_user` WHERE nombre_apellidos LIKE REPLACE('Mar

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.