I have data I am trying to pull for a report and I am working on a Year to Date report. My columns in the table are formatted as datetime. I am trying to run a select statement to get all the results where date = this year.
For example:
SELECT A.[id], A.[classXML] FROM tuitionSubmissions as A WHERE A.[status] = 'Approved' AND A.[reimbursementDate] = THISYEAR FOR XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');
Is there an eay way to acomplish this?
The YEAR() function returns the year part for a given date (a number from 1000 to 9999).
Just run these SQL queries one by one to get the specific element of your current date/time: Current year: SELECT date_part('year', (SELECT current_timestamp)); Current month: SELECT date_part('month', (SELECT current_timestamp)); Current day: SELECT date_part('day', (SELECT current_timestamp));
SQL Server YEAR() Function The YEAR() function returns the year part for a specified date.
Yes, it's remarkably easy in fact:
WHERE YEAR(A.[reimbursementDate]) = YEAR(GETDATE())
This should be a better way if performance is an issue (although it isn't as readable)
where A.[reimbursementDate] between DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) and DATEADD(MILLISECOND, -3, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
Those funky DATEADD statements return the first and last days of the current year (through December 31 23:59:59.997). Since reimbursementDate
isn't contained in a function, the query will be able to take advantage of any applicable indexes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With