sql server - CAST and IsNumeric -
why following query return "error converting data type varchar bigint"? doesn't isnumeric make cast safe? i've tried every numeric datatype in cast , same "error converting..." error. don't believe size of resulting number problem because overflow different error.
the interesting thing is, in management studio, results show in results pane split second before error comes back.
select cast(myvarcharcolumn bigint) mytable isnumeric(myvarcharcolumn) = 1 , myvarcharcolumn not null group myvarcharcolumn
any thoughts?
isnumeric returns 1 if varchar value can converted number type. includes int, bigint, decimal, numeric, real & float.
scientific notation causing problem. example:
declare @temp table(data varchar(20)) insert @temp values(null) insert @temp values('1') insert @temp values('1e4') insert @temp values('not number') select cast(data bigint) @temp isnumeric(data) = 1 , data not null
there trick can use isnumeric returns 0 numbers scientific notation. can apply similar trick prevent decimal values.
isnumeric(yourcolumn + 'e0')
isnumeric(yourcolumn + '.0e0')
try out.
select cast(myvarcharcolumn bigint) mytable isnumeric(myvarcharcolumn + '.0e0') = 1 , myvarcharcolumn not null group myvarcharcolumn
Comments
Post a Comment