user87
user87

Reputation: 81

Datatype Conversion

I am trying to execute the following Query

select distinct pincode as Pincode,CAST(Date_val as DATE) as Date,
SUM(cast(megh_38 as int)) as 'Postage Realized in Cash',
SUM(cast(megh_39 as int)) as 'MO Commission',
from dbo.arrow_dtp_upg
group by pincode,Date_Val

but I am getting an error "Conversion failed when converting the nvarchar value '82.25' to data type int."

Am I using a wrong data type?

Upvotes: 0

Views: 883

Answers (3)

Phil
Phil

Reputation: 43021

The string "82.25" represents a float (or a decimal) not an int, so use cast(megh_38 as float) if you need a float.

If you require the integer part only then use floor(cast(megh_38 as float)).

Upvotes: 1

sarutobi
sarutobi

Reputation: 26

One possible problem is delimiter (dot vs comma). Questions - why you prefer nvarchar for store float or int values? Maybe decimal or float is better solution?

Upvotes: 0

Krishna
Krishna

Reputation: 636

You could try this:

cast(megh_38 as decimal(10,2))
cast(megh_39 as decimal(10,2))

Upvotes: 0

Related Questions