位置:首页 > 编程专栏 > SQL Server

SQL Server下解决手机号中间4位, 手机号星号显示


--SQL Server下解决手机号中间4位隐藏方案
CREATE TABLE [TestUser](
		[Id] [int] PRIMARY KEY([Id]),
		[Name] [nvarchar](50),
		[Mobile] [nvarchar](50)
);

INSERT INTO [TestUser]VALUES(1,'张三','13312341234');
INSERT INTO [TestUser]VALUES(2,'李四','');
INSERT INTO [TestUser]VALUES(3,'小明',null);

--1.
SELECT [Id],[Name],LEFT([Mobile],3)+'****'+RIGHT([Mobile],4) as [Mobile] from [TestUser]

--2.
SELECT
	[Id],
	[Name],
	(Case When [Mobile] IS NOT NULL Then (LEFT([Mobile],3)+'****'+RIGHT([Mobile],4))  When [Mobile] IS NULL Then '' End ) AS [Mobile]
FROM [TestUser]

--3.
SELECT
	[Id],
	[Name],
	(CASE WHEN [Mobile] IS NULL THEN '' ELSE (LEFT([Mobile],3)+'****'+RIGHT([Mobile],4)) END) AS [Mobile]
FROM [TestUser]

--4.多条件
SELECT
	[Id],
	[Name],
	(CASE WHEN [Mobile] IS NULL THEN '' WHEN LEN([Mobile])<11 THEN '' ELSE (LEFT([Mobile],3)+'****'+RIGHT([Mobile],4)) END) AS [Mobile]
FROM [TestUser]

--5.可以判断手机号是否为11位
SELECT
	[Id],
	[Name],
	(CASE WHEN LEN([Mobile])<11 THEN '' ELSE (LEFT([Mobile],3)+'****'+RIGHT([Mobile],4)) END) AS [Mobile]
FROM [TestUser]

C# 社区推荐