I’m doing my best to set a date variable so I can compare it later. I would like something that basically says:
If the current day-of-month is less than 11, then date is 10th of LAST month
If the current day-of-month is greater-than-or-equal-to 11, then date is 10th of THIS month
Date is 11/6/2012 expected output:
@PODate = 10/10/2012
Date is 11/16/2012 expected output:
@PODate = 11/10/2012
Currently all I have is this:
DECLARE @PODate as DATETIME
Set @PODate = Convert(varchar(8),GetDate(),1)
Any tips or help would be greatly appreciated. Thank you!!
Trying to keep it as straightforward as possible:
The surrounding
DATEADD/DATEDIFFpair are being used to normalize the date to the 10th of the current month. We then use a smallCASEexpression to subtract a month if the day is less than or equal to the 10th.Whatever solution you pick, please try to avoid ones that do it as string manipulation. The usual cause of
datetimerelated bugs in SQL is when people treat dates as strings. Keeping the data types appropriately is usually the best way to prevent these issues.There are, admittedly, 2 strings in my solution, but these are fixed constant strings (all that matters is that they’re both for the same year and month, and the second one is for the 10th of the month) and are in an unambiguous format.