根据月份和年份获取记录 [英] Get Records based on month and year
本文介绍了根据月份和年份获取记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好吧
i有两个组合框,我添加了所有月份名称的前3个字母(例如1月,2月等)
和另一个从2005 - 2012年开始的所有年份的组合框
现在我写了下面的查询来查找一年2个月之间的结果>
但它没有给我输出。
请告诉我哪里出错了
Hi all
i have two combo box where i added all the months name's first 3 letters(e.g. Jan, Feb etc.)
and another combo box which is having all the years from 2005-2012
Now i wrote the below query to find the result between 2 months of a year
but it's not giving me the output.
Please tell me where i am going wrong
select DatePart(MM,FromDate) as Month,Sum(Amount) as Amount
from SuppReport where DatePart(MM,FromDate) between
DATEPART(MM,CAST('Jan'+ '2005' AS datetime) and DATEPART(MM,CAST('Dec'+ '2005' AS datetime)
and CONVERT(VARCHAR(4),
DateName(YEAR,FromDate))='2005' Group By DatePart(MM,FromDate)
在此查询中,我正在尝试将月份名称转换为月份号码。
但显示错误
In this query i am trying to convert month name to month number.
but showing error
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'and'.
推荐答案
使用DATEPART
i nstead,请看这里: http://www.w3schools.com/sql/func_datepart.asp [ ^ ]
,你也应该使用数字月而不是'Feb'和'Apr'(你可以使用DATEPART
函数)。
UseDATEPART
instead, see here : http://www.w3schools.com/sql/func_datepart.asp[^]
and also you should use numeric months not 'Feb' and 'Apr' (you can do it with theDATEPART
function).
这篇关于根据月份和年份获取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文