I've inherited a table in a third part app that contains both strings and ints - I want to split the table but want to know the fastest way to determine if the value is an int? Column is varchar(250)
e.g.
Save
123456
Edit
Save
123455
Delete
123444
I've inherited a table in a third part app that contains both strings and ints - I want to split the table but want to know the fastest way to determine if the value is an int? Column is varchar(250)
e.g.
Save
123456
Edit
Save
123455
Delete
123444
I would suggest using TRY_CONVERT
, which will return NULL
if the value cannot be converted:
SELECT TRY_CONVERT(int, YourColumn) AS intColumn
FROM YourTable
--WHERE TRY_CONVERT(int, YourColumn) IS NOT NULL --If you only want rows that converted.
If you prefer the syntax, you also have TRY_CAST
(TRY_CAST(YourColumn AS int)
).
I would use TRY_CONVERT.
declare @Something table(SomeVal varchar(50))
insert @Something values
('Save')
, ('123456')
, ('Edit')
, ('Save')
, ('123455')
, ('Delete')
, ('123444')
select *
from @Something s
where TRY_CONVERT(int, s.SomeVal) is not null
One approach would be to use SQL Server's enhanced LIKE
operator:
SELECT col
FROM yourTable
WHERE col NOT LIKE '%[^0-9]%';
Using TRY_CONVERT
might also be an option, but only if you are using a more recent version of SQL Server.