In this post:
http://weblogs.sqlteam.com/jeffs/archive/2004/12/02/2954.aspx
I talked about separating dates from times for certain columns in your database. One thing I have never really used before in SQL Server 2000 is user-defined datatypes and rules.
Read more →
Update: More on dates and times in T-SQL here.
I don't like storing dates along with the time portion in my database tables. Sure, I use the datatype, but I wish SQL provided a “Date” datatype and a “Time” datatype that were separate.
Read more →
As it is implemented, the RAND() function in SQL Server doesn't let you return a different random number per row in your SELECT statement. For example, if you execute this:
Read more →
Just a quick post of some benchmarking code for our good friend Guayo at TheDailyWTF, in regards to this discussion. The debate is, essentially: Assuming everything is properly indexed, which is faster/more efficient?
Read more →
When you have two tables (or resultsets from SELECT statements) that you wish to compare, and you want to see any changes in ANY columns, as well as to see which rows exist in 1 table but not the other (in either direction) I have found that the UNION operator works quite well.
Read more →
(This is from the SQLTeam forums. I noticed a reply of mine to a user's problem might be useful to others .. so here it is!)
Here's one way to implement a search, similar to Google's, if you don't want to (or can't) use full text indexing.
Read more →
Here's a scenario many of us have encountered:
You inherit a database. TableX has an identity column "ID" set up as the primary key, and contains many duplicates. It is clear a better, natural primary key is a combination of columns A and B.
Read more →
I see many people have the need to pass in multiple pieces of information to stored procedures in a parameter, often as a CSV string. For example, you might have a stored procedure that will return customer info for all customer ID's passed in a CSV string, called something like this:
Read more →
I just wanted to take a minute to highlight this thread in the sqlteam forums:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=39736
It's really interesting stuff, and a bunch of us worked together to come up with some really cool ideas about using T-SQL to solve the classic sliding tile puzzle -- you know, where tiles on a square board are mixed up, with one missing, and you slide them around until the picture is complete or they are in the proper order.
Read more →
Despite the cool things you can do with writing your own custom aggregate functions as shown in my last article, in many cases you don't need custom functions to get what you need.
Read more →
If the topic intrigues you, please check out my latest article:
http://weblogs.sqlteam.com/jeffs/articles/1490.aspx
A little too long for a blog unfortunately. Before reading the article, keep in mind it's purely an academic exercise.
Read more →
(UPDATE:If you are using SQL Server 2005 or above, there is a much easier and more efficient wayto page your data.)
Here is a completely dynamic version of my paging technique in the form of a stored procedure.
Read more →
This may be common knowledge to those among us that are handy with XML (I'm not), but since I just uncovered this nice little trick I thought I'd pass it along either way.
Read more →
Here's the most efficient way that I can think of to return rows @a through @b from a table or query, when each row is ordered and can be uniquely identified by a set of columns.
Read more →
Another way to create dates easily, w/o using CONVERT or using a UDF (i.e., if you have SQL 7.0), if you are starting out with integers representing the year, month and day of the date you need to create:
Read more →
Update: Please refer to this page for an update on this concept; I recommend that you use the newer functions presented on that page.
To me, this is a pretty handy function to have in your toolbox:
Read more →
Anyone know what this will return?
create function RecursiveTest(@V int) returns @t table (i int) as begin set @v = @v - 1 if @v<>0 insert into @t select @v union select * from dbo.
Read more →
As some of you may know, I recommend to avoid using CASE expressions in the WHERE clause of a query to express conditional logic. I prefer to have everything translated to simple ANDs, ORs and NOTs to keep things a) portable, b) easier to read and c) efficient.
Read more →
I recently helped someone with performing some simple cross tabs (or pivots) in SQL, such as:
Select CustomerID, SUM(CASE WHEN type = 'Land' THEN Amount ELSE 0 END) as Land, SUM(CASE WHEN type = 'Sea' THEN Amount ELSE 0 END) as Sea, SUM(CASE WHEN type = 'Air' THEN Amount ELSE 0 END) as Air FROM SomeTable GROUP BY CustomerID
Read more →
I thought I'd post a blog showing a technique I haven't seen elsewhere. This is from a recent post of mine at the SqlTeam forums.
The problem: you have two tables, each containing mutiple rows of data per “SetID”.
Read more →