How in T-SQL to multiplying two different datatypes?
How can I multiply two types in SQL Server?
I have one column with cost, whose datatype is money
, and another integer
column that has the number of those products.
How can I mult开发者_如何学编程iply these two values to result in a value representing the cost?
Example: $2000 * 2
Use:
SELECT t.number_of_items * t.cost_of_item AS total_cost
FROM PRODUCTS t
DECLARE @UNITS int
DECLARE @UnitCost money
SET @UNITS = 100
SET @UnitCost = 2.20
SELECT (@UNITS * CONVERT(decimal(18,2), @unitcost)) as Total
Replace the @variables with your column names.
Dependantly of the types implicit or explicit convertion will take place (as @KM commented).
Implicit convertion is done automatically by SQL Server (which is your case). If it is not possible Error occurs.
Explicit convertion is done by developer (in SQL server using CAST
or CONVERT
). If convertion is not possible Error occurs.
For more information see SQL Server CAST and CONVERT help.
精彩评论