SQL-Case When用法
/* 人员:LDH 功能:SQL-Case When用法 日期:2021-5-14 */ USE TestDB; GO CREATE TABLE Employee ( EmpId INT IDENTITY(20210001, 1) PRIMARY KEY , EmpName NVARCHAR(20) NOT NULL , EmpAge INT CHECK ( EmpAge BETWEEN 1 AND 150 ) DEFAULT 1 , EmpStatus BIT NOT NULL DEFAULT 0 --0:在职 1-离职 ); INSERT INTO dbo.Employee ( EmpName, EmpAge, EmpStatus ) VALUES ( N'Jason', 31, 0 ), ( N'Tom', 18, 1 ), ( N'Lucy', 21, 1 ), ( N'Jack', 31, 0 ), ( N'Lily', 18, 1 ), ( N'Frank', 21, 0 ), ( N'Bush', 51, 0 ), ( N'Apple', 18, 1 ), ( N'Mack', 21, 0 ), ( N'Black', 28, 1 ), ( N'Pear', 18, 0 ), ( N'Banana', 21, 0 ); SELECT EmpId , EmpName , EmpAge , EmpStatus FROM dbo.Employee; -- ============================= -- 方法1 -- ============================= SELECT EmpId , EmpName , EmpAge , 'EmpStatus' = CASE WHEN EmpStatus = 0 THEN '在职' WHEN EmpStatus = 1 THEN '离职' END FROM dbo.Employee; -- ============================= -- 方法2 -- ============================= SELECT EmpId , EmpName , EmpAge , 'EmpStatus' = CASE EmpStatus WHEN 0 THEN '在职' WHEN 1 THEN '离职' END FROM dbo.Employee;
本文作者:Love In Winter
本文链接:https://www.cnblogs.com/LifeDecidesHappiness/p/14770462.html
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以扫一扫,任意打赏,您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/LifeDecidesHappiness/p/14770462.html
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以扫一扫,任意打赏,您的鼓励是博主的最大动力!
![扫一扫,支付宝打赏](https://files.cnblogs.com/files/LifeDecidesHappiness/%E6%94%AF%E4%BB%98%E5%AE%9D.bmp)
![扫一扫,微信打赏](https://files.cnblogs.com/files/LifeDecidesHappiness/%E5%BE%AE%E4%BF%A1%E6%94%B6%E6%AC%BE%E7%A0%81.bmp)