Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Date Range coding

Author  Topic 

LOLCatLady
Starting Member

24 Posts

Posted - 2011-03-15 : 17:36:57
I inherited the query this section of code is in. It works fine, however, rather than declaring @today smalldatetime set @today = getdate() I want to set that date for 03/01/2011. Changing getdate() to 2011-03-01 returns the error: Msg 298, Level 16, State 1, Line 13
The conversion from datetime data type to smalldatetime data time resulted in a smalldate time overflow error.

What do I need to change to get this to work? I'm want to pull data based on 3/1/2011, not the current date.

declare @today smalldatetime set @today = getdate()

declare @startFY smalldatetime
set @startFY = case when month(@today)>='3' then '3/1/'+ cast(YEAR (@today) AS varchar(4))
else DATEADD(yy,-1, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @startFYM1 smalldatetime
set @startFYM1 = case when month(@today)>='3' then DATEADD(yy,-1, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-2, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @endMinus1 smalldatetime
set @endMinus1 = cast(cast(month(@today) as varchar(2)) +'/1/' + CAST(YEAR(@today) AS varchar(4)) as datetime)
declare @startFYM2 smalldatetime
set @startFYM2 = case when month(@today)>='3' then DATEADD(yy,-2, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-3, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @startFYM3 smalldatetime
set @startFYM3 = case when month(@today)>='3' then DATEADD(yy,-3, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-4, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @startFYM4 smalldatetime
set @startFYM4 = case when month(@today)>='3' then DATEADD(yy,-4, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-5, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @startFYM5 smalldatetime
set @startFYM5 = case when month(@today)>='3' then DATEADD(yy,-5, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-6, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @startFYM6 smalldatetime
set @startFYM6 = case when month(@today)>='3' then DATEADD(yy,-6, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-7, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @startFYM7 smalldatetime
set @startFYM7 = case when month(@today)>='3' then DATEADD(yy,-7, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-8, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @startFYM8 smalldatetime
set @startFYM8 = case when month(@today)>='3' then DATEADD(yy,-8, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-9, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @startFYM9 smalldatetime
set @startFYM9 = case when month(@today)>='3' then DATEADD(yy,-9, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-10, '3/1/'+ cast(YEAR(@today) as varchar(4))) end
declare @startFYM10 smalldatetime
set @startFYM10 = case when month(@today)>='3' then DATEADD(yy,-10, '3/1/'+ cast(YEAR(@today) as varchar(4)))
else DATEADD(yy,-11, '3/1/'+ cast(YEAR(@today) as varchar(4))) end

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-15 : 17:48:15
so where is your SET code?

Should be an easy test

If SET @today = '3/1/2011' doesn't work, then you have a diff problem



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-03-15 : 17:55:16
I'm not sure I'm following you either, but...

1. You shouldn't use date strings. If you are just trying to get to the first of the current month you can use simple date math. For example:
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0)


2. If you insist on using date strings you should use an ISO date format (or possibly ANSI).
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-03-15 : 22:38:14
>> I inherited the query this section of code is in.<<

The fifth labor of Hercules was to clean the stables of King Augeas in a single day. The Augean stables held thousands of animals and were over a mile long. This story has a happy ending for three reasons: (1) Hercules solved the problem in a clever way (2) Hercules got one tenth of the cattle for his work (3) At the end of the story of the Labors of Hercules, he got to kill the bastard that gave him this job.

>> It works fine, however, rather than declaring @today ; SET @today = getdate().I want to set that date for 03/01/2011. <<

Yup, you are in the Stables. Why have a local variable for the current date? Why use the proprietary SMALLDATETIME; when we have the ANSI/ISO DATE data type? And please start using ONLY ISO-8601 dates.

You can can now write CAST(CURRENT_TIMESTAMP AS DATE)


Changing getdate() to 2011-03-01 returns the error: Msg 298, Level 16, State 1, Line 13
The conversion from DATETIME data type to DATE; data time resulted in a smalldate time overflow error.

What do I need to change to get this to work? I'm want to pull data based on 2011-03-01 not the current date.

DECLARE @today DATE;
SET @today = CAST(CURRENT_TIMESTAMP AS DATE);

The est of this code is, well, what you find in stables. SQL is a data language and this mess is procedural code for COBOL style strings. We would have a Calendar and mot mock-COBOL.


CREATE TABLE Fiscal_Calendar
(fiscal_period_name CHAR(12) NOT NULL PRIMARY KEY
CHECK (fiscal_period_name LIKE 'FY[12][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
cal_start_date DATE NOT NULL,
cal_end_date DATE NOT NULL,
CHECK(cal_start_date < cal_end_date);

Load it with the name of each fiscal month and the calendar date range of that fiscal month. A full century is easy to do and small

INSERT INTO Fiscal_Calendar
VALUES ('FY2011-01-00', '2011-03-01', '2012-02-28'),
etc

The query skeleton is now:

SELECT FC.fiscal_period_name, ..
FROM Fiscal_Calendar AD FC, etc
WHERE @my_date BETWEEN FC.cal_start_date AND FC.cal_end_date
AND ..;

This is 1-2 orders of magnitude faster than your old code and MUCH easier to maintain.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-03-16 : 02:30:01
Something like this?
SELECT	DATEADD(MONTH, 12 * DATEPART(YEAR, GETDATE()) + m.num -22800, 0)
FROM (
SELECT 2 UNION ALL
SELECT 1 UNION ALL
SELECT 0 UNION ALL
SELECT -1 UNION ALL
SELECT -2 UNION ALL
SELECT -3 UNION ALL
SELECT -4 UNION ALL
SELECT -5 UNION ALL
SELECT -6 UNION ALL
SELECT -7 UNION ALL
SELECT -8 UNION ALL
SELECT -9
) AS m(num)



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

LOLCatLady
Starting Member

24 Posts

Posted - 2011-03-16 : 09:38:39
quote:
Originally posted by jcelko

>> I inherited the query this section of code is in.<<

The fifth labor of Hercules was to clean the stables of King Augeas in a single day. The Augean stables held thousands of animals and were over a mile long. This story has a happy ending for three reasons: (1) Hercules solved the problem in a clever way (2) Hercules got one tenth of the cattle for his work (3) At the end of the story of the Labors of Hercules, he got to kill the bastard that gave him this job.

>> It works fine, however, rather than declaring @today ; SET @today = getdate().I want to set that date for 03/01/2011. <<

Yup, you are in the Stables. Why have a local variable for the current date? I don't know, I'm still new at this.

Why use the proprietary SMALLDATETIME; when we have the ANSI/ISO DATE data type? Again, I don't know.

And please start using ONLY ISO-8601 dates. I will learn this.

You can can now write CAST(CURRENT_TIMESTAMP AS DATE) maroon]This worked, thanks!


Changing getdate() to 2011-03-01 returns the error: Msg 298, Level 16, State 1, Line 13
The conversion from DATETIME data type to DATE; data time resulted in a smalldate time overflow error.

What do I need to change to get this to work? I'm want to pull data based on 2011-03-01 not the current date.

DECLARE @today DATE;
SET @today = CAST(CURRENT_TIMESTAMP AS DATE);

The est of this code is, well, what you find in stables. SQL is a data language and this mess is procedural code for COBOL style strings. We would have a Calendar and mot mock-COBOL. [size=2] When I get more proficient at SQL (and I have some time) I plan to revise the inherited queries. I appreciate your help![/maroon]

CREATE TABLE Fiscal_Calendar

(fiscal_period_name CHAR(12) NOT NULL PRIMARY KEY
CHECK (fiscal_period_name LIKE 'FY[12][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
cal_start_date DATE NOT NULL,
cal_end_date DATE NOT NULL,
CHECK(cal_start_date < cal_end_date);

Load it with the name of each fiscal month and the calendar date range of that fiscal month. A full century is easy to do and small

INSERT INTO Fiscal_Calendar
VALUES ('FY2011-01-00', '2011-03-01', '2012-02-28'),
etc

The query skeleton is now:

SELECT FC.fiscal_period_name, ..
FROM Fiscal_Calendar AD FC, etc
WHERE @my_date BETWEEN FC.cal_start_date AND FC.cal_end_date
AND ..;

This is 1-2 orders of magnitude faster than your old code and MUCH easier to maintain.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL


Go to Top of Page
   

- Advertisement -