SQL syntax in SQL Server 2019 (1 Viewer)

nector

Member
Local time
Today, 10:14
Joined
Jan 21, 2020
Messages
368
Can someone help me correct what is wrong with these letters in the SQL syntax below:

(1) "D" & "C3" line 16


SELECT
DISTINCTROW tblProducts.ProductID,
tblProducts.ProductName,
tblProducts.BarCode,
tblSalesTax.TaxClass,
tblPricing.Prices,
tblPricing.RRP,
tblSalesTax.VatRate,
tblSalesTax.Tourism,
tblSalesTax.Insurance,
tblSalesTax.TourismLevy,
tblPricing.TaxInclusive,
tblSalesTax.InsuranceRate,
tblSalesTax.InsuranceRate AS Premium,
IIf(
([TaxClass] = "D"),
Round(
IIf(([TaxClass] = "D"), [Prices],([Prices] / 1.16)),
2
),
Round(
IIf(([TaxClass] = "C3"), [Prices],([Prices] / 1.16)),
2
)
) AS ExportPrice,
tblPricing.NoTaxes,
tblProducts.Sales
FROM
tblSalesTax
INNER JOIN (
tblProducts
INNER JOIN (
tblcartergory
INNER JOIN tblPricing ON tblcartergory.CartID = tblPricing.CartID
) ON tblProducts.ProductID = tblPricing.ProductID
) ON tblSalesTax.IDClass = tblPricing.IDClass
WHERE
(((tblProducts.Sales) = Yes))
ORDER BY
tblProducts.ProductID DESC;
 

theDBguy

I’m here to help
Staff member
Local time
Today, 00:14
Joined
Oct 29, 2018
Messages
21,477
Have you tried using single quotes in place of double quotes?
 

GPGeorge

Grover Park George
Local time
Today, 00:14
Joined
Nov 25, 2004
Messages
1,877
Yup, theDBGuy is right about the proper string delimiter for T-SQL as opposed to Access SQL.
 

plog

Banishment Pending
Local time
Today, 02:14
Joined
May 11, 2011
Messages
11,648
Logically:

Code:
IIf(
   ([TaxClass] = "D"),
   Round(
     IIf(([TaxClass] = "D"), [Prices],([Prices] / 1.16)),
     2
   ),
   Round(
     IIf(([TaxClass] = "C3"), [Prices],([Prices] / 1.16)),
     2
   )
) AS ExportPrice,

That only needs to be one IF instead of 3.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:14
Joined
Feb 19, 2002
Messages
43,302
I don't see any difference in the two calculations. AND there is no Else option. AND the Round() can go outside the primary IIf()
 

sonic8

AWF VIP
Local time
Today, 09:14
Joined
Oct 27, 2015
Messages
998
Can someone help me correct what is wrong with these letters in the SQL syntax below:

(1) "D" & "C3" line 16
As others pointed out correctly, the primary delimiter for string literals in T-SQL is a single quote.
You can also enable double quotes as delimiter by setting SET QUOTED_IDENTIFIER OFF. This can also be configured in the ODBC connection settings. However, I would only see this as a temporary solution during an migration or for legacy applications. Some SQL Server functionality requires QUOTED_IDENTIFIER=ON. So, I would try to only use single quotes for any future development work.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:14
Joined
Feb 19, 2002
Messages
43,302
[Prices],([Prices] / 1.16)
Clearly, I'm missing something.

How is this:
Code:
IIf(
   ([TaxClass] = "D"),
   Round(
     IIf(([TaxClass] = "D"), [Prices],([Prices] / 1.16)),
     2
   ),
   Round(
     IIf(([TaxClass] = "C3"), [Prices],([Prices] / 1.16)),
     2
   )
) AS ExportPrice,
Different from This:
Code:
 Round(IIf(TaxClass = "D" OR TaxClass = "C3", 
      Prices, (Prices / 1.16)),2) AS ExportPrice,
 

cheekybuddha

AWF VIP
Local time
Today, 08:14
Joined
Jul 21, 2014
Messages
2,280
Round(IIf(TaxClass = "D" OR TaxClass = "C3", Prices, (Prices / 1.16)),2) AS ExportPrice,
Or different from:
Code:
Round(
  Prices / IIF(TaxClass IN ('D', 'C3'), 1, 1.16),
  2
) AS ExportPrice
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 17:14
Joined
Jan 20, 2009
Messages
12,852
Using a hard coded formula isn't a great idea.

A better plan would be to store the factor in a table related to TaxClass. Any future changes to the values or even a new TaxClass can be simply updated in the table.
 

cheekybuddha

AWF VIP
Local time
Today, 08:14
Joined
Jul 21, 2014
Messages
2,280
SQL:
SELECT TOP 20
  i.InvoiceID,
  i.RegNo,
  i.Amount,
  ROUND(
    i.Amount * IIF(i.RegNo IN ('772 LTM', 'LC10 CPK'), 1, 2),
    2
  ) AS ExportPrice
FROM Invoices i
;

Code:
InvoiceID|RegNo   |Amount |ExportPrice|
---------+--------+-------+-----------+
    10001|LC10 CPK| 126.56|     126.56|
    10002|LC10 CHX| 461.56|     923.12|
    10003|LF12 ZRG| 341.38|     682.76|
    10004|772 LTM |1253.55|    1253.55|
    10005|772 LTM |5179.08|    5179.08|
    10006|LJ58 NTM| 238.80|     477.60|
    10007|LC10 CPK| 387.28|     387.28|
    10008|LC10 CHX| 645.26|    1290.52|
    10009|LC10 CPK| 387.28|     387.28|
    10010|LC10 CHY| 401.60|     803.20|
    10011|LC10 CPK| 126.56|     126.56|
    10012|LJ58 NTM| 313.80|     627.60|
    10013|LC10 CHY| 697.47|    1394.94|
    10014|LM10 MEV|3685.55|    7371.10|
    10015|        |1265.62|    2531.24|
    10016|RO12 UCJ| 312.41|     624.82|
    10017|LC10 CPK| 216.00|     216.00|
    10018|LC10 CPK| 246.00|     246.00|
    10019|LC10 CPK| 222.50|     222.50|
    10020|LF12 ZRC|2456.20|    4912.40|
 

Users who are viewing this thread

Top Bottom