How Can I Sort A ‘Version Number’ Column Generically Using a SQL Server Query

If You are using SQL Server 2008 select VersionNo from Versions order by cast(“https://stackoverflow.com/” + replace(VersionNo , ‘.’, “https://stackoverflow.com/”) + “https://stackoverflow.com/” as hierarchyid); What is hierarchyid Edit: Solutions for 2000, 2005, 2008: Solutions to T-SQL Sorting Challenge here. The challenge

How to create the linked server for SQL Server 2008 where we have the database from 2000 and 2005

There are a few different ways that you can create a linked server in SQL Server you can use the GUI in SQL Server Management Studio or via a script. Using the instructions on MSDN you can do the following: Click Start, click All Programs, click Microsoft SQL Server 2005 or Microsoft SQL Server 2008, … Read more

How can I generate a temporary table filled with dates in SQL Server 2000?

I needed something similar, but all DAYS instead of all MONTHS. Using the code from MatBailie as a starting point, here’s the SQL for creating a permanent table with all dates from 2000-01-01 to 2099-12-31: CREATE TABLE _Dates ( d DATE, PRIMARY KEY (d) ) DECLARE @dIncr DATE = ‘2000-01-01’ DECLARE @dEnd DATE = ‘2100-01-01’ … Read more

Query extremely slow in code but fast in SSMS

Your code in SSMS is not the same code you run in your application. This line in your application adds a NVARCHAR parameter: ada.SelectCommand.Parameters.AddWithValue(“@clientID”, ClientID); while in the SSMS script you declare it as VARCHAR: declare @clientID varchar(200) Due to the rules of Data Type Precedence the Where client_id = @clientID expression in your query … Read more

Find index of last occurrence of a sub-string using T-SQL

Straightforward way? No, but I’ve used the reverse. Literally. In prior routines, to find the last occurence of a given string, I used the REVERSE() function, followed CHARINDEX, followed again by REVERSE to restore the original order. For instance: SELECT mf.name ,mf.physical_name ,reverse(left(reverse(physical_name), charindex(‘\’, reverse(physical_name)) -1)) from sys.master_files mf shows how to extract the actual … Read more