I have a table in which I get the coupons of a specific company. My coupon table is like:
Company_Coupon
- ID
- CompanyID
- Month
- Total_Coupons
- Year
I keep my months in the format of their number (1-12).
When I update the total coupons for the invoicing, I want to update the ones from the previous month.
I do this by the following query:
UPDATE Company_Coupon
SET Total_Coupons = @count
WHERE CompanyID = 1205
AND Month = MONTH(GETDATE())-1 AND Year = YEAR (GETDATE())
My query works but I noticed that this won't work in January 2019.
How can I update this query so that it will work in January 2019?
YearMonthcolumn, typed asdatewith a constraint enforcing that it's always the first day of the month. That way you can use straightforward date math functions to compute this. E.g.DATEADD(month,DATEDIFF(month,'20010201',GETDATE()),'20010101')always gives you the first of last month. - Damien_The_Unbeliever