Hello, all
Can you please tell me why this query:
SELECT POWER(0.5, 2), POWER(0.50, 2)
Returns 0.3 0.25
Instead of 0.25 0.25
Anton Sheplev:
Can you please tell me why this query:
SELECT POWER(0.5, 2), POWER(0.50, 2)
Returns 0.3 0.25
Instead of 0.25 0.25
All say after me: "SQL Server is designed for maximum
confusion!".
I know how it feels, I've run into exactly this thing
myself. And I thought, what!? Then I read the manu-
al ->
and learnt this:
The retun type of power() is the type of the first ar-
gument. The 0.3 has the type decimal(1, 1), and so the
return type is numeric(1,1) and you get 0.3. Intu-
itive? Maybe not. But this is what the manual says.
By the way, this is how you can tell the type of a nu-
meric literal:
DECLARE @s sql_variant = 0.3
SELECT sql_variant_property(@s, 'Basetype'),
sql_variant_property(@s, 'Precision'),
sql_variant_property(@s, 'Scale')
To avoid such surpises, say something like:
SELECT cast(power(cast @val as float) as decimal(10,3))
I think your outer `CAST' is superfluous because it can-
not increase actual precision.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 58:07:22 |
Calls: | 6,652 |
Calls today: | 4 |
Files: | 12,200 |
Messages: | 5,331,127 |