I would like this query to be able to automagically know today's date & time as well as the first of the year (or month) (or week)...
SELECT TicketID
FROM Ticket
WHERE (Ticket.DtCheckOut > '1/1/2011 12:00:00 AM')
AND (Ticket.DtCheckOut < '8/27/2011 12:00:00 AM')
I know it will use GETDATE()
in some form, but you don't want to see what I've come up with, I promise!
Here is what I was reading on GETDATE()
MDSN: GETDATE(Transact-SQL)
I looked around here and Google - and didn't find anything 'clean' - so any input would be awesome!
DECLARE @now DATETIME
SET @now = GETDATE()
SELECT
DATEADD(yy, DATEDIFF(yy, 0, @now), 0) AS FirstDayOfYear,
DATEADD(mm, DATEDIFF(mm, 0, @now), 0) AS FirstDayOfMonth,
DATEADD(DAY, -DATEDIFF(dd, @@DATEFIRST - 1, @now) % 7, @now) AS FirstDayOfWeek
@@DATEFIRST
is SQL Server's first day of the week, which defaults to Sunday if you are using U.S. English.
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