Execute Dynamic SQL commands in SQL Server

In some applications having hard-coded SQL statements is not appealing, because of the dynamic nature of the queries being issued against the database server. Because of this sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can be done quite simply from the application perspective where the statement is built on the fly whether you are using ASP.NET , ColdFusion or any other programming language. But how do you do this from within a SQL Server stored procedure? SQL Server offers a few ways of running a dynamically built SQL statement. These ways are: Writing a query with parameters Using EXEC Using sp_executesql Writing a query with parameters This first approach is pretty straightforward if you only need to pass parameters into the WHERE clause of your SQL statement. Let’s say we need to find all records from the Customers table where City = ‘London’. This can be done easily as the following example shows. ...

Sorting IP Addresses in SQL

IP addresses are represented in dotted-decimal notation, i.e. four numbers, each ranging from 0 to 255 and separated by dots. Each range from 0 to 255 can be represented by 8 bits and is thus called an octet. Some first octet values like 127 have special meaning - 127 means the local computer. Octets 0 and 255 are not acceptable values in some situations. 0 can, however, be used as the second and third octet.

So, as you can imagine, unless we store the data in a sortable friendly way, sorting this data would require some string manipulation. Let’s follow this up with an example:

CREATE TABLE IP_ADDR (COL1 NVARCHAR(30)); INSERT INTO IP_ADDR VALUES (30.33.33.30); 
INSERT INTO IP_ADDR VALUES (256.10.1.2); 
INSERT INTO IP_ADDR VALUES (256.255.10.2); INSERT INTO IP_ADDR VALUES (127.0.0.1); 
INSERT INTO IP_ADDR VALUES (132.22.33.44); INSERT INTO IP_ADDR VALUES (132.10.30.1);
 INSERT INTO IP_ADDR VALUES (132.1.1.132);
  INSERT INTO IP_ADDR VALUES (10.20.30.10); 

Now, if we order by COL1, then we will get:

SELECT * FROM IP_ADDR ORDER BY COL1;

COL1
10.20.30.10
127.0.0.1
132.1.1.132
132.10.30.1
132.22.33.44
256.10.1.2
256.255.10.2
30.33.33.30

As you can see from above, 30.33.33.30 comes last though we should expect it after 10.20.30.10. And likewise, in some other cases as well. Now, if we use the SUBSTRING (SQL Server) - SUBSTR in Oracle function and make use of the CHARINDEX (SQL Server) or INSTR (Oracle) or LOCATE (DB2 LUW), we can easily do these manipulations. Let us take SQL Server as an example - the same methodology would apply to Oracle and DB2 as well but do keep in mind the differences between CHARINDEX(), INSTR(), and LOCATE() - we had discussed these before in some of our blog posts.

SELECT
SUBSTRING(COL1, 1, CHARINDEX('.', COL1) - 1) AS FIRST_OCTET,
CAST(SUBSTRING(COL1, CHARINDEX('.', COL1)+1, CHARINDEX('.', SUBSTRING(COL1, CHARINDEX('.', COL1)+1, LEN(COL1))) - 1) AS INT) AS SECOND_OCTET,
*
    FROM IP_ADDR
        ORDER BY
            CAST(SUBSTRING(COL1, 1, CHARINDEX('.', COL1) - 1) AS INT),
            CAST(SUBSTRING(COL1, CHARINDEX('.', COL1)+1, CHARINDEX('.', SUBSTRING(COL1, CHARINDEX('.', COL1)+1, LEN(COL1))) - 1) AS INT)

The SQL above shows how to use the CHARINDEX() and the SUBSTRING() function to separate out the first. In the second octets…you can do the same with the third and the fourth one and then order by those in the same order, i.e. the first_octet first (after converting it to an integer), then the second_octet and so on. Here are the results from the execution above:

FIRST_OCTET SECOND_OCTET COL1


10 20 10.20.30.10
30 33 30.33.33.30
127 0 127.0.0.1
132 1 132.1.1.132
132 10 132.10.30.1
132 22 132.22.33.44
256 10 256.10.1.2
256 255 256.255.10.2

Comments

Unknown said…
We can also use PARSENAME

SELECT * FROM IP_ADDR
ORDER BY CAST (PARSENAME(COL1, 4) as INT)
, CAST (PARSENAME(COL1, 3) as INT)
, CAST (PARSENAME(COL1, 2) as INT)
, CAST (PARSENAME(COL1, 1) as INT)

Popular posts from this blog

Check If Temporary Table Exists

Multiple NULL values in a Unique index in SQL

How To search entire database?