SQL Server的XQUERY返回NULL,而不是空 [英] SQL Server xQuery return NULL instead of empty
问题描述
因此,在这个例子中,我试图返回一个空nvarchar的一个NULL,而不是为元素1和元素3。我似乎无法找到一个答案,这在任何地方,或者如果它甚至有可能。我知道我可以检查.exists()与案例/当一个NULLIF,但我宁愿不作出这样的检查,为了表现。
WqlEventQuery查询=新WqlEventQuery(); query.EventClassName =__InstanceCreationEvent; query.Condition =TargetInstance ISA'Win32_NTLogEvent'; query.GroupWithinInterval =新时间跨度(0,0,10); System.Collections.Specialized.StringCollection集合= 新System.Collections.Specialized.StringCollection(); collection.Add(TargetInstance.SourceName); query.GroupByPropertyList =收集; query.HavingCondition =NumberOfEvents> 25;
我不知道你是从你的问题写什么语言,但SQL Server有一个 NULLIF(VAL,校验值)
功能。如果你可以包括在SQL传递给了应该做的伎俩服务器:
选择NULLIF(元素1,'')
将返回一个空如果元素1等于空字符串('')
So in this example I'm trying to return a NULL instead of an empty nvarchar for element2 and element3. I can't seem to find an answer to this anywhere, or if it's even possible. I know I can check .exists() with a nullif of Case/When, but I'd rather not make that check for the sake of performance.
WqlEventQuery query = new WqlEventQuery(); query.EventClassName = "__InstanceCreationEvent"; query.Condition = "TargetInstance ISA 'Win32_NTLogEvent'"; query.GroupWithinInterval = new TimeSpan(0, 0, 10); System.Collections.Specialized.StringCollection collection = new System.Collections.Specialized.StringCollection(); collection.Add("TargetInstance.SourceName"); query.GroupByPropertyList = collection; query.HavingCondition = "NumberOfEvents > 25";
I'm not sure what language you are writing in from your question, but SQL Server has a NullIf(val, CheckValue)
function. If you can include that in the sql you pass to the server that should do the trick:
Select NullIf(element2, '')
will return a null if element2 is equal to an empty string ('')
这篇关于SQL Server的XQUERY返回NULL,而不是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!