Solved

SQL Select Prior Ship Date Data

Posted on 2016-11-10
2
44 Views
Last Modified: 2016-11-16
Receiving incorrect syntax error near H in the last line of my code attempting to select all data from yesterday.

SELECT  CASE WHEN H.inv_num = 0 THEN SPACE(24)
             ELSE ' Invoice No.: ' + RTRIM(CAST(H.inv_num AS CHAR(24)))
        END AS cSKU ,
        ( (H.ar_post) ) AS AR_POST ,
        ( (H.bill_num) ) AS BILL_NUM ,
        ( (H.carton) ) AS CARTON ,
        ( (H.cncl_num) ) AS CNCL_NUM ,
        ( (H.consol_num) ) AS CONSOL_NUM ,
        ( (H.co_rate) ) AS CO_RATE ,
        ( (H.customer) ) AS CUSTOMER ,
        ( (H.division) ) AS DIVISION ,
        ( ( H.frgt_amt ) * H.co_rate ) AS FRGT_AMT ,
        ( (H.inv_num) ) AS INV_NUM ,
        ( (H.location) ) AS LOCATION ,
        ( (H.mani_num) ) AS MANI_NUM ,
        ( ( H.misc_amt ) * H.co_rate ) AS MISC_AMT ,
        ( (H.ord_num) ) AS ORD_NUM ,
        ( (H.ord_qty) ) AS ORD_QTY ,
        ( (H.pick_num) ) AS PICK_NUM ,
        ( (H.po_num) ) AS PO_NUM ,
        ( (H.proc_856) ) AS PROC_856 ,
        ( ( H.zsumorderextendedstandardcost ) * H.co_rate ) AS SFCORDEREXTENDEDCOST_B ,
        ( ( CASE WHEN H.ord_status <> 'C' THEN H.zordgrossext
                 ELSE 0
            END ) * H.co_rate ) AS SFCORDEREXTGROSSPRICE_B ,
        ( ( ROUND(H.zsumordextdetaildisc, 5) ) * H.co_rate ) AS SFCTOTALLINEDISCOUNT_B ,
        ( ( I.zinvoiced_amount ) * H.co_rate ) AS SFC_INV_AMOUNT_BH ,
        ( (CAST(CASE WHEN ( YEAR(H.ship_date) * 100 + MONTH(H.ship_date) ) = 190001
                     THEN 0
                     ELSE ( YEAR(H.ship_date) * 100 + MONTH(H.ship_date) )
                END AS VARCHAR(6))) ) AS SFSSHIP_DATE_YYYYMM_B ,
        ( (ORDAD2.S_STATE) ) AS SFS_SHIP_STATE_V ,
        ( (ORDAD2.S_ZIPCODE) ) AS SFS_SHIP_ZIP_V ,
        ( (H.shipper) ) AS SHIPPER ,
        ( (H.ship_date) ) AS SHIP_DATE ,
        ( (H.slsperson1) ) AS SLSPERSON1 ,
        ( (H.store) ) AS STORE ,
        ( ( H.tax_amt ) * H.co_rate ) AS TAX_AMT ,
        ( (H.weight) ) AS WEIGHT ,
        H.co_curr AS SFC_CURR_CODE ,
        H.co_rate AS SFC_RPTRATE
FROM    zvoordersh H
        LEFT OUTER JOIN zzxcbolr CB ON H.customer = CB.CUSTOMER
                                       AND H.bill_num = CB.BILL_NUM
        LEFT JOIN ( SELECT  pick_num ,
                            MIN(carton_id) AS CARTON_ID
                    FROM    zzoupsin
                    GROUP BY pick_num
                  ) LTN ON LTN.pick_num = H.pick_num
        LEFT OUTER JOIN zzxcustr CR ON H.customer = CR.customer
        LEFT OUTER JOIN zzxcslsr CS ON H.customer = CS.customer
                                       AND H.store = CS.store
                                       AND H.department = CS.department
                                       AND H.division = CS.division
                                       AND H.ord_type = CS.ord_type
                                       AND CS.active_rec = 'Y'
        LEFT OUTER JOIN vvoshprh I ON H.pkey = I.pkey
                                      AND H.inv_num = I.inv_num
        LEFT OUTER JOIN zzordad2 ORDAD2 ON H.ord_num = ORDAD2.ORD_NUM
                                           AND H.pick_num = ORDAD2.PICK_NUM
                                           AND H.inv_num = ORDAD2.INV_NUM
                                           AND H.cncl_num = ORDAD2.CNCL_NUM
WHERE   ( (H.division = 'KLL') )
        AND ( (H.inv_num > 0) )
        --AND ( (H.ship_date = {TS '2016-11-09 00:00:00' }) )
		 (H.ship_date = DATEADD(d,-1,GETDATE()) )

Open in new window

0
Comment
Question by:mburk1968
2 Comments
 
LVL 21

Accepted Solution

by:
JesterToo earned 500 total points
ID: 41882896
Put an "AND" in the beginning of line 66 like this...

AND  (H.ship_date = DATEADD(d,-1,GETDATE()) )
0
 
LVL 24

Expert Comment

by:Pawan Kumar
ID: 41883201
Yes looking at your code it looks like AND is missing. If it OR then make changes accordingly.

Also note that you are using lot of extra brackets which we can remove. 'AS' keyword can also be removed. We can directly type the name of the column,

Also use SINGLE character as alias name. This will help you with more readability.


SELECT  CASE WHEN H.inv_num = 0 THEN SPACE(24)
             ELSE ' Invoice No.: ' + RTRIM(CAST(H.inv_num AS CHAR(24)))
        END cSKU ,
        ( (H.ar_post) ) AS AR_POST ,
        ( (H.bill_num) ) AS BILL_NUM ,
        ( (H.carton) ) AS CARTON ,
        ( (H.cncl_num) ) AS CNCL_NUM ,
        ( (H.consol_num) ) AS CONSOL_NUM ,
        ( (H.co_rate) ) AS CO_RATE ,
        ( (H.customer) ) AS CUSTOMER ,
        ( (H.division) ) AS DIVISION ,
        ( ( H.frgt_amt ) * H.co_rate ) AS FRGT_AMT ,
        ( (H.inv_num) ) AS INV_NUM ,
        ( (H.location) ) AS LOCATION ,
        ( (H.mani_num) ) AS MANI_NUM ,
        ( ( H.misc_amt ) * H.co_rate ) AS MISC_AMT ,
        ( (H.ord_num) ) AS ORD_NUM ,
        ( (H.ord_qty) ) AS ORD_QTY ,
        ( (H.pick_num) ) AS PICK_NUM ,
        ( (H.po_num) ) AS PO_NUM ,
        ( (H.proc_856) ) AS PROC_856 ,
        ( ( H.zsumorderextendedstandardcost ) * H.co_rate ) AS SFCORDEREXTENDEDCOST_B ,
        ( ( CASE WHEN H.ord_status <> 'C' THEN H.zordgrossext
                 ELSE 0
            END ) * H.co_rate ) AS SFCORDEREXTGROSSPRICE_B ,
        ( ( ROUND(H.zsumordextdetaildisc, 5) ) * H.co_rate ) AS SFCTOTALLINEDISCOUNT_B ,
        ( ( I.zinvoiced_amount ) * H.co_rate ) AS SFC_INV_AMOUNT_BH ,
        ( (CAST(CASE WHEN ( YEAR(H.ship_date) * 100 + MONTH(H.ship_date) ) = 190001
                     THEN 0
                     ELSE ( YEAR(H.ship_date) * 100 + MONTH(H.ship_date) )
                END AS VARCHAR(6))) ) AS SFSSHIP_DATE_YYYYMM_B ,
        ( (ORDAD2.S_STATE) ) AS SFS_SHIP_STATE_V ,
        ( (ORDAD2.S_ZIPCODE) ) AS SFS_SHIP_ZIP_V ,
        ( (H.shipper) ) AS SHIPPER ,
        ( (H.ship_date) ) AS SHIP_DATE ,
        ( (H.slsperson1) ) AS SLSPERSON1 ,
        ( (H.store) ) AS STORE ,
        ( ( H.tax_amt ) * H.co_rate ) AS TAX_AMT ,
        ( (H.weight) ) AS WEIGHT ,
        H.co_curr AS SFC_CURR_CODE ,
        H.co_rate AS SFC_RPTRATE
FROM    zvoordersh H
        LEFT OUTER JOIN zzxcbolr CB ON H.customer = CB.CUSTOMER
                                       AND H.bill_num = CB.BILL_NUM
        LEFT JOIN ( SELECT  pick_num ,
                            MIN(carton_id) AS CARTON_ID
                    FROM    zzoupsin
                    GROUP BY pick_num
                  ) LTN ON LTN.pick_num = H.pick_num
        LEFT OUTER JOIN zzxcustr CR ON H.customer = CR.customer
        LEFT OUTER JOIN zzxcslsr CS ON H.customer = CS.customer
                                       AND H.store = CS.store
                                       AND H.department = CS.department
                                       AND H.division = CS.division
                                       AND H.ord_type = CS.ord_type
                                       AND CS.active_rec = 'Y'
        LEFT OUTER JOIN vvoshprh I ON H.pkey = I.pkey
                                      AND H.inv_num = I.inv_num
        LEFT OUTER JOIN zzordad2 ORDAD2 ON H.ord_num = ORDAD2.ORD_NUM
                                           AND H.pick_num = ORDAD2.PICK_NUM
                                           AND H.inv_num = ORDAD2.INV_NUM
                                           AND H.cncl_num = ORDAD2.CNCL_NUM
WHERE   H.division = 'KLL'
        AND H.inv_num > 0
        AND H.ship_date = DATEADD(d,-1,GETDATE())

Open in new window

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
affinity mask in sql server 1 23
SQL Server stored proc 2 13
Sql query 107 29
Anyway to make these 2 SQL statements into one? 13 25
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now