如何根据月份名称查找记录?

时间:2014-03-26 05:09:15

标签: sql sql-server

我有这个问题:

select dbo.CLOI_ClientOrderItems.cl_id,sum(dbo.IN_Invoices.in_total) as Total
from CLOI_ClientOrderItems
inner join IN_Invoices on CLOI_ClientOrderItems.MasterOrderId=IN_Invoices.MasterOrderId

where dbo.IN_Invoices.in_date_issued='2014-01-21 12:39:20.593'
group by cl_id

我希望能够使用以下内容选择3月份发出的发票,而不是指定完整日期:

select dbo.CLOI_ClientOrderItems.cl_id,sum(dbo.IN_Invoices.in_total) as Total
from CLOI_ClientOrderItems
inner join IN_Invoices on CLOI_ClientOrderItems.MasterOrderId=IN_Invoices.MasterOrderId

where dbo.IN_Invoices.in_date_issued='march'
group by cl_id

我该怎么做?

3 个答案:

答案 0 :(得分:1)

使用DatePart选择仅限月份

select dbo.CLOI_ClientOrderItems.cl_id,sum(dbo.IN_Invoices.in_total) as Total
from CLOI_ClientOrderItems
inner join IN_Invoices on CLOI_ClientOrderItems.MasterOrderId=IN_Invoices.MasterOrderId

where DatePart(mm,dbo.IN_Invoices.in_date_issued)=3
group by dbo.CLOI_ClientOrderItems.cl_id

答案 1 :(得分:1)

使用DATENAME()

select dbo.CLOI_ClientOrderItems.cl_id,sum(dbo.IN_Invoices.in_total) as Total
from CLOI_ClientOrderItems
inner join IN_Invoices on CLOI_ClientOrderItems.MasterOrderId=IN_Invoices.MasterOrderId

where DATENAME(month,dbo.IN_Invoices.in_date_issued)='March'
group by cl_id

答案 2 :(得分:0)

为以下

创建实例
public static final SimpleDateFormat FULL_DATE_TIME_FORMAT = new SimpleDateFormat(DD_MM_YYYY_HH_MM_SS_SSS_A);

对象(FULL_DATE_TIME_FORMAT).parse(" 2014-01-21 12:39:20.593 Am");