查询获取当前和前2个月的记录 [英] Query for getting Current and previous 2 months records
本文介绍了查询获取当前和前2个月的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我的桌子名为'Open_Months',下面有列,每个客户只有记录
Hi All,
I have table called 'Open_Months' with below columns, there is only record for every client
Emp_Id Month Year Name Created Date Type
00FBG 2 2014 prakasan 2014-01-12 10:21:54.730 R
如何获得当前月份的前2个月记录
问候,
Abhishek Patil
PUTTU PATIL
how to get the current months previous 2 months records
Regards,
Abhishek Patil
PUTTU PATIL
推荐答案
你可以尝试这个
You can try this
select *
from YourTable
where Time_Open > dateadd(month, datediff(month, 0, GETDATE()) - 4, 0)
and Time_Open < dateadd(month, datediff(month, 0, GETDATE()) - 1, 0)
我的表'Open_Months'如下所示
Emp_Id月份年度
00FBG 4 2013
我需要当前月份和前2个月,为此我将月份和年份列连接到日期格式化,查询为此如下所示
SELECT cast(CONVERT(datetime,CONVERT(varchar(4),OM_Year)+ RIGHT('0'+ CONVERT(varchar(2) ,OM_Month),2)+'01',112)AS日期时间)来自Open_Months,其中OM_Emp_ID ='00FBG'
日期
2013 -04-01
从上述日期开始我需要找到当月和之前的2个月。
问候,
AbhiShek Patil
I have table 'Open_Months' as shown below
Emp_Id Month Year
00FBG 4 2013
I need current month and previous 2 months, for this i have concatinated month and year column into date formate, the query for this as shown below
SELECT cast(CONVERT(datetime, CONVERT(varchar(4), OM_Year) + RIGHT('0' + CONVERT(varchar(2), OM_Month), 2) + '01', 112) AS Datetime) from Open_Months where OM_Emp_ID = '00FBG'
Date
2013-04-01
from the above date i need to find current month and its previous 2 months.
Regards,
AbhiShek Patil
这篇关于查询获取当前和前2个月的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文