2

我有一个TELEPHONE_NUMBER这样的varchar

Value : "1112223344"

我需要在值中添加“-”,如下所示:

111-222-33-44

那么如何-在我的 SQL Server 存储过程中添加我的电话号码值呢?

CREATE PROCEDURE SP_TELEPHONE_NUMBER
   (@TELEPHONE_NUMBER VARCHAR(4000))
AS
BEGIN
   INSERT INTO DOSYA_ARSIV(TELEPHONE_NUMBER)
   VALUES(@TELEPHONE_NUMBER)
END
4

2 回答 2

2

只是一个选择:

select SUBSTRING('1112223344',1,3)+'-'+
       SUBSTRING('1112223344',4,3)+'-'+
       SUBSTRING('1112223344',7,2)+'-'+
       SUBSTRING('1112223344',9,2)

在你的情况下:

CREATE PROCEDURE SP_TELEPHONE_NUMBER
   (@TELEPHONE_NUMBER VARCHAR(4000))
AS
BEGIN
   INSERT INTO DOSYA_ARSIV(TELEPHONE_NUMBER)
   VALUES
   (
       SUBSTRING(@TELEPHONE_NUMBER,1,3)+'-'+
       SUBSTRING(@TELEPHONE_NUMBER,4,3)+'-'+
       SUBSTRING(@TELEPHONE_NUMBER,7,2)+'-'+
       SUBSTRING(@TELEPHONE_NUMBER,9,2)
   )
END

正如 noobob 在他的评论中提到的那样,您可能将其作为 INT 类型(INT、BIGINT 或类似的东西)并处理它在前端的显示方式。例如,在 C# 中,您可以将其设置为:

TELEPHONE_NUMBER.ToString("###-###-##-##");

Another comment would be that defining the expected argument as VARCHAR(4000) is way too much. Though it might not be very bad, it is a good point to define arguments or variables as close to expected input as you can. In your case i would say that something like VARCHAR(30) would be enough.

于 2013-11-01T07:52:24.833 回答
0

在 MS SQL 服务器中使用STUFF函数

SET @TELEPHONE_NUMBER=STUFF(STUFF(STUFF(@TELEPHONE_NUMBER,4,0,'-'),8,0,'-'),11,0,'-')
于 2013-11-01T07:22:35.723 回答