How to convert nvarchar
to numeric
?
I got this error:
Error converting data type nvarchar to numeric.
This is my query
SELECT
strIpay88Amt,
CONVERT(NUMERIC(18, 2), strIpay88Amt) AS iPay
FROM
tblCurrTrxMaster
Assuming that there are string values in strIpay88Amt column. You can use following to convert only numeric values.
select cast(strIpay88Amt as numeric) from tblCurrTrxMaster
where ISNUMERIC(strIpay88Amt) = 1
And following to fetch string values.
select strIpay88Amt from tblCurrTrxMaster where ISNUMERIC(strIpay88Amt) = 0
尝试强制转换语法:
SELECT strIpay88Amt, CAST(strIpay88Amt AS NUMERIC) AS iPay FROM tblCurrTrxMaster
There must be some entry in your table that is NOT a valid number and thus your conversion fails - and rightfully so.
So in order to find these entries, you can try this:
SELECT
strIpay88Amt,
TRY_CONVERT(NUMERIC(18, 2), strIpay88Amt) AS iPay
FROM
dbo.tblCurrTrxMaster
WHERE
TRY_CONVERT(NUMERIC(18, 2), strIpay88Amt) IS NULL
This will use the TRY_CONVERT
function to attempt the conversion - and with this WHERE
clause, you'll only get back all those entries that could not be converted to a valid NUMERIC
.
While NULL
will "convert" properly to a numeric, an empty string ( ''
) for instance will already cause an error......
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.