Arpit
Arpit

Reputation: 65

Fetching records from SQL based on Month name

enter image description here So this my table structure and data.

Now I want to filter data based on Month by ExpenseDate column. So how can I achieve that? I was trying

select * from tblExpenses where (ExpenseDate = MONTH('April'))

But it throws an error: "Conversion failed when converting date and/or time from character string." Please help. Thank you.

Upvotes: 0

Views: 183

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269513

You are putting month() on the wrong column. It can be applied to ExpensesDate:

select *
from tblExpenses
where month(ExpenseDate) = 4;

Note that month() returns a number, not the name of the month.

I think it is more likely that you want records from a particular April, not every April. This would be expressed as:

where ExpenseDate >= '2018-04-01' and ExpenseDate < '2018-05-01'

Upvotes: 2

Brad
Brad

Reputation: 3591

I think your where clause is just reversed I think you want this (and change the word to a number)

select * from tblExpenses where Month(ExpenseDate) = 4

Upvotes: 0

Related Questions