130

I am trying to detect if the server is running Express Edition.

I have the following t sql.

DECLARE @edition varchar(50); 
set @edition = cast((select SERVERPROPERTY ('edition')) as varchar)

print @edition

In my instance, @edition = Express Edition (64-bit)

How can I do the following? (C# inspired).

DECLARE @isExpress bit;
set @isExpress = @edition.StartsWith('Express Edition');

4 Answers 4

173

StartsWith

a) left(@edition, 15) = 'Express Edition'
b) charindex('Express Edition', @edition) = 1

Contains

charindex('Express Edition', @edition) >= 1

Examples

left function

set @isExpress = case when left(@edition, 15) = 'Express Edition' then 1 else 0 end

iif function (starting with SQL Server 2012)

set @isExpress = iif(left(@edition, 15) = 'Express Edition', 1, 0);

charindex function

set @isExpress = iif(charindex('Express Edition', @edition) = 1, 1, 0);
3
  • 4
    Note that internally these work differently, especially w.r.t. performance and its use of indexes. For example, a query using colName LIKE 'prefix%' will be very fast when colName is indexed, but colName LIKE '%substring%' or colName LIKE '%suffix' will be slow because SQL Server does not create suffix-trees when indexing text. Similarly using LEFT with a column will also be slow because those queries are not SARGable. SARGability is important: dba.stackexchange.com/questions/162263/…
    – Dai
    Feb 14, 2020 at 6:09
  • I would recommend to test the LIKE 'x%' method mentioned below. In some cases it is much faster
    – tonysepia
    Jun 24, 2020 at 21:04
  • 1
    Logically I would avoid charindex.. I would assume char index will search the entire string to find the first chacacter, then check for the second character etc. Eg it won't realize that you are not interesed if the string starts at position 2 or greater Apr 7, 2021 at 18:49
77

It seems like what you want is http://msdn.microsoft.com/en-us/library/ms186323.aspx.

In your example it would be (starts with):

set @isExpress = (CharIndex('Express Edition', @edition) = 1)

Or contains

set @isExpress = (CharIndex('Express Edition', @edition) >= 1)
2
  • Why not use LIKE Express Edition% ? Also, doesn't your solution return true of "Express Edition" is anywhere in the string? So it's more specifically just a "Contains" sort of way of doing it Feb 15, 2016 at 19:41
  • 3
    @mmcrae While using the like version below would be fine this was the first way that came to mind and looks most like what the OP had. Also, the first expression will only return true if the variable starts with (charindex returns 1) the given argument. The second expression is a contains as it will return true if the argument is found anywhere in the string (charindex returns 1 or greater).
    – Gary.S
    Feb 17, 2016 at 4:40
64

I would use

like 'Express Edition%'

Example:

DECLARE @edition varchar(50); 
set @edition = cast((select SERVERPROPERTY ('edition')) as varchar)

DECLARE @isExpress bit
if @edition like 'Express Edition%'
    set @isExpress = 1;
else
    set @isExpress = 0;

print @isExpress
4
  • If you read the question, it is not a query. Try my example with your answer and then please update your answer showing it works.
    – Valamas
    Apr 15, 2015 at 20:39
  • 2
    You can use like in if-statements in t-sql. This is why I use like. I don't see why you want to give me minus points for that but why not. Apr 16, 2015 at 6:53
  • 14
    This should be the preferred way; it's more elegant, less verbose and "SQL aligned": as it uses the standard SQL LIKE operator, I should not need to read the documentation to understand it! May 22, 2015 at 0:23
  • Beware of this when there might be special characters in the like-string, e. g. '['. One might have to add escaping, but even then it seems to behave differently in different circumstances. Oct 28, 2022 at 12:36
4

Here is the explanation of what you can do while filtering the data - in the end, you just pass the variable in a place where you need it.

WHERE CustomerName LIKE 'a%'
--Finds any values that start with "a"

WHERE CustomerName LIKE '%a'
--Finds any values that end with "a"

WHERE CustomerName LIKE '%or%'
--Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%'
--Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a__%'
--Finds any values that start with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o'
--Finds any values that start with "a" and ends with "o"

SELECT * FROM my_table WHERE upper(my_column) LIKE 'SEARCHED%';
--Starts with, case insensitive 

SELECT * FROM my_table WHERE upper(my_column) LIKE '%SEARCHED';
--Ends with, case insensitive

SELECT * FROM my_table WHERE upper(my_column) LIKE '%SEARCHED%';
--Contains, case insensitive

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.