SQL Server 中 CASE WHEN 使用

SQL Server 中 CASE WHEN 使用
IF EXISTS(SELECT * FROM sysobjects WHERE [name]='Users')
    DROP TABLE [Users]
GO
CREATE TABLE [Users](
        [UserName] [nvarchar](50) PRIMARY KEY([UserName]),
        [Sex] [smallint]
);
GO

INSERT INTO Users([UserName],[Sex])VALUES('Tom',1);
INSERT INTO Users([UserName],[Sex])VALUES('Alice',2);

SELECT
	[UserName],
	(CASE [Sex] WHEN '1' THEN '男' WHEN '2' THEN '女'  ELSE '其他' END ) AS '性别'
FROM
	[Users];

热门推荐