Implementing StartsWith and Contains Functionality in T-SQL: A Comprehensive Guide

Dec 01, 2025 · Programming · 11 views · 7.8

Keywords: T-SQL | String Matching | SQL Server

Abstract: This article provides an in-depth exploration of implementing string matching functionality similar to C#'s StartsWith and Contains methods in T-SQL. Focusing on retrieving SQL Server edition information using the SERVERPROPERTY function, it details multiple approaches including LEFT function, CHARINDEX function, and LIKE operator with complete code examples and performance considerations. Based on high-scoring Stack Overflow answers supplemented by alternative solutions, it offers practical technical guidance for database developers.

In SQL Server database development, conditional logic based on server edition information is frequently required, such as detecting whether Express Edition is running. While T-SQL, as an extension language for SQL Server, doesn't provide direct string methods like C#, equivalent functionality can be achieved through built-in functions.

Retrieving Server Edition Information

The first step involves obtaining SQL Server edition information, which can be accomplished using the SERVERPROPERTY system function. This function returns property information about the server instance, with the 'edition' parameter specifying the edition type to return.

DECLARE @edition VARCHAR(50); 
SET @edition = CAST((SELECT SERVERPROPERTY('edition')) AS VARCHAR(50));
PRINT @edition;

After executing this code, the @edition variable will contain a string value like Express Edition (64-bit). In practical applications, edition strings may include different suffixes, necessitating flexible matching approaches.

Implementing StartsWith Functionality

The StartsWith functionality checks whether a string begins with a specific substring, with three primary implementation methods in T-SQL.

Using the LEFT Function

The LEFT function returns a specified number of characters from the left side of a string, enabling StartsWith functionality by comparing these characters with the target substring.

DECLARE @isExpress BIT;
SET @isExpress = CASE 
    WHEN LEFT(@edition, 15) = 'Express Edition' THEN 1 
    ELSE 0 
END;

This approach is straightforward but requires prior knowledge of substring length. For 'Express Edition', the length is 15 characters (including spaces). If edition strings vary, the length parameter must be adjusted accordingly.

Using the CHARINDEX Function

The CHARINDEX function returns the starting position of a substring within a string. A return value of 1 indicates the string begins with that substring.

SET @isExpress = CASE 
    WHEN CHARINDEX('Express Edition', @edition) = 1 THEN 1 
    ELSE 0 
END;

This method offers greater flexibility as it doesn't require pre-calculation of substring length. The CHARINDEX function returns 0 when the substring isn't found, so checking whether the return value equals 1 determines if the string starts with the specified substring.

Using the IIF Function (SQL Server 2012+)

Starting with SQL Server 2012, the IIF function can simplify conditional expressions.

SET @isExpress = IIF(LEFT(@edition, 15) = 'Express Edition', 1, 0);
-- Or
SET @isExpress = IIF(CHARINDEX('Express Edition', @edition) = 1, 1, 0);

The IIF function provides more concise syntax but is limited to newer SQL Server versions. For scenarios requiring backward compatibility, the CASE statement is recommended.

Implementing Contains Functionality

The Contains functionality checks whether a string contains a specific substring, regardless of its position.

SET @isExpress = CASE 
    WHEN CHARINDEX('Express Edition', @edition) >= 1 THEN 1 
    ELSE 0 
END;

Unlike StartsWith, Contains only requires the CHARINDEX return value to be greater than or equal to 1. If the substring appears anywhere in the string (including at the beginning), it's considered a successful match.

Alternative Approach Using LIKE Operator

Beyond the aforementioned functions, the LIKE operator can also achieve similar functionality.

IF @edition LIKE 'Express Edition%'
    SET @isExpress = 1;
ELSE
    SET @isExpress = 0;

The LIKE operator supports wildcards, with % representing any sequence of characters. This method offers concise syntax but may have inferior performance compared to the CHARINDEX function, particularly when processing large datasets.

Performance and Best Practices

When selecting an implementation method, performance considerations are crucial:

  1. The CHARINDEX function typically offers the best performance, especially when only checking for substring existence
  2. The LEFT function is highly efficient when substring length is known and exact matching is required
  3. The LIKE operator excels with complex pattern matching but may be less efficient for simple substring matching

It's recommended to choose the appropriate method based on specific application requirements. For simple scenarios like edition detection, either CHARINDEX or LEFT functions are suitable choices.

Complete Example Code

The following complete T-SQL script demonstrates how to detect whether SQL Server is running Express Edition:

DECLARE @edition VARCHAR(50);
DECLARE @isExpress BIT;

-- Retrieve server edition
SET @edition = CAST((SELECT SERVERPROPERTY('edition')) AS VARCHAR(50));

-- Method 1: Using CHARINDEX
SET @isExpress = IIF(CHARINDEX('Express Edition', @edition) = 1, 1, 0);
PRINT 'Detection result using CHARINDEX: ' + CAST(@isExpress AS VARCHAR);

-- Method 2: Using LEFT
SET @isExpress = IIF(LEFT(@edition, 15) = 'Express Edition', 1, 0);
PRINT 'Detection result using LEFT: ' + CAST(@isExpress AS VARCHAR);

-- Method 3: Using LIKE
IF @edition LIKE 'Express Edition%'
    SET @isExpress = 1;
ELSE
    SET @isExpress = 0;
PRINT 'Detection result using LIKE: ' + CAST(@isExpress AS VARCHAR);

Through these methods, developers can effectively implement string matching functionality in T-SQL to meet various business requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.