Sql server SQL Server-if-else/case-when语句

Sql server SQL Server-if-else/case-when语句,sql-server,sql-server-2008,if-statement,sql-server-2012,case-when,Sql Server,Sql Server 2008,If Statement,Sql Server 2012,Case When,我在select查询中编写了一个case语句,如下所示。我不确定SQL Server中If-else的语法,而且我想使用'as'命名列,但在'as'附近使用错误语法会引发错误。请帮忙。T select a.account_id as [Account Id], substring(a.account_id,1,3) as [Company ID], substring(a.account_id,4,6) as [Account No], substring(a.

我在select查询中编写了一个case语句,如下所示。我不确定SQL Server中If-else的语法,而且我想使用'as'命名列,但在'as'附近使用错误语法会引发错误。请帮忙。T

select 
    a.account_id as [Account Id],
    substring(a.account_id,1,3) as [Company ID],
    substring(a.account_id,4,6) as [Account No],
    substring(a.account_id,10,5) as [Cost Center],
    b.amount as Amount,
    b.period as Period,
    getdate() as SysDate,
    format(getdate(), 'mm/dd/yyyy') as SYSDtText,
    substring('SYSDtText',1,2) as [Current Period],
    b.year_budget as [Year Budget],
    substring('SYSDtText', 7, 4 ) as SysDtYearText,
    case 
       when Period <= 'Current Period' 
          then 'include' 
          else '' 
    as [Period Include Flag], 
    a.description as [Description]
from 
    period_summary b
inner join 
    account a on b.account_id = a.account_id;
选择
a、 帐户id为[帐户id],
子字符串(a.account_id,1,3)作为[公司id],
子字符串(a.account_id,4,6)作为[账号],
子字符串(a.account_id,10,5)作为[成本中心],
b、 金额作为金额,
b、 期间作为期间,
getdate()作为SysDate,
将(getdate(),'mm/dd/yyyy')格式设置为SYSDtText,
子字符串('SYSDtText',1,2)作为[当前期间],
b、 年度预算作为[年度预算],
子字符串('SYSDtText',7,4)作为SysDtYearText,
案例
当周期时,您缺少一个“结束”

正确:

CASE WHEN Period<='Current Period' then 'include' else '' end as [Period Include Flag]

CASE WHEN Period我认为您需要更改此行:

CASE WHEN Period<='Current Period' then 'include' else '' as [Period Include Flag], 

周期工作时的情况。谢谢!您错过了
else'
as
之间的
end
CASE WHEN Period <= substring('SYSDtText',1,2) then 'include' else '' end as [Period Include Flag],