Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Highly Rated Users

Forum
ramireddyindia
gmmastros
SQLDenis

Top 50
Given
Received

Links

Wiki
Blog

Forum Statistics

Users
Members:
543
Members Online:
8
Guests Online:
4

Total Post History
Posts:
45704
Topics:
9397

7-Day Post History
New Posts:
343
New Topics:
81
Active Topics:
89

Our newest member
sangeeta

Other

FAQ
All times are UTC [ DST ]

Google Ads

SQL Server Programming Hacks

Microsoft SQL Server
Please wait...

SQL Server Programming Hacks

Postby SQLDenis on Fri Nov 02, 2007 3:24 pm

This is a collection of SQL hacks available on the Wiki, right now we have 8 sections and between 70 and 80 hacks. Ideally we will have more hacks and we will also have a SQL admin hacks page in the future.

Sections
* 1 NULLS
* 2 Dates
* 3 Sorting, Limiting Ranking, Transposing and Pivoting
* 4 Handy tricks
* 5 Pitfalls
* 6 Query Optimization
* 7 Undocumented but handy
* 8 Usefull Admin stuff For The Developer


Below are direct links to all the hacks on the wiki, you can also get a list of all the hacks on the wiki itself here: SQL Server Programming Hacks


NULLS

Dates

Sorting, Limiting,Ranking, Transposing and Pivoting


Handy tricks


Pitfalls

Query Optimization

Undocumented but handy


Usefull Admin stuff For The Developer
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467
 
Posts: 11779
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond

Re: SQL Server Programming Hacks

Postby SQLSister on Thu Jun 05, 2008 2:24 pm

Nice Dennis, You guys have been busy haven't you?
SQLSister
LTD Senior Moderator
LTD Senior Moderator
LTD Bronze - Rating: 64LTD Bronze - Rating: 64
 
Posts: 341
Joined: Wed Jun 04, 2008 10:43 pm
Location: Virginia Beach
Unrated

Re: SQL Server Programming Hacks

Postby chrissie1 on Thu Jun 05, 2008 2:31 pm

Nah, not really ;)
User avatar
chrissie1
LTD Admin
LTD Admin
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264
 
Posts: 6038
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: SQL Server Programming Hacks

Postby hakus on Mon Jun 30, 2008 1:31 am

cool collection ... more more more
I recommend krakow school of english: learn polish krakow
hakus
Newbie
Newbie
 
Posts: 1
Joined: Mon Jun 30, 2008 1:26 am
Unrated

Re: SQL Server Programming Hacks

Postby Remou on Mon Jun 30, 2008 7:55 pm

It is a great achievement.
x
User avatar
Remou
LTD Admin
LTD Admin
LTD Silver - Rating: 427LTD Silver - Rating: 427LTD Silver - Rating: 427LTD Silver - Rating: 427LTD Silver - Rating: 427
LTD Silver - Rating: 427LTD Silver - Rating: 427
 
Posts: 3160
Joined: Sun Oct 14, 2007 11:26 am
Unrated

Re: SQL Server Programming Hacks

Postby SQLDenis on Mon Jun 30, 2008 8:00 pm

We had 80 hacks when we started and now we have 84. I have been concentrating on the SQL Server Admin Hacks lately
Right now we have 24 admin 'hacks' and these are not categorized yet
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467
 
Posts: 11779
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond

Re: SQL Server Programming Hacks

Postby dodo666 on Tue Aug 26, 2008 9:00 am

Gr8 stuff guyz, :thumright:

Thnx.
User avatar
dodo666
Newbie
Newbie
 
Posts: 1
Joined: Thu Jun 26, 2008 12:31 pm
Location: Croatia
Unrated

Re: SQL Server Programming Hacks

Postby Emtucifor on Tue Aug 26, 2008 10:10 pm

Do you want to put "Date Range WHERE Clause Simplification" under the dates section?
It's more cheesier if you do it right the first time!
User avatar
Emtucifor
Senior Sage
Senior Sage
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753
 
Posts: 2152
Joined: Fri May 30, 2008 9:30 pm
Location: California
Unrated

Re: SQL Server Programming Hacks

Postby SQLDenis on Tue Aug 26, 2008 10:42 pm

Will do (l8er 2nite)
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467
 
Posts: 11779
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: SQL Server Programming Hacks

Postby SQLDenis on Wed Aug 27, 2008 2:09 pm

I moved it
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467
 
Posts: 11779
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: SQL Server Programming Hacks

Postby JeffModen on Sun Oct 11, 2009 3:44 am

Since it's a Wiki, can anyone add to it? If so, how to add to existing Wiki posts and how to create a new one, please?
JeffModen
Apprentice
Apprentice
LTD Bronze - Rating: 12
 
Posts: 45
Joined: Fri Oct 09, 2009 5:54 am
Unrated

Re: SQL Server Programming Hacks

Postby JeffModen on Sun Oct 11, 2009 5:41 am

To give an example of why I wanted to get in on the Wiki, let's look at the undocumented hack called xp_DIRTREE. The Wiki has code in it like this...
  1. EXEC master..xp_dirtree 'C:\Program Files\Microsoft SQL Server\MSSQL'


If you run something like that, you get a listing of directories and all subdirectories along with what the depth of the directory is. While that's nice, there are other uses for xp_DIRTREE if you understand that there's not just one operand for DIR, but 3

The first operand you've already seen and guessed. It's the "base" directory that you want to work down from.

The second operand is numeric. 0 (the default when not included), says to list all subdirectories. Any other number will be the limit to the depth of subdirectories found. If you GOOGLE xp_DIRTREE, you can find many articles about this second operand.

Not so true with the third operand. I think there might only be two articles on all of GOOGLE that describes it. 0 (the default value when not included), says to list only directories. Any non-zero value tells it to also list file names along with the directories and an extra column that shows up called "file"... if that column has a 0 in it, its a directory. If that column has a 1 in it, it's a FILE!

Try running this and see...
  1. EXEC master..xp_dirtree 'C:\Program Files',2,1


I use the 3 operand version in my BULK INSERT ETL code all the time to fill a file with file names. xp_DirTree is available in 2k, 2k5, and 2k8. Dunno about 2010.
  1. CREATE TABLE #MyDir
  2.         (
  3.         RowNum INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
  4.         Name   VARCHAR(256),
  5.         DEPTH  SMALLINT,
  6.         IsFile TINYINT
  7.         )
  8.  
  9.  INSERT INTO #MyDir
  10.         (Name, DEPTH, IsFile)
  11.    EXEC Master..xp_DirTree '<somedirectoryhere>',1,1
  12.  
  13.  SELECT * FROM #MyDir WHERE IsFile = 1
JeffModen
Apprentice
Apprentice
LTD Bronze - Rating: 12
 
Posts: 45
Joined: Fri Oct 09, 2009 5:54 am
Unrated

Re: SQL Server Programming Hacks

Postby chrissie1 on Sun Oct 11, 2009 7:58 am

JeffModen wrote:Since it's a Wiki, can anyone add to it? If so, how to add to existing Wiki posts and how to create a new one, please?


Editing is simple, you just go to the article and then click on the edit link in the sidebarmenu.

Adding an article is also simple (most of the time). YOu just go to the desired category and then fill in some title in the create article in this category textbox.
User avatar
chrissie1
LTD Admin
LTD Admin
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264
 
Posts: 6038
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: SQL Server Programming Hacks

Postby Naomi on Tue Oct 13, 2009 4:06 am

BTW, thanks to both you guys I finally read it - good read. Otherwise I was just skiping this thread...
Premature optimization is the root of all evil in programming.
Donald Knuth, repeating C. A. R. Hoare
User avatar
Naomi
Sage
Sage
LTD Bronze - Rating: 79LTD Bronze - Rating: 79
 
Posts: 996
Joined: Thu Mar 26, 2009 5:46 am
Location: WI
Unrated

Re: SQL Server Programming Hacks

Postby onpnt on Mon Jan 04, 2010 3:04 am

Tarwn: Yeah yeah, all you do is SELECT * all day long and say "no" to people...Life of a DBA
User avatar
onpnt
LTD Admin
LTD Admin
LTD Silver - Rating: 463LTD Silver - Rating: 463LTD Silver - Rating: 463LTD Silver - Rating: 463LTD Silver - Rating: 463
LTD Silver - Rating: 463LTD Silver - Rating: 463LTD Silver - Rating: 463
 
Posts: 1072
Joined: Tue Oct 09, 2007 5:23 pm
Location: Kenosha, WI