case when SUBSTRING(CONVERT(varchar(10), business_date_key, 112),0,7)=
 SUBSTRING(convert(varchar(10),getdate(),112),0,7) then '本月' else '' end AS month1,
 case when SUBSTRING(CONVERT(varchar(10), business_date_key, 112),0,7)=
 SUBSTRING(convert(varchar(10),getdate(),112),0,7)-1 then '上月' else '' end as month2根据business_date_key 来算上季度
格式和本月上月一样。。求sql高人。

解决方案 »

  1.   

    case when datepart(quarter,business_date_key) - 4*datediff(year,business_date_key,getdate()) = datepart(quarter,getdate())-1 then '上季度' else '' endcase when datepart(quarter,business_date_key)=datepart(quarter,getdate()) and year(business_date_key)=year(getdate()) then '本季度' else '' end
      

  2.   


    case datediff(quarter, business_date_ke, getdate()) when 1 then '上季度' when 0 then '本季度' else '' end
      

  3.   

    上面的 不行呀。。我那business_date_key 类型是interger
      

  4.   


    case datediff(quarter, ltrim(business_date_ke), getdate()) when 1 then '上季度' when 0 then '本季度' else '' end
      

  5.   

    int 应该是 yyyymmdd 的格式吧,ltrim(int)将int隐式转换为char,char形式的yyyymmdd可以隐式转换为datetime
      

  6.   

    case datediff(quarter, cast(str(business_date_ke) as datetime), getdate()) when 1 then '上季度' when 0 then '本季度' else '' endstr(int)
    DATEDIFF
    返回跨两个指定日期的日期和时间边界数。 语法
    DATEDIFF ( datepart , startdate , enddate ) 参数
    datepart是规定了应在日期的哪一部分计算差额的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。日期部分 缩写 
    year yy, yyyy 
    quarter qq, q 
    Month mm, m 
    dayofyear dy, y 
    Day dd, d 
    Week wk, ww 
    Hour hh 
    minute mi, n 
    second ss, s 
    millisecond ms