开发者

Select records that fail casting varchar column to bigint

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜