My favorite TSQL Function is COALESCE which returns the first non-null expression among its arguments. If all arguments are null, null is returned. Here are a couple of simple examples:
declare @a varchar(25); set @a = null; declare @b varchar(25); set @b = null; declare @c varchar(25); set @c = 'I am not null'; select coalesce(@a, @b, @c); ------------------------- I am not null (1 row(s) affected) select coalesce(@a, @b); ------------------------- NULL (1 row(s) affected)
Coalesce can be really slick especially if you want to concatenate multiple rows into a single string.
As of .NET 2.0 we can use the C# Coalesce Operator. The same concept applies — provided arguments are evaluated and the first non-null argument is returned.
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(baseKey, true) ?? Registry.LocalMachine.CreateSubKey(baseKey);
Above you will notice “??” is the delimiter. If the baseKey can’t be found, the first argument evaluates to null. In turn, the second argument will create the baseKey and return the reference. I’ve found this particular pattern (return reference if it exists, else create reference and return) to be most useful.
The above code snippet is for demonstration purposes only. It should be noted that the CreateSubKey function creates a new subkey or opens an existing subkey on its own. Perhaps CreateSubKey uses a form of the Coalesce Operator itself…
thanks! very usefull when you have two fields and wanna get the filled one.
select COALESCE (Email1, Email2) AS TABLE
NAPLAN
With havin so much content do you ever run into any problems of plagorism or copyright infringement? My blog has a lot of completely unique content I’ve either written myself or outsourced but it seems a lot of it is popping it up all over the web without my authorization. Do you know any solutions to help prevent content from being stolen? I’d genuinely appreciate it.
When i beloved approximately you will be given executed here. This sketch is usually tasteful, ones wrote subject matter trendy. on the other hand, people get receive bought a edginess in excess of you hope possibly be giving the subsequent. not well it goes without saying are available further recently all over again since precisely the same just about a lot typically interior event people face shield that increase.