Fetching records from SQL based on Month name - sql

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.

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'

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

Related

Extracting date in SQL

I have a column due date in the format 20210701 (YYYYMMDD), using SQL I want to extract all the dates apart from 5th of particular month ( As highlighted in the pic below )
I used the below code:
SELECT Due_Date_Key
FROM Table
WHERE Due_Date_Key <>20210705
However the error in the above code is it will exclude only the month of jul but not for other months.
How can extract the dates apart from 5th from the entire column.
Help would be much appreciated.
Note that column DUE_DATE_KEY is numeric.
A more SQLish way would be to convert string to date and then check if day is not 5
SELECT * FROM Table
WHERE DATE_PART('day', to_date(cast(DUE_DATE_KEY as varchar), 'YYYYMMDD')) != 5
Using modulo operator to determine whether the last two digits of DUE_DATE_KEY are 05.
select * from T where DUE_DATE_KEY % 100 <> 5
Using your sample data, the above query returns the following:
due_date_key
20210701
20210708
20210903
Refer to this db fiddle

Date - Data type mismatch in criteria expression in Access

I have a table which stores birthdate in the field DateValueOf. I am creating a query where user can input any date and the query will return customers which have birthday on that day.
For this, I am trying to convert all the year in DateValueOf to current year.
eg > #13-10-1996# to #13-10-2016#
eg > #13-10-2001# to #13-10-2016#
So that I will then ask user to select date from date picker and run the below query.
SELECT CustomerID
WHERE FormatDateTime(DateValue(DateSerial(Year(Date()),Month([DateValueOf]),Day([DateValueOf]))),2) >= #13-10-2016#
But this SQL statement produces an error data type mismatch in criteria expression
Please highlight my mistake. Thanks in advance. I am also open to any other way to get who's birthday it is today.
Assuming you receive the date from a date picker Control (call it: myDatePicker), today's date, returned by function Date() is not relevant.
And unless you want the age, Year() is not relevant either.
SELECT CustomerID
FROM MyTableName
WHERE Month(myDatePicker) = Month([DateValueOf]) AND Day([DateValueOf]) = Day([myDatePicker])
If you want the customer's age at this birthday, you can add the calculation to the SELECT clause:
SELECT CustomerID, Year(myDatePicker) - Year(DateValueOf) As CustomerAge
FROM MyTableName
WHERE Month(myDatePicker) = Month([DateValueOf]) AND Day([DateValueOf]) Day([myDatePicker])
Don't worry about formatting the date, just use a date comparison:
SELECT CustomerID
WHERE DateSerial(Year(Date()),Month([DateValueOf]),Day([DateValueOf]))
>= #13-10-2016#

SQL How to convert date DD-MM-YYYY to MM-YYYY and retrieve most recent records?

Hi I have to retrieve from my table most recent records that match a given MM-YYYY, but the date field in the table is DD-MM-YYYY, how do I get this done?
Thank you very much
Best Regards
Ignacio.
How many of the most recent records do you need?
Could you not query for all the dates in the relevant month, order by the dates, and then only select the results you need from the top of the list.
For example:
SELECT TOP(1) *
FROM your_table
WHERE date_time_field >= '20120101'
AND date_time_field < '20120201'
ORDER BY date_time_field DESC
This would select the most recent record from January of this year.
Change the number inside the TOP() statement to change the number of results returned, or leave it off altogether and take comfort in the fact that your results are ordered.
SELECT * FROM `your`.`schemaTableNames` WHERE your_coulumn_name LIKE "%-MM-YYYY";
should give you the matching records, as you wrote that you want to retrieve.
Have you tried using DateDiff(month, date1, date2) == 0 in your where clause?
This assumes that you can convert both values to DATETIME. If you only have the year and the month, every month has day 01.
to convert a date to desire format you can use style to retrive the records date formats
The following works:
right(select convert(varchar, <date field>, 110), 7)
The full query would look someting like this:
select *
from table
where right(select convert(varchar, <date field>, 110), 7) = 'MM-YYYY'

Get data between record in table

I have data like this:
For example, today is on April 2012. Referring to data above, I want to get the data with M_PER = 03-2012 because this month is in the range 03-2012 TO 06-2012.
--EditedIn this case, I wanna get a rate for used currency code. Because today is still in April, and I want to know rate US Dollar (USD) to Indonesia Rupiah (IDR) I must get the data with M_PER = 03-2012 and CRR_CURRENCY_CODE = USD.
The question is what query can retrieve data like that?
Since you seem to be using quarterly values, I would use the TRUNC function with the 'Q' format model. This truncates a date to 1/1/YYYY, 1/4/YYYY, 1/7/YYYY and 1/10/YYYY, i.e. the first day of the quarter.
To fit your model which is the month at the end of the quarter, you would then have to add two months. This assumes that the MONTH_PERIOD column is a SQL date and not some other data type.
Included below is an example, using SYSDATE as the input date.
select *
from your_table
where add_months(trunc(sysdate, 'Q'),2) = month_period;
I use the rownum and order by to get the value.
SELECT * FROM tables WHERE m_per > '04-2012' AND ROWNUM = 1 ORDER BY month_period ASC

mysql return rows matching year month

How would I go about doing a query that returns results of all rows that contain dates for current year and month at the time of query.
Timestamps for each row are formated as such: yyyy-mm-dd
I know it probably has something to do with the date function and that I must somehow set a special parameter to make it spit out like such: yyyy-mm-%%.
Setting days to be wild card character would do the trick but I can't seem to figure it out how to do it.
Here is a link to for quick reference to date-time functions in mysql:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Thanks
I think EXTRACT is the function you are looking for:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM NOW())
you could extract the year and month using a function, but that will not be able to use an index.
if you want scalable performance, you need to do this:
SELECT *
FROM myTable
WHERE some_date_column BETWEEN '2009-01-01' AND '2009-01-31'
select * from someTable where year(myDt) = 2009 and month(myDt) = 9 and day(myDt) = 12