SQL Server Nation
For all of your SQL Server needs.

Browse by Tags

  • Unstringing a comma delimited column

    Ever had a column which had a pile of information delimited by commas that you want to unpack ? Well, it is not that hard so long as the information is pretty short, and tightly controlled... -- step 1 create a sample table used as a datasource... if object_id('tempdb..#tmp_colors','U') is not null drop table #tmp_colors create table #tmp_colors (Date datetime, Name varchar(60), Choices varchar(100)) insert #tmp_colors select '01 Mar 2009','John S','Red,Blue,Yellow'...
  • Two things I love about Management Studio 2008

    I've been using the SQL Server product for quite some time now, and I almost always use the IDE programs that ship with the database engine. Enterprise Manager and Query Analyzer certainly had their quicks, but were overall nice tools. Management Studio 2005 was a great improvement over these tools because it integrated Enterprise Manager and Query Analyzer into one tool, and it added some neat bells and whistles. Now that Management Studio 2008 is out, there are two really neat new features...
  • Using TRY...CATCH IN SQL Server 2005/2008

    New to SQL Server 2005 (and present in SQL Server 2008) is the ability to use TRY..CATCH constructs inside of your TSQL code to catch error statements. If you've done any type of programming in other programming languages, you know that this is a very handy tool to have. Whats even better in my opinion is the ability to write a generic error handling procedure you can use inside of your CATCH blocks to capture and record errors when they occur. So, lets take a look at how we can take advantage...
  • Concatenating Results through SELECT statements

    In this SQL tip I am going to show you a neat little trick which involves using a simple SELECT statement to concatenate row values into a single variable value. First, I'll create a temp table named #Results for which I'll store FirstName and LastName data. IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results CREATE TABLE #Results ( FirstName varchar(25), LastName varchar(25) ) GO This script adds some names to the temp table I just created. INSERT INTO #Results(FirstName...
  • Nested Common Table Expressions

    If you've looked up a Common Table Expressions in Books Online, you might have noticed that Microsoft says that you cannot have nested CTEs (meaning, you can't define a CTE in the definition of another CTE. Although this is technically true, you can still accomplish the same functionality in a different manner. Here is an example of a simple CTE using the AdventureWorks database that will return a list of orders. USE AdventureWorks ;WITH CTE_Orders (OrderID, OrderDate) AS ( SELECT PurchaseOrderID...
Copyright SQL Server Nation 2010
Powered by Community Server (Non-Commercial Edition), by Telligent Systems