2017-07-26 58 views
-4

我有两个表格A1和A2。 表A1有列转移索引,日期和金额,A2表有列转移索引,名称和年龄。SQL查询找到数据

1.如何找到当月的shift-index数据。如果我选​​择shift-index 6,则应该填充6之前的数据。 移位指数表示日期

data

+3

似乎很大。什么东西阻止你? –

+0

寻找解决方案..如何做到这一点 –

回答

0

我想这你想要做什么:

where datecol >= dateadd(day, 1 - day(getdate()), cast(getdate() as date) and 
     datecol < cast(getdate() as date) 

这将返回所有行日期之间的第一当月和昨天的。在本月的第一天,它不返回任何行。

0

你也可以做这样的事情

select * from YourTable 
where YourDateColumn between 
        DATEADD(month, DATEDIFF(month, 0, getDate()), 0) and getDate() - 1 

只需用日期值替换getDate(),并给它一个去