开发者

Select records that fail casting varchar column to bigint

开发者 https://www.devze.com 2023-01-27 19:50 出处:网络
is there an easy way to get all records where casting a varchar column to an bigint would fail? This causes a conversion error:

is there an easy way to get all records where casting a varchar column to an bigint would fail? This causes a conversion error:

SELECT CAST(IMEI AS BIGINT)FROM RMA

Use this sql as example:

if OBJECT_ID('tempdb..#RMA') is not null
    DROP开发者_如何学C TABLE #RMA

CREATE TABLE #RMA
( 
    IMEI VARCHAR(20)
)
INSERT INTO  #RMA(IMEI)VALUES('352382021485772')
INSERT INTO  #RMA(IMEI)VALUES('352022033456409')
INSERT INTO  #RMA(IMEI)VALUES('BN332VWY653577440220')

SELECT * FROM  #RMA
SELECT CAST(IMEI AS BIGINT)FROM #RMA

DROP TABLE #RMA

So, in this example i need only the record with IMEI='BN332VWY653577440220'.

Thank you.


Try the T-SQL ISNUMERIC function:

SELECT IMEI 
FROM  #RMA
WHERE ISNUMERIC(IMEI) = 0   -- not numeric

See the MSDN SQL Server Books Online docs for it.

0

精彩评论

暂无评论...
验证码 换一张
取 消