Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using ISNUMERIC() with sql_variant SQL Server

How can I use ISNUMERIC() function with sql_variant datatype?

This following code doesn't work

DECLARE @x sql_variant 
SET @x = 3 
IF ISNUMERIC(@x) = 1 
 SELECT 'Numeric'
 ELSE
SELECT ' NOT' 

Error :

Msg 8116, Level 16, State 1, Line 4
Argument data type sql_variant is invalid for argument 1 of isnumeric function.

But this works

IF ISNUMERIC(CAST(@x AS INT)) = 1 

is there is any way of making this happened with out CAST()

Haven't found any thing useful on google regarding this issue thanks , I'm using SQL Server 2008 R2

like image 706
Mina Gabriel Avatar asked Apr 26 '26 02:04

Mina Gabriel


1 Answers

Casting to int doesn't really make sense. Your conversion will fail (when something else than an integer appears) and you'll get an error.

You can, however, cast to nvarchar(max) - this should always work and your ISNUMERIC function will behave as expected

like image 188
Filip Avatar answered Apr 30 '26 12:04

Filip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!