我想做一个查询,以文本形式显示当前月份,然后在后面的行中打印今年剩余的月份,直到打印出一年中的所有月份。
这有点难以解释,所以我做了下面的例子。我知道下面的代码很荒谬,但这是我知道的以我目前的技能水平做到这一点的唯一方法。理想情况下,我还希望将月份从打印整数转换为打印月份的字符值(因此 1 将是一月)。我知道我可以用 case/when 来做到这一点,但我确信有一种我还没有接触过的更好的方法。
declare @currentmonth as int = datepart(month, getdate())
select
@currentmonth
union
select
@currentmonth +1
union
select
@currentmonth +2
union
select
@currentmonth +3
union
select
@currentmonth +4
union
select
@currentmonth +5
union
select
@currentmonth +6
union
select
@currentmonth +7
union
select
@currentmonth +8
union
select
@currentmonth +9
union
select
@currentmonth +10
union
select
@currentmonth +11
最佳答案
在 SQL Server 2008+ 中你可以使用这个
SELECT v.i
FROM (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) v(i)
WHERE v.i>=MONTH(GETDATE())
关于sql - 打印当前月份的行,然后是一年中剩余的月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8831732/