If you’ve ever needed to work with sp_who2 to figure out what’s going on, then you know it would be nice to be able to sort the results, right? Well, if you use the following code, then you’ll get exactly that functionality. Enjoy! CREATE TABLE #who2 ( spid INT , [status] SYSNAME , [sid]…
Tag: SQL
Moving Data Between Tables
Very early into my SQL career I had to get comfortable moving records around between tables and databases. There are several ways to do it, you just have to get used to some of the requirements in moving the records. Moving records into a temporary table The first time you move records, you’ll probably need…
SQL 201 – Combining Query Results
Overview There comes a point in your SQL career where you will need to combine the results of several queries into one data set, and present it in one view. When you get to this point, you’ll need to be familiar with the UNION command. With Microsoft SQL 2005, there are two new functions, similar…
SQL 101 – JOIN
Overview I want to cover a fundamental topic for SQL programming. This is the topic you should start studying right after you have a basic understanding of what a relational database, what tables are, and that tables can be related to each other. If you’re a fan of Duct tape, then Joins will become the…
SQL 201 – Sub Queries
Sub queries or nested queries are complete queries wrapped inside of another complete query. Some times this technique is used as a replacement for a JOIN statement. Some times it’s the only way to define exactly what you you want to get from a database. The important thing to learn about sub queries is they…
SQL 101 – GROUP BY
Consider the following table, orders. buyer productName purchaseDate qtyPurchased pricePaid ————- ————— ————— ———– ——— Shannon Lowder pencil 1/1/2000 1 .25 Shannon Lowder paper 1/1/2000 2 1.00 Shannon Lowder Mountain Dew 1/1/2000 1 1.25 Shannon Lowder pencil 1/5/2000 1 …
SQL 101 – Summarizing Data
This is the last of the lessons on functions for the 100 level. I hope you’ve enjoyed them so far, but it’s time to wrap these up! When beginning to learn SQL, it won’t be long until you have to answer questions that require you to summarize the data. It’s one of the primary reasons…
SQL 101 – System Functions
There are many functions built in to SQL 2000. I’m not going to cover them all, since many of them you won’t have a need for until you really dig in deep with SQL programming. But there are seven you should really be familiar with. CASE You have no idea how much you’ll use the…
SQL 101 – Date Manipulation Functions
Welcome back! This post is a followup to the post about string functions. The difference is this time, we’re discussing functions that manipulate dates. GETDATE The most often used function I can think of, with the possible exception of COUNT(*) is GETDATE(). When called this gives you the current datestamp from the server. This gives…
SQL 201 – String Manipulation Functions
In a previous post I introduced you to calculated fields, and mentioned there were many other functions you could use to compute values or columns. It’s time to introduce you to the eight most used functions when it comes to manipulating strings in SQL LEFT and RIGHT If you ever need the first 3 or…