Tag calendar

PASS Summit Call For Speakers – 8 Days Left

I finally put together my abstracts and submitted them to PASS for the 2009 Summit. If you want to speak, but haven’t submitted abstracts yet, you better hop to it.

I figured that I would go ahead and list my abstracts here so you can copy them, fix the typos, and submit them as your own.

A Dynamic World Demands Dynamic SQL

Dynamic SQL is a misunderstood and much maligned part of a DBA’s tool kit – it can be used to solve difficult business problems, respond to diverse data needs, and alleviate performance problems. Many DBAs reject dynamic SQL outright as a potential source of SQL injections, being poorly performing, or just for being a hacky solution in general. Not so!

Jeremiah Peschka has been making extensive use of dynamic SQL throughout his career to solve a variety of problems. He’ll set about dispelling these misconceptions and demonstrate how dynamic SQL can become a part of every DBA’s tool kit.

Session Goals

  • Learn how to work effectively with diverse inputs.
  • Learn how to avoid common pitfalls of dynamic SQL.
  • Develop an understanding of real world uses for dynamic SQL.

Digits, Dates, and Databases

Calendar and number tables migh not help you get a date, but they can help you accomplish difficult tasks in the database while sticking true to your set-based roots. Nobody wants to admit that they’ve used a while loop in their SQL code, but there are ways to avoid looping constructs and use a set-based solution using a numbers table for loop control. Date manipulation can be cumbersome to peform, can create unwieldy and unreadable code, and can even create performance bottlenecks. Using a calendar table can alleviate these performance bottlenecks and make many calculations easy.

Session Goals

  • Learn what number and calendar tables are.
  • Learn how to effectively use a numbers table in set-based SQL.
  • Learn how to work with a calendar table to remove the need for complex date manipulation.

What are you waiting for? Submit your sessions today!

Calendar Table? Yes please!

I finally got sick of not having a calendar table, so I went ahead and made one today. It’s nothing special and it really didn’t take much time, but I thought I would go ahead and share my script in the hopes that google will index it and somebody won’t have to think about it:

CREATE TABLE [dbo].[Calendar](
	[Date] [datetime] NOT NULL,
	[FirstDayOfMonth] [datetime] NOT NULL,
	[LastDayOfMonth] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED
(
	[Date] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 100) ON [PRIMARY]
) ON [PRIMARY]
GO

DECLARE @the_date AS DATETIME;

SET @the_date = N'19990101'

-- Oooh, a loop! Yup, I was feeling very lazy and procedural and
-- really didn't feel like writing a nifty cross join to get 50 bajillion
-- rows or anything like that.
WHILE @the_date < (DATEADD(yyyy, 10, GETDATE()))
BEGIN
  INSERT INTO Calendar
  (
    Date,
    FirstDayOfMonth,
    LastDayOfMonth
  )
  VALUES
  (
    @the_date,
    DATEADD(mm, DATEDIFF(mm, 0, @the_date), 0),
    -- My data is not very granular by time, you might want to decrease this to
    -- three milliseconds if you have a lot of time-based data to store.
    DATEADD(ss,-1,DATEADD(mm,1,DATEADD(mm,DATEDIFF(mm,0,@the_date),0)))
  );

  SELECT @the_date = DATEADD(dd, 1, @the_date);
END

Or, if you have a numbers table, you could do something like this for a more set based approach:

DECLARE @the_date AS DATETIME;
SET @the_date = N'19990101'

INSERT INTO Calendar
(
  Date,
  FirstDayOfMonth,
  LastDayOfMonth
)
SELECT DATEADD(dd, n, @the_date),
       DATEADD(mm, DATEDIFF(mm, 0, DATEADD(dd, n, @the_date)), 0),
       DATEADD(ss,-1,DATEADD(mm,1,DATEADD(mm,DATEDIFF(mm,0,DATEADD(dd, n, @the_date)),0)))
  FROM nums

My numbers table has 1,000,000 rows in it, so running this will take “some time”. You’ll probably want to add a where clause to limit the number of days you create in your calendar table if that’s the case.

This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 219 items have been purified.