![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
= @FirstDayOfThisYear AND < @FirstDayOfNextYear |
|
Are these good ways, in SQL 2000, to find the first and last day of THIS year for a given date? Set @DateValue = '8/10/2009' Set @FirstDayOfRange = dateadd(year, datediff(year, '', @DateValue),'') Set @LastDayOfRange = dateadd(dd, 364, DATEADD(year, datediff(year, '', @DateValue),'')) |
#3
| |||
| |||
|
|
First, don't use awful formats like 8/10/1009. On some systems, that will be interpreted as August 10th, on others October 8th. I strongly suggest always using YYYYMMDD for date-only literals in SQL Server. Second, don't rely on offsets like 364 days. On leap years, your formula will suddenly break, and while you may not miss a lot of data on New Years' Eve depending on your business, it will still look funny in reports. Probably the easiest way is: DECLARE @DateValue SMALLDATETIME, @BeginningOfYear SMALLDATETIME, @EndOfYear SMALLDATETIME; SELECT @DateValue = '20090810', @FirstDayOfRange = DATEADD(YEAR, DATEDIFF(YEAR, '19000101', @DateValue), '19000101'), @LastDayOfRange = DATEADD(DAY, -1, DATEADD(YEAR, 1, @FirstDayOfRange)); SELECT @DateValue, @BeginningOfYear, @EndOfYear; However, if you are going to use these dates in a BETWEEN clause, unless you are certain that your data will never contain a time component, much safer to say: = @FirstDayOfThisYear AND < @FirstDayOfNextYear On 8/10/09 6:31 PM, in article egm6IpgGKHA.2516 (AT) TK2MSFTNGP02 (DOT) phx.gbl, "Derek Hart" <derekmhart (AT) yahoo (DOT) com> wrote: Are these good ways, in SQL 2000, to find the first and last day of THIS year for a given date? Set @DateValue = '8/10/2009' Set @FirstDayOfRange = dateadd(year, datediff(year, '', @DateValue),'') Set @LastDayOfRange = dateadd(dd, 364, DATEADD(year, datediff(year, '', @DateValue),'')) |
#4
| |||
| |||
|
|
SET @LastDayOfRange = DATEADD(YEAR, 1, @FirstDayOfRange) -1; not a huge deal, but simpler in my mind. |
#5
| |||
| |||
|
|
First, don't use awful formats like 8/10/1009. On some systems, that will be interpreted as August 10th, on others October 8th. I strongly suggest always using YYYYMMDD for date-only literals in SQL Server. Second, don't rely on offsets like 364 days. On leap years, your formula will suddenly break, and while you may not miss a lot of data on New Years' Eve depending on your business, it will still look funny in reports. Probably the easiest way is: DECLARE @DateValue SMALLDATETIME, @BeginningOfYear SMALLDATETIME, @EndOfYear SMALLDATETIME; SELECT @DateValue = '20090810', @FirstDayOfRange = DATEADD(YEAR, DATEDIFF(YEAR, '19000101', @DateValue), '19000101'), @LastDayOfRange = DATEADD(DAY, -1, DATEADD(YEAR, 1, @FirstDayOfRange)); SELECT @DateValue, @BeginningOfYear, @EndOfYear; However, if you are going to use these dates in a BETWEEN clause, unless you are certain that your data will never contain a time component, much safer to say: = @FirstDayOfThisYear AND < @FirstDayOfNextYear On 8/10/09 6:31 PM, in article egm6IpgGKHA.2516 (AT) TK2MSFTNGP02 (DOT) phx.gbl, "Derek Hart" <derekmhart (AT) yahoo (DOT) com> wrote: Are these good ways, in SQL 2000, to find the first and last day of THIS year for a given date? Set @DateValue = '8/10/2009' Set @FirstDayOfRange = dateadd(year, datediff(year, '', @DateValue),'') Set @LastDayOfRange = dateadd(dd, 364, DATEADD(year, datediff(year, '', @DateValue),'')) |
#6
| |||
| |||
|
|
So what is the best way to get @FirstDayOfNextYear ? |
#7
| |||
| |||
|
|
So what is the best way to get @FirstDayOfNextYear ? I thought I already kind of covered that (just don't subtract a day), but let's do it again. DECLARE @FirstDayOfThisYear SMALLDATETIME, @FirstDayOfNextYear SMALLDATETIME; SELECT @FirstDayOfThisYear = DATEADD(YEAR, DATEDIFF(YEAR, '19000101', CURRENT_TIMESTAMP), '19000101'), @FirstDayOfNextYear = DATEADD(YEAR, 1, @FirstDayOfThisYear); SELECT @FirstDayOfThisYear, @FirstDayOfNextYear; |
#8
| |||
| |||
|
|
Thank you. One more question, very much along the same lines. I need to figure out a fiscal date. I will have the month and year passed in. For the month, I will have a number such as 7 for July, and the year will be passed in as a four digit integer. So I have 7 and 2009, and I need to get 7/31/2009. I know I can use a month calculation and parse this all together as text, but is there a more efficient way? "Aaron Bertrand [SQL Server MVP]" <ten.xoc (AT) dnartreb (DOT) noraa> wrote in message news:C6A6FB35.22054%ten.xoc (AT) dnartreb (DOT) noraa... So what is the best way to get @FirstDayOfNextYear ? I thought I already kind of covered that (just don't subtract a day), but let's do it again. DECLARE @FirstDayOfThisYear SMALLDATETIME, @FirstDayOfNextYear SMALLDATETIME; SELECT @FirstDayOfThisYear = DATEADD(YEAR, DATEDIFF(YEAR, '19000101', CURRENT_TIMESTAMP), '19000101'), @FirstDayOfNextYear = DATEADD(YEAR, 1, @FirstDayOfThisYear); SELECT @FirstDayOfThisYear, @FirstDayOfNextYear; |
#9
| |||
| |||
|
|
= @FirstDayOfFiscal And <=@FirstDayOfFiscalOfOneYearForward. |
|
DECLARE @m INT, @y INT, @dt SMALLDATETIME; SELECT @m = 7, @y = 2009, @dt = RTRIM(@y) + '0101'; SELECT DATEADD(DAY, -1, DATEADD(MONTH, @m, @dt)); But again, why do you need the *last* day of the month? If you are doing range queries, you should be thinking in terms of "greater than the first day of this month, and less than the first day of the next month." I also recommend you stay away from ambiguous formats like m/d/y. It's obvious when the only date you see is 7/31/2009, however if you say 6/1/2009, the interpretation will depend on your audience. In SQL Server I strongly recommend always using date-only literals in the form YYYYMMDD. On 8/11/09 1:59 PM, in article uRuYI2qGKHA.1988 (AT) TK2MSFTNGP03 (DOT) phx.gbl, "Derek Hart" <derekmhart (AT) yahoo (DOT) com> wrote: Thank you. One more question, very much along the same lines. I need to figure out a fiscal date. I will have the month and year passed in. For the month, I will have a number such as 7 for July, and the year will be passed in as a four digit integer. So I have 7 and 2009, and I need to get 7/31/2009. I know I can use a month calculation and parse this all together as text, but is there a more efficient way? "Aaron Bertrand [SQL Server MVP]" <ten.xoc (AT) dnartreb (DOT) noraa> wrote in message news:C6A6FB35.22054%ten.xoc (AT) dnartreb (DOT) noraa... So what is the best way to get @FirstDayOfNextYear ? I thought I already kind of covered that (just don't subtract a day), but let's do it again. DECLARE @FirstDayOfThisYear SMALLDATETIME, @FirstDayOfNextYear SMALLDATETIME; SELECT @FirstDayOfThisYear = DATEADD(YEAR, DATEDIFF(YEAR, '19000101', CURRENT_TIMESTAMP), '19000101'), @FirstDayOfNextYear = DATEADD(YEAR, 1, @FirstDayOfThisYear); SELECT @FirstDayOfThisYear, @FirstDayOfNextYear; |
#10
| |||
| |||
|
|
Okay I will try to change to use only the yyyymmdd format. I forgot to ask one other point. If I have the 7/31/2009, I need to get the full fiscal year. So then to finally get the full range, how to I get the date 8/1/2008 from this? So the range I will check is 8/1/2008 - 7/31/2009. But as you stated I may not want the solution below to get the range. So the range statement in sql will be: = @FirstDayOfFiscal And <=@FirstDayOfFiscalOfOneYearForward. But I do also need @LastDayOfFiscal because this needs to be stored in the database. Can you do a final tweak on this to show all three? "Aaron Bertrand [SQL Server MVP]" <ten.xoc (AT) dnartreb (DOT) noraa> wrote in message news:C6A72BB3.220AC%ten.xoc (AT) dnartreb (DOT) noraa... DECLARE @m INT, @y INT, @dt SMALLDATETIME; SELECT @m = 7, @y = 2009, @dt = RTRIM(@y) + '0101'; SELECT DATEADD(DAY, -1, DATEADD(MONTH, @m, @dt)); But again, why do you need the *last* day of the month? If you are doing range queries, you should be thinking in terms of "greater than the first day of this month, and less than the first day of the next month." I also recommend you stay away from ambiguous formats like m/d/y. It's obvious when the only date you see is 7/31/2009, however if you say 6/1/2009, the interpretation will depend on your audience. In SQL Server I strongly recommend always using date-only literals in the form YYYYMMDD. On 8/11/09 1:59 PM, in article uRuYI2qGKHA.1988 (AT) TK2MSFTNGP03 (DOT) phx.gbl, "Derek Hart" <derekmhart (AT) yahoo (DOT) com> wrote: Thank you. One more question, very much along the same lines. I need to figure out a fiscal date. I will have the month and year passed in. For the month, I will have a number such as 7 for July, and the year will be passed in as a four digit integer. So I have 7 and 2009, and I need to get 7/31/2009. I know I can use a month calculation and parse this all together as text, but is there a more efficient way? "Aaron Bertrand [SQL Server MVP]" <ten.xoc (AT) dnartreb (DOT) noraa> wrote in message news:C6A6FB35.22054%ten.xoc (AT) dnartreb (DOT) noraa... So what is the best way to get @FirstDayOfNextYear ? I thought I already kind of covered that (just don't subtract a day), but let's do it again. DECLARE @FirstDayOfThisYear SMALLDATETIME, @FirstDayOfNextYear SMALLDATETIME; SELECT @FirstDayOfThisYear = DATEADD(YEAR, DATEDIFF(YEAR, '19000101', CURRENT_TIMESTAMP), '19000101'), @FirstDayOfNextYear = DATEADD(YEAR, 1, @FirstDayOfThisYear); SELECT @FirstDayOfThisYear, @FirstDayOfNextYear; |
![]() |
| Thread Tools | |
| Display Modes | |
| |