coalesce

Is the null coalesce operator thread safe?

孤人 提交于 2019-11-27 23:28:25
So this is the meat of the question: Can Foo.Bar ever return null? To clarify, can '_bar' be set to null after it's evaluated as non-null and before it's value is returned? public class Foo { Object _bar; public Object Bar { get { return _bar ?? new Object(); } set { _bar = value; } } } I know using the following get method is not safe, and can return a null value: get { return _bar != null ? _bar : new Object(); } UPDATE: Another way to look at the same problem, this example might be more clear: public static T GetValue<T>(ref T value) where T : class, new() { return value ?? new T(); } And

Is there a coalesce-like function in Excel?

夙愿已清 提交于 2019-11-27 11:05:53
问题 I need to fill a cell with the first non-empty entry in a set of columns (from left to right) in the same row - similar to coalesce() in SQL. In the following example sheet --------------------------------------- | | A | B | C | D | --------------------------------------- | 1 | | x | y | z | --------------------------------------- | 2 | | | y | | --------------------------------------- | 3 | | | | z | --------------------------------------- I want to put a cell function in each cell of row A

ORA-12704: character set mismatch

二次信任 提交于 2019-11-27 09:41:05
Hell when I do: select COALESCE (CORP_ID, 0) from crmuser.accounts; The CORP_ID records which are Null returns 0 but when I do: select COALESCE (EMAIL, 'NO EMAIL') from crmuser.accounts I get an error: ORA-12704: character set mismatch The EMAIL field in NVARCHAR2(30). Is is My Datatype and if so What should I do to return default Values? you should do select COALESCE (EMAIL, n'NO EMAIL') from crmuser.accounts to convert the literal to NVARCHAR. eg http://sqlfiddle.com/#!4/73929/1 vs http://sqlfiddle.com/#!4/73929/2 This generic fix works with columns defined as either VARCHAR2 or NVARCHAR2:

combine rows in data frame containing NA to make complete row

喜欢而已 提交于 2019-11-27 09:29:56
I know this is a duplicate Q but I can't seem to find the post again Using the following data df <- data.frame(A=c(1,1,2,2),B=c(NA,2,NA,4),C=c(3,NA,NA,5),D=c(NA,2,3,NA),E=c(5,NA,NA,4)) A B C D E 1 NA 3 NA 5 1 2 NA 2 NA 2 NA NA 3 NA 2 4 5 NA 4 Grouping by A , I'd like the following output using a tidyverse solution A B C D E 1 2 3 2 5 2 4 5 3 4 I have many groups in A . I think I saw an answer using coalesce but am unsure how to get it work. I'd like a solution that works with characters as well. Thanks! Not tidyverse but here's one base R solution df <- data.frame(A=c(1,1),B=c(NA,2),C=c(3,NA)

?? Null Coalescing Operator --> What does coalescing mean?

故事扮演 提交于 2019-11-27 09:11:05
I'm tempted to lie and say that English is my second language, but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me. I looked up the word and I understand it to be a synonym for 'join'. 'Null Join Operator' still doesn't make sense. Can someone enlighten me? I'm tempted to lie and say that English is my second language...but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me. I looked up the word and I understand it to be a synonym for

Merge rows in a dataframe where the rows are disjoint and contain NAs

跟風遠走 提交于 2019-11-27 08:55:27
I have a dataframe that has two rows: | code | name | v1 | v2 | v3 | v4 | |------|-------|----|----|----|----| | 345 | Yemen | NA | 2 | 3 | NA | | 346 | Yemen | 4 | NA | NA | 5 | Is there an easy way to merge these two rows? What if I rename "345" in "346", would that make things easier? You can use aggregate . Assuming that you want to merge rows with identical values in column name : aggregate(x=DF[c("v1","v2","v3","v4")], by=list(name=DF$name), min, na.rm = TRUE) name v1 v2 v3 v4 1 Yemen 4 2 3 5 This is like the SQL SELECT name, min(v1) GROUP BY name . The min function is arbitrary, you

Why is T-SQL ISNULL() truncating the string and COALESCE is not?

送分小仙女□ 提交于 2019-11-27 03:48:04
问题 Given the following: SELECT ISNULL('XY' + NULL, 'ABCDEFGHIJ') -- Outputs ABC (Why?) SELECT COALESCE('XY' + NULL, 'ABCDEFGHIJ') -- Outputs ABCDEFGHIJ Why are these statements returning different results? 回答1: According to Microsoft documentation, for function: ISNULL(check_expression, replacement_value) replacement_value must be of a type that is implicitly convertible to the type of check_expression . Note that type for 'xy'+NULL is VARCHAR(3) . Because of this your string 'ABCDEFGHIJ' is

Return zero if no record is found

♀尐吖头ヾ 提交于 2019-11-27 03:03:30
问题 I have a query inside a stored procedure that sums some values inside a table: SELECT SUM(columnA) FROM my_table WHERE columnB = 1 INTO res; After this select I subtract res value with an integer retrieved by another query and return the result. If WHERE clause is verified, all works fine. But if it's not, all my function returns is an empty column (maybe because I try to subtract a integer with an empty value). How can I make my query return zero if the WHERE clause is not satisfied? 回答1:

Using COALESCE to handle NULL values in PostgreSQL

ぃ、小莉子 提交于 2019-11-27 00:19:56
问题 I have the following query SELECT DISTINCT pt.incentive_marketing, pt.incentive_channel, pt.incentive_advertising FROM test.pricing pt WHERE pt.contract_id = 90000 group by 1,2,3 order by pt.incentive_marketing; The above query returns the o/p as shown in the attached image However I want to replace all null values by 0 using COALESCE Please let me know how this can be achieved in above SELECT query Now I further modified the query using coalesce as below SELECT COALESCE( pt.incentive

?? Null Coalescing Operator --> What does coalescing mean?

久未见 提交于 2019-11-26 17:48:46
问题 I'm tempted to lie and say that English is my second language, but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me. I looked up the word and I understand it to be a synonym for 'join'. 'Null Join Operator' still doesn't make sense. Can someone enlighten me? 回答1: I'm tempted to lie and say that English is my second language...but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does'