简体   繁体   中英

Why does this SQL code not work in Microsoft Access but works in SQL Server Management Studio?

This code will not work in Microsoft Access but it works in Microsoft SQL Server Management Studio. What can I do to make this work in Access? I keep getting a

"Syntax error (missing operator) in query expression 'CAST(od.UnitPrice * od.Quantity * (1 + od.Discount) as decimal(10,2)) 'Order Total'.

Select TOP 5 
    c.CompanyName as 'Company Name', 
    CAST(od.UnitPrice * od.Quantity * (1 + od.Discount) as decimal(10, 2)) 'Order Total'
From 
    Customers as c
Join 
    Orders as o On c.CustomerID = o.CustomerID
Join 
    OrderDetails as od On o.OrderID = od.OrderID
Where 
    od.UnitPrice * od.Quantity * (1 + od.Discount) > 5000 
    AND c.Country IN ('Austria', 'Denmark', 'Germany', 'Ireland', 'Sweden')
Order By 
    o.OrderDate desc

you cant use CAST in Access... try with this options:

Cint -- cast to integer

Clng -- long

Cdbl -- double

Csng - single

Cstr - string

Cbool - boolean

CDec - Decimal

Example: SELECT clng(fieldName) FROM tableName

In your case:

Select TOP 5 
    c.CompanyName as 'Company Name', 
    CDec(od.UnitPrice * od.Quantity * (1 + od.Discount)) 'Order Total'
From 
    Customers as c
Join 
    Orders as o On c.CustomerID = o.CustomerID
Join 
    OrderDetails as od On o.OrderID = od.OrderID
Where 
    od.UnitPrice * od.Quantity * (1 + od.Discount) > 5000 
    AND c.Country IN ('Austria', 'Denmark', 'Germany', 'Ireland', 'Sweden')
Order By 
    o.OrderDate desc

Hope this help!

I don't think the CAST works OK in Access (if it exists). I would take the CAST out and change it for something like the CDec() function. You have to use one of the followings in general: https://support.office.com/en-ie/article/Type-Conversion-Functions-8ebb0e94-2d43-4975-bb13-87ac8d1a2202

The CAST and CONVERT column functions do not seem to work in MS Access.

Try the CInt or the CLng function, depending upon whether you want an integer (16 bit) or a long integer (32 bit).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM