Microsoft
Software
Hardware
Network
Question : Help filtering on derived column
I have the following query which returns a rows that contain a date and an amount. I tried to modify the WHERE clause to return only a single row for a specific date. The query executes but no data is returned. Can someone clue me into the problem?
SELECT CONVERT(varchar, DATEADD(d, tblPaymentTerms.LateAfterD
ays, tblInvoice.DateSubmitted),
111) AS DueDate, SUM(COALESCE (details.SubTotal, 0)
- COALESCE (payments.Total, 0) + COALESCE (tblInvoice.Shipping, 0)) AS BalanceDue
FROM tblPaymentTerms INNER JOIN
tblInvoice ON tblPaymentTerms.ID = tblInvoice.fkPaymentTerms LEFT OUTER JOIN
(SELECT fkInvoice, SUM(Amount) AS Total
FROM tblInvoicePayment
GROUP BY fkInvoice) AS payments ON payments.fkInvoice = tblInvoice.ID LEFT OUTER JOIN
(SELECT d.fkInvoice, SUM((d.Quantity * d.UnitPrice) * (1 + CASE d .Taxable WHEN 1 THEN i.SalesTaxRate ELSE 0 END)) AS SubTotal
FROM tblInvoiceDetail AS d INNER JOIN
tblInvoice AS i ON i.ID = d.fkInvoice
GROUP BY d.fkInvoice) AS details ON details.fkInvoice = tblInvoice.ID
WHERE (tblInvoice.fkInvoiceStatu
s IN (2, 3, 4, 5, 6, 7))
GROUP BY CONVERT(varchar, DATEADD(d, tblPaymentTerms.LateAfterD
ays, tblInvoice.DateSubmitted),
111)
ORDER BY duedate
If I change the WHERE clause to the following I have no rows returned even though there is valid data.
WHERE (tblInvoice.fkInvoiceStatu
s IN (2, 3, 4, 5, 6, 7)) AND (CONVERT(varchar, DATEADD(d, tblPaymentTerms.LateAfterD
ays, tblInvoice.DateSubmitted),
111) = '06/01/2010')
Answer : Help filtering on derived column
The derived actually results in YYYY/MM/DD Format ..so change your Where clause to
(CONVERT(varchar, DATEADD(d, tblPaymentTerms.LateAfterD
ays, tblInvoice.DateSubmitted),
111) = '2010/06/01')
Check these date formats
SELECT CONVERT(VARCHAR,GETDATE(),
101) -- MM/DD/YYYY
SELECT CONVERT(VARCHAR,GETDATE(),
111) -- YYYY/MM/DD
Random Solutions
Task Manager and regedit 'disabled by administrator', unable to access Safe Mode, Antivirus deactivated
Failed to allocate port error when configuring EM (Oracle 11g)
Routing Between VLAN's on ASA 5505
Windows 2003 Interview Questions
How do I delegate control to a user to ONLY remotely use Get-MessageTrackingLong cmdlet?
Password Protect Back End of an Access Database
PayPal Express Checkout Module
How do we use GPO (Group Policy) to REMOVE trusted sites from I.E. Trusted Sites Zone on workstations on a domain?
How do I add image to the lotus notes signature
C# int with variable as part of int name