通过以下查询,我希望能够从提交的表单中获取特定月份/年份的数据。在表单中,他们可以选择两个月/年,然后用户提交表单。
查询正在工作,但是出现问题,因为我没有工作日,有些月份有28/30/31天。
例如,假设说date_one = 2016/01和date_two = 2016/10我会得到错误
Conversion failed when converting date and/or time from character string.
因为我没有变量的日子。我可能以错误的方式解决了这个问题。
我只有一个月/一年的期权才能解决这个问题?
SELECT .......
from test_table
WHERE paid_dt between <cfqueryparam cfsqltype="cf_sql_integer" value="#date_one#"> and
<cfqueryparam cfsqltype="cf_sql_integer" value="#date_two#">
GROUP BY
DATEPART(year, paid_dt),datename(month,paid_dt),
datepart(month, paid_dt)
ORDER BY
DATEPART(year, paid_dt) ASC,
datepart(month, paid_dt) ASC
我有sql server 2012。
您可以使用try_convert()。例如
Select Try_Convert(Date,'2016/10'+'/01') -- Notice the addition of /01
退货
2016-10-01
无效的转换将返回null。
获得月末
Select EOMonth(Try_Convert(Date,'2016/2'+'/01'))
退货
2016-02-29
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句