1. 程式人生 > 其它 >SQL--Case When.. Then.. end的使用

SQL--Case When.. Then.. end的使用

Case  When.. Then.. end的使用場景

當欄位有不同的值,根據不同的值表示不同的內容

use [資料庫名]     
go
if exists( select * from sys.views where name='v_KAR_Schedule_Report')  --判斷檢視是否已存在
drop view v_KAR_Schedule_Report      --存在則刪除檢視
go

Create view  [dbo].[v_KAR_Schedule_Report]    --建立檢視
as
select Draft_Number as DraftNO,Schedule_Approval_Date as
ScheduleApprovalDate,Engagement_No as EngagementNO,Schedule_ID as ScheduleID,Schedule_Created as ScheduleCreated,Schedule_Currency as ScheduleCurrency,Schedule_Total_InclVAT as ScheduleTotalInclVAT,Schedule_Fee as ScheduleFee, Schedule_Expense as ScheduleExpense ,Net_Total_ExclVAT as NetTotalExclVAT,Allocated_Total_ExclVAT as
AllocatedTotalExclVAT ,Schedule_Narrative as ScheduleNarrative, -- case 欄位 when 初始值 then 要展示的值 else 剩餘情況要表示的值 end as 新欄位名 -- case Hardcopy_Preference when 'A' then 'Softcopy only' when 'B' then 'Softcopy & hardcopy (centrally dispatch to client)' when 'C' then 'Softcopy & hardcopy (EM to collect and deliver)
' else '' end as HardcopyPreference, --使用場景 isnull(Is_Cover_Required,'') as IsCoverRequired, -- 空值(null)的判斷 為null則表示為空字串 Invoice_To_EntityNo as InvoiceToEntityNO,Invoice_To_ContactNo as InvoiceToContactNO,Invoice_To_APContactNo as InvoiceToAPContactNO,Draft_Number_Create_Time as DraftNOCreateTime from EM_Schedule go