Scott Elkin
.net, technology and my family
RSS 2.0
Atom 1.0
Browse by Tags
All Tags
»
SQL
(RSS)
Programming
Aug
01
Using a Case Statement in a SQL Where Clause
It is common knowledge that you can apply a Case statement in the SELECT or ORDER BY portion of a SQL statement. What isn’t well known is that you can use it effectively in a WHERE clause. Take an example I just ran into: I want to show all orders
Read More...
Apr
19
Alphabetizing Words in a Phrase Using SQL
I had the need to today to alphabetize a phrase using SQL. For example, I needed "red cowboy pretty hat" to read "cowboy hat pretty red". Here is the code I wrote to do so: declare@inputvarchar(100)set@input="redcowboyprettyhat"--declarationsDECLARE@iint,@lenint,@wordvarchar(100),@charvarchar(1),@newphrasevarchar(1000)--Variable/ConstantinitializationsSET@i=1SET@len=LEN(@input)SET@word=""SET@newphrase=""createtable#temp(wordvarchar(100))WHILE@i@lenbeginSET@char=SUBSTRING(@input,@i,1)IF@char=""begin--addwordtotemptableiflen(@word)>0insertinto#temp(word)select@wordSET@word=""endelseSET@word=@word+@charSET@i=@i+1endselect@newphrase=@newphrase+""+wordfrom#temporderbywordprint@newphrasedroptable#temp
Read More...
Mar
24
Find and Delete Duplicates in SQL
We all know we shouldn't have duplicates in the database. And despite my best efforts, somehow they sneak in from some legacy code, or from the hyper-active-compulsory-submit-the-form-fifty-times-in-five-seconds-bloke. So now and then I find myself
Read More...
Feb
23
Using Case in Order By with Group By
Wow, that’s a mouthful. This is what I am trying to convey: When you do a Group By with Rollup, what happens is that the rolled up column shows null, and won’t necessarily be ordered at the bottom. For example: .code { word-wrap:break-word;
Read More...
Sep
10
Using SQL Dateparts
I don't know what my problem is with my short term memory, but certain things defy my ability to remember. Which way do I turn the faucet? Did I pass my street to the barbershop? What the heck DID I do last weekend? In all seriousness, I was posed
Read More...
May
06
Using SQL Where Clauses for Searching
One piece of SQL lore I take for granted is COALESCE(). The dictionary definition describes SQL's use pretty close as "To bring or come together into a united whole". Books Online states in laymans terms that SQL allows you to pass any number
Read More...
May
06
Retrieving Values from SQL Exec()
Never had a good reason to do this up until now, but if you need to get a value from a generated sql string, there are a few hoops you must jump through. Example: declare @table varchar(20) ,@username varchar(30)--can't use double quotes w/out this
Read More...
Apr
29
Sql Dependencies
This is a rad sproc to find dependencies from Brett. Sometimes I come across a sproc that I *think* I can delete but you never can be too sure. This makes that process A LOT easier. I am reposting it here for my own benefit with some slight formatting
Read More...
Feb
06
Count Distinct Rows in SQL Server
Question: How do you count distinct rows in a query? 1st Try: I *thought* this would work: select distinct count(taskid) from #temp however, it returned the count of all taskid's. 2nd Try: then I tried keeping duplicates out to begin with, so I could
Read More...
Dec
23
Generate Inserts from a Stored Procedure
I have said it before, but I use this website as a storage facility for my brain. Trying to find that one useful script I used 2 years ago is always harder than it needs to. Well, here is the latest cool script I couldn't find. It was written by Narayana
Read More...
Aug
17
Setting a Column to Itself in a Where Clause
Many developers don't realize how handy it can be to set a column to itself in a WHERE clause in SQL. Here is a short list of when I have used it to tidy things up: Lets say I have a sproc that lets me choose ALL employees OR only employees that are
Read More...
Jul
23
Export your Data In Sql Server Using A Stored Procedure
This is seriously one of the coolest stored procedures there is. It is called sp_generate_inserts. I did not write this. I got pissed off one day because I was tired of dealing with the pains of exporting/importing data, so I started looking around before
Read More...
Jul
20
Find All Children of Parent in Stored Procedure
Given a Parent ID, what is the best way to return all child records, their child records, ad inifitum? I needed this to find all employees beneath a given manager no matter how deep. So given a table of employees with a employeeid and managerid field,
Read More...
Jun
18
How To Debug Stored Procedures
Post inprogress. As I have two friends learning the fun of Stored Procedures, I figure I would write up the questions and how-to's they are experiencing and I am having to demo or explain. Create a dummy Stored Procedure (hereafter referred to as
Read More...
Jun
10
Sql Server Templates
I keep saying this, but I can't believe I have been programming in Sql Server for this many years, and never heard of templates. While in Query Analyzer, hit Ctrl-Shift-Ins. Choose the type of template you want to use. Replace all the code between
Read More...
More Posts
Next page »
Comics
Penny Arcade
Little Gamers
Garfield
Dilbert
TrueNuff
Design
Caffeine Web Design
A List Apart
Slayer Office
Jeffrey Zeldman
Programming
Eric Gunnerson
Scott Guthrie
Scott Watermasysk
Scott Mitchell
Andy Smith
Aaron Boodman
Scott Hanselman
Elegant Code
Fun
Preshrunk
Hacking Netflix
Friends
Erica Falke
Samantha Elkin
Gary Sinard
Chris Henry
Brandon Dove
Charles Pham
David Barkol
Nam Huong
Electronics / Gadgets
Boing Boing
Woot
2600
Gadget Madness
Gizmodo
Tags
AJAX
ASP.NET
ASP.NET 2.0
C#
Celeb Spottings
Codesmith
Community Server
Cooking
CSS
Enterprise Library
Exceptions
Family
gaming
iPod
iTunes
Javascript
mma
Music
Netflix
Personal
Pets
Phoenix
Photos
Podcasts
Prius
Programming
ps3
Reshaper
Resharper
RSS
SQL
technology
Tivo
TV
UFC
Vacation
VB.NET
Visual Studio
Windows
Xbox 360
XHTML
Recent Posts
A Parent Must Have: Flip Video Camcorder
Ebay's Paypal Shipping Is Raping You
How to Eat in the Apple Store
Google Reader Broken In Firefox
Twittering Samantha's Delivery
Search
Readers
Find out who's reading this blog, powered by MyBlogLog.com.
View reader community »