¿Hay startsWith o Contiene en t sql con variables?
Estoy tratando de detectar si el servidor está ejecutando Express Edition.
Tengo el siguiente t sql.
DECLARE @edition varchar(50);
set @edition = cast((select SERVERPROPERTY ('edition')) as varchar)
print @edition
En mi caso, @edition = Express Edition (64-bit)
¿Cómo puedo hacer lo siguiente? (C# inspirado).
DECLARE @isExpress bit;
set @isExpress = @edition.StartsWith('Express Edition');
64
Author: Kirill Polishchuk, 0000-00-00
3 answers
StartsWith
: left(@edition, 15) = 'Express Edition'
o charindex('Express Edition', @edition) = 1
.Contains
: charindex('Express Edition', @edition) >= 1
Ejemplos:
Utilizando left
función:
set @isExpress = case when left(@edition, 15) = 'Express Edition' then 1 else 0 end
O a partir de SQL Server 2012, puede utilizar iif
función:
set @isExpress = iif(left(@edition, 15) = 'Express Edition', 1, 0);
O utilizando charindex
función:
set @isExpress = iif(charindex('Express Edition', @edition) = 1, 1, 0);
77
Author: Kirill Polishchuk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-22 00:23:13
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-22 00:23:13
Parece que lo Que quieres es http://msdn.microsoft.com/en-us/library/ms186323.aspx.
En tu ejemplo sería (comienza con):
set @isExpress = (CharIndex('Express Edition', @edition) = 1)
O contiene
set @isExpress = (CharIndex('Express Edition', @edition) >= 1)
67
Author: Gary.S,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-29 05:12:41
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-29 05:12:41
Usaría
like 'Express Edition%'
Ejemplo:
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
23
Author: Thomas Koelle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-16 07:53:37
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-16 07:53:37