dbTalk Databases Forums  

First Day Of Year AND Last Day Of Year

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss First Day Of Year AND Last Day Of Year in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Derek Hart
 
Posts: n/a

Default First Day Of Year AND Last Day Of Year - 08-10-2009 , 05:31 PM






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),''))

Reply With Quote
  #2  
Old   
Aaron Bertrand [SQL Server MVP]
 
Posts: n/a

Default Re: First Day Of Year AND Last Day Of Year - 08-10-2009 , 05:48 PM






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:

Quote:
= @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:

Quote:
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),''))


Reply With Quote
  #3  
Old   
Jeremy A. Holovacs
 
Posts: n/a

Default Re: First Day Of Year AND Last Day Of Year - 08-10-2009 , 07:09 PM



I agree except the @LastDayOfRange I would calculate as follows:

SET @LastDayOfRange = DATEADD(YEAR, 1, @FirstDayOfRange) -1;

not a huge deal, but simpler in my mind.

--
-------------------------------
Jeremy Holovacs
MCITP, MCDBA, MCSE
"Aaron Bertrand [SQL Server MVP]" <ten.xoc (AT) dnartreb (DOT) noraa> wrote

Quote:
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),''))



Reply With Quote
  #4  
Old   
Aaron Bertrand [SQL Server MVP]
 
Posts: n/a

Default Re: First Day Of Year AND Last Day Of Year - 08-10-2009 , 07:46 PM



Quote:
SET @LastDayOfRange = DATEADD(YEAR, 1, @FirstDayOfRange) -1;

not a huge deal, but simpler in my mind.
I used to use this "shortcut" and it is certainly quicker to write, but do
those benefits really pay off? For example, have you tried it with the new
DATE data types in SQL Server 2008?


DECLARE @d DATE;
SELECT @d = GETUTCDATE();
SELECT @d = @d + 1;

Msg 206, Level 16, State 2, Line 3
Operand type clash: date is incompatible with int


As a result, I've been very careful to avoid shortcuts that might compromise
the future-proofness of my code.

A

Reply With Quote
  #5  
Old   
Derek Hart
 
Posts: n/a

Default Re: First Day Of Year AND Last Day Of Year - 08-11-2009 , 09:33 AM



So what is the best way to get @FirstDayOfNextYear ?

"Aaron Bertrand [SQL Server MVP]" <ten.xoc (AT) dnartreb (DOT) noraa> wrote

Quote:
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),''))



Reply With Quote
  #6  
Old   
Aaron Bertrand [SQL Server MVP]
 
Posts: n/a

Default Re: First Day Of Year AND Last Day Of Year - 08-11-2009 , 09:40 AM



Quote:
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;

Reply With Quote
  #7  
Old   
Derek Hart
 
Posts: n/a

Default Re: First Day Of Year AND Last Day Of Year - 08-11-2009 , 12:59 PM



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

Quote:
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;


Reply With Quote
  #8  
Old   
Aaron Bertrand [SQL Server MVP]
 
Posts: n/a

Default Re: First Day Of Year AND Last Day Of Year - 08-11-2009 , 01:07 PM



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:

Quote:
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;




Reply With Quote
  #9  
Old   
Derek Hart
 
Posts: n/a

Default Re: First Day Of Year AND Last Day Of Year - 08-11-2009 , 11:00 PM



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:
Quote:
= @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

Quote:
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;





Reply With Quote
  #10  
Old   
Derek Hart
 
Posts: n/a

Default Re: First Day Of Year AND Last Day Of Year - 08-12-2009 , 11:57 AM



May have answered my question. Would you tell me if you see any
inefficiencies in writing the procedure this way?
Set @DateValue = '20090512'
Set @LastMonth = 7
Set @Year = Year(@DateValue)
Set @dt = RTRIM(@Year) + '0101'
Set @FirstDayOfRange = dateadd(day, -1, dateadd(month, @LastMonth, @dt))
Set @LastDayOfRange = dateadd(day, 0, dateadd(month, @LastMonth-12, @dt))

So the dates I get are 8/1/2008 - 7/31/2009, which is the fiscal year.
Correct data, but any flaws in this process?

"Derek Hart" <derekmhart (AT) yahoo (DOT) com> wrote

Quote:
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;







Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.