Jacob
Jacob

Reputation: 11

Cast function in SQL server

Sample data:

1) 0.02500 = 25
2) 1.0000=100

I tried below code but it's converting 0.25 to 30

Select Try_cast(workcountry as decimal (10,2)*100

Upvotes: 0

Views: 102

Answers (3)

Denis Reznik
Denis Reznik

Reputation: 964

Multiply first, then cast. This should give you your an expected behavior.

SELECT TRY_CAST(0.02500*100 AS decimal(10,2))

Upvotes: 1

Dean Savović
Dean Savović

Reputation: 739

You can try this:

declare @workcountry float = 0.025;

select try_cast(@workcountry * 100. as decimal (10,2))

Upvotes: 0

Preston
Preston

Reputation: 8187

Your problem is the rounding, 0.025 with 2 decimal places rounds to 0.03. You then multiply that by 100 to get 3. Add an extra decimal place and you'll be fine

select try_cast(0.02500 as decimal (10,3)) * 100

Upvotes: 0

Related Questions