Posted on August 14, 2008 by Balaji Ramesh
I had a table with two columns: ID and Description. The task was to display the description of all elements whose ID is a power of two. Here’s the query that let me accomplish that:
SELECT ID, Desc FROM tblBits WHERE ID > 0 AND ID & (ID-1) = 0
How does this work?
Every power of two [...]
Filed under: Microsoft, SQL, SQL Server, Technical | Leave a Comment »
Posted on June 5, 2008 by Balaji Ramesh
Using Persited Columns in SQL Server 2005 via t-sql and sql server management studio
Filed under: Code, Microsoft, Performance, SQL, SQL Server, T-SQL, Technical | Tagged: Computed Column, Management Studio, Persisted, SQL Server | 3 Comments »
Posted on April 14, 2008 by Balaji Ramesh
If you want to concatenate values from a specific column like for example:
Table=====1 One2 Two3 Three4 Four
to the following:
Output======One, Two, Three, Four
Here is a simple way to do it:
SQL Server 2005:
SELECT STUFF(SELECT ‘, ‘ + ColumnName FROM TableName FOR XML PATH(”),1,2,”)
SQL Server 2000:
DECLARE @t varchar(4000)SET @t = ”SELECT @t = @t + ‘, [...]
Filed under: SQL, SQL Server, T-SQL, Technical | Leave a Comment »
Posted on March 10, 2008 by Balaji Ramesh
I had this binary column in SQL Server 2005 which was storing the MD5 hashes of certain files. The requirement was to display these hashes in a ASP.NET web UI. So i set about the task of converting the same. Binary data as such cannot be displayed on the UI. It needs to be converted [...]
Filed under: Microsoft, SQL Server, Technical | 4 Comments »
Posted on January 13, 2008 by Balaji Ramesh
If you ever wanted to run multiple openxml queries and join them give this a go:
DECLARE @idoc intDECLARE @idoc1 intDECLARE @doc varchar(1000)DECLARE @doc1 varchar(1000)SET @doc =’<ROOT><Customer CustomerID=”1″ ContactName=”Balaji Ramesh” /></ROOT>’SET @doc1 =’<ROOT><Customer CustomerID=”1″ ContactNumber=”1234567890″ /></ROOT>’–Create an internal representation of the XML document.EXEC sp_xml_preparedocument @idoc OUTPUT, @docEXEC sp_xml_preparedocument @idoc1 OUTPUT, @doc1– Execute a SELECT statement that [...]
Filed under: SQL Server | Leave a Comment »
Posted on January 11, 2008 by Balaji Ramesh
I am currently working on a SQL Server 2005 database that has over 500 stored procedures on it. And finding them on the object explorer has been tedious if i just need to view its contents. So i prefer typing in ‘SP_HELPTEXT <procname>’ to view the contents of the procedure. But SQL Server management studio [...]
Filed under: SQL Server, Shortcuts | Leave a Comment »
Posted on August 7, 2007 by Balaji Ramesh
Want to know what functions are available in your database but don’t want to go through the pain of searching help.. well you can actually look for it right in SQL Server management studio. Connect to your database. Expand programmability -> Functions -> System Functions. This lists out all the available system functions. A [...]
Filed under: Microsoft, SQL Server, Technical | Leave a Comment »
Posted on July 29, 2007 by Balaji Ramesh
Identity columns are kind of a helpful feature. But there might be times when you would want to change the value of an identity column.Guess what, SQL server does not allow it. I have not come across a way by which you can update the identity column but there sure is a way to insert [...]
Filed under: Microsoft, SQL Server, Technical | Leave a Comment »