t sql 中是否有 StartsWith 或 Contains 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9493844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there StartsWith or Contains in t sql with variables?
提问by Valamas
I am trying to detect if the server is running Express Edition.
我正在尝试检测服务器是否正在运行 Express Edition。
I have the following t sql.
我有以下 t sql。
DECLARE @edition varchar(50);
set @edition = cast((select SERVERPROPERTY ('edition')) as varchar)
print @edition
In my instance, @edition = Express Edition (64-bit)
在我的例子中, @edition = Express Edition (64-bit)
How can I do the following? (C# inspired).
我怎样才能做到以下几点?(C# 启发)。
DECLARE @isExpress bit;
set @isExpress = @edition.StartsWith('Express Edition');
回答by Kirill Polishchuk
StartsWith
以。。开始
a) left(@edition, 15) = 'Express Edition'
b) charindex('Express Edition', @edition) = 1
Contains
包含
charindex('Express Edition', @edition) >= 1
Examples
例子
left
function
left
功能
set @isExpress = case when left(@edition, 15) = 'Express Edition' then 1 else 0 end
iif
function (starting with SQL Server 2012)
iif
函数(从 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);
回答by Gary.S
It seems like what you want is http://msdn.microsoft.com/en-us/library/ms186323.aspx.
看起来你想要的是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)
回答by Thomas Koelle
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