Sql server Ms Sql将varchar转换为大整数
我在下方的varchar中有客户电话号码值 客户电话号码值:(222)-3333-333Sql server Ms Sql将varchar转换为大整数,sql-server,varchar,bigint,Sql Server,Varchar,Bigint,我在下方的varchar中有客户电话号码值 客户电话号码值:(222)-3333-333 INSERT INTO DATABASE_1.dbo.CUSTOMER_TABLE ( Customer_Telephone_Number ) Select CONVERT(BIGINT,Customer_Telephone_Number) from DATABASE_2.DBO.CUSTOMER_TABLE 若ı尝试将Customer_Telephone_Number值插入数据库_1,我会得
INSERT INTO DATABASE_1.dbo.CUSTOMER_TABLE
(
Customer_Telephone_Number
)
Select
CONVERT(BIGINT,Customer_Telephone_Number)
from
DATABASE_2.DBO.CUSTOMER_TABLE
若ı尝试将Customer_Telephone_Number值插入数据库_1,我会得到以下异常
Error converting data type varchar to bigint.
那么我如何解决这个问题,或者我可以删除“(“and”)来解决问题呢?
任何帮助都将不胜感激
谢谢。一个快速而肮脏的方法是:
Select
CONVERT
(
BIGINT,
REPLACE
(
REPLACE
(
REPLACE
(
REPLACE
(
Customer_Telephone_Number,
' ',--Replace white spaces
''
),
'-',--Replace dashes
''
),
'(',--Replace open parenthesis
''
),
')',--Replace close parenthesis
''
)
)
from
DATABASE_2.DBO.CUSTOMER_TABLE
您可以运行以下操作以查看其他潜在字符:
select Customer_Telephone_Number
from DATABASE_2.DBO.CUSTOMER_TABLE
where ISNUMERIC
(
REPLACE
(
REPLACE
(
REPLACE
(
REPLACE
(
Customer_Telephone_Number,
' ',--Replace white spaces
''
),
'-',--Replace dashes
''
),
'(',--Replace open parenthesis
''
),
')',--Replace close parenthesis
''
)
) = 0
这将为您提供无法转换为
BIGINT
的电话号码列表。检查原因(例如,如果他们有一个
),并像我在示例中所做的那样将这些字符添加到替换中。您知道哪些可能是非数字字符吗?1-()-222-2296 2-(332)-228-1180 3-()--