Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there StartsWith or Contains in t sql with variables?

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'); 
like image 621
Valamas Avatar asked Feb 29 '12 05:02

Valamas


People also ask

Is there a Contains function in SQL?

CONTAINS is a predicate used in the WHERE clause of a Transact-SQL SELECT statement to perform SQL Server full-text search on full-text indexed columns containing character-based data types. CONTAINS can search for: A word or phrase. The prefix of a word or phrase.

How do you write contains in SQL?

For Microsoft SQL Server, CONTAINS() allows for a full text pattern match SQL search queries on your table. It returns a boolean value that indicates whether the function is truthy or falsy. SELECT <columnName> FROM <yourTable> WHERE CONTAINS (<columnName>, '<yourSubstring>');

How do I check if a string starts with SQL?

ssll If you want to match the start of the string you should use 'ss%' instead. select * from table_name where column_name line 'ss%'; this query will return only ssll.


1 Answers

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); 
like image 154
Kirill Polishchuk Avatar answered Sep 24 '22 05:09

Kirill Polishchuk