SQL Server 2005: Date Only

by Bryan Sever 6/23/2009 10:08:03 AM

In the business world, many reports are date and time sensitive. At times, all that matters is the date or the number of days difference from the current point and time.

This frequently creates the situation where the time portion of the datetime field type does not need and should not examine the time portion of this data type. When running a comparison in SQL Server 2005 for example, June 23rd at 1:00 A.M. is not the same as June 23rd at 12:00 A.M.

Therefore, it becomes necessary to compare the date portion of this type.

The official SQL Server Books online documentation states regarding the datetime type: "The first 4 bytes store the number of days before or after the base date: January 1, 1900."

Note the following examples executed in Query Analyzer application:

select convert(datetime, 39988) 
- output: 2009-06-26 00:00:00.000

select convert(datetime, 39988.5) 
- output:2009-06-26 12:00:00.000

select convert(datetime, 39988 + 14) 
- output:2009-07-10 00:00:00.000

select floor(convert(numeric(18,9),getdate())) 
- output:39985

select convert(datetime, floor(convert(numeric(18,9), getdate() + 14))) 
- output: 2009-07-07 00:00:00.000

The first example shows that a whole number produces a date and time that is 39988 days beyond January 1, 1900. It also shows that a whole number produces no time element in the datetime evaluation.

The second example show that providing a partial number (in this case 1/2) moves the hours to the time elapsed during the day (0.5 = 12 hours).

The third example shows adding 14 days to the previous input.

The fourth example shows getting the current system date and truncating the decimal portion.

The fifth example shows adding 14 days to the current date, truncating the time/decimal portion and then converting the numeric result back to the datetime type.

One consideration for these snippets is to create a SQL Server function that abstracts the conversion process.

Tags:

T-SQL | Microsoft SQL Server | Programming

Example: CSS Floating Layout

by Bryan Sever 4/15/2008 10:18:00 PM

For years, web developers have used the <table> tag to control page formatting. Even tools like Adobe Dreamweaver has templates that make extensive use of the <table> tag for this purpose. However, the <table> tag was intended for displaying tabular data.

CSS 2.1 gives the web developer great control over the format of the page. Although all the major browsers have slightly different implementations, there are few glaring differences.

Click here to learn more.

Tags: , , ,

CSS | Programming | World Wide Web | XHTML

Code Snippet: T-SQL Paging

by Bryan Sever 4/11/2008 11:43:00 AM

Although there is already some infrastructure for paging in many development tools, sometimes a custom solution is needed.

Here's a T-SQL example of how to create your own paging data within your query. In this example, I have aggregated four additional columns. Replace 'sys.sysobjects' with the table that you wish to query. Replace 'sys.sysobjects.[name]' with the column list that you would like to use to order the query results. The @pagesize variable can be modified to set the page size.

declare @resultcount int
set @resultcount = (select count(*) from sys.sysobjects)

declare @pagesize int
set @pagesize = 10

SELECT
    row_number() over (order by sys.sysobjects.[name]) as 'ResultCount',
    convert(int, (row_number() over (order by sys.sysobjects.[name]) - 1) / @pagesize) + 1 as 'CurrentPage',
    convert(int, @resultcount / @pagesize + 1) as 'MaxPages',
    @resultcount as 'MaxResult',
    sys.sysobjects.*
FROM
    sys.sysobjects

Tags: , , , ,

Programming | Microsoft SQL Server | T-SQL | Code Snippet

Speech Synthesis on Windows 2003

by Bryan Sever 3/31/2008 6:07:00 PM
I learned the hard way that the SpeechSynthesis class doesn't run over ASP .NET when hosted on Windows Server 2003. Installing a demo version of Speech Server remedies this problem. Interestingly enough, it can be ran without issue on Windows Vista. I ran the same type of application prototype on a LAMP server with festival installed. The programming was easy and finished within a few seconds. If anybody knows of a better server-based TTS solution, then please leave a comment.

Tags: , ,

ASP .Net | Programming | TTS | Windows Server 2003 | Windows Vista | Open-Source | Linux

Using CollectionBase

by Bryan Sever 3/30/2008 4:12:00 PM

Collection classes can be a convenient method to handling sets of data. In many cases, collections handle strong types better than other classes within the .NET framework. There are several different ways to implement a collection class. This blog entry demonstrates how easy it is to implement a collection class by inheriting the CollectionBase class. See how an amortization schedule can be implemented as a collection class. Read More

Tags: , , , ,

C# | Programming