coalesce

Best way to check for “empty or null value”

和自甴很熟 提交于 2019-11-26 17:01:41
What is best way to check if value is null or empty string in Postgres sql statements? Value can be long expression so it is preferable that it is written only once in check. Currently I'm using: coalesce( trim(stringexpression),'')='' But it looks a bit ugly. stringexpression may be char(n) column or expression containing char(n) columns with trailing spaces. What is best way? Erwin Brandstetter The expression stringexpression = '' yields: TRUE .. for '' (or for any string consisting of only spaces with the data type char(n) ) NULL .. for NULL FALSE .. for anything else So to check for: "

ORA-12704: character set mismatch

可紊 提交于 2019-11-26 14:48:42
问题 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? 回答1: 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

combine rows in data frame containing NA to make complete row

血红的双手。 提交于 2019-11-26 14:45:48
问题 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

Using ISNULL vs using COALESCE for checking a specific condition?

帅比萌擦擦* 提交于 2019-11-26 14:39:52
I know that multiple parameters can be passed to COALESCE , but when you want to to check just one expression to see if it doesn't exist, do you use a default or is it a better practice to use ISNULL instead? Is there any performance gain between the two? onedaywhen This problem reported on Microsoft Connect reveals some differences between COALESCE and ISNULL : an early part of our processing rewrites COALESCE( expression1, expression2 ) as CASE WHEN expression1 IS NOT NULL THEN expression1 ELSE expression2 END . In [this example]: COALESCE ( ( SELECT Nullable FROM Demo WHERE SomeCol = 1 ), 1

How to get the first non-null value in Java?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 14:29:47
Is there a Java equivalent of SQL's COALESCE function? That is, is there any way to return the first non-null value of several variables? e.g. Double a = null; Double b = 4.4; Double c = null; I want to somehow have a statement that will return the first non-null value of a , b , and c - in this case, it would return b , or 4.4. (Something like the sql method - return COALESCE(a,b,c) ). I know that I can do it explicitly with something like: return a != null ? a : (b != null ? b : c) But I wondered if there was any built-in, accepted function to accomplish this. No, there isn't. The closest

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

只愿长相守 提交于 2019-11-26 14:22:28
问题 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? 回答1: 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

COALESCE关键字的使用

守給你的承諾、 提交于 2019-11-26 12:43:09
COALESCE是sql标准里面的一个关键字,我们可以和聚合函数sum,count,max等一起使用完成一些特殊的功能。以下sql语句基于mysql1、查询某一个列总和,如果没有数据或者NULL返回0表字段中有一个字段:公里数(f_gls),统计总的公里数 COALESCE (sum(f_gls), 0) distance 2、查出最大值 表字段中有一个字段:模块id(f_moudleId),查询最大值 COALESCE(MAX(f_moudleId),0) max_moudleId 3、综合使用:格式化 根据知识库表里面的记录,生成下一条知识库id SELECT CONCAT('KB',DATE_FORMAT(NOW(),'%Y%m%d'),LPAD(COALESCE(COUNT(id),0)+1, 3, '0')) <!--格式 KB20190610001--> FROM user_knowledgebase WHERE DATEDIFF(DATE(now()), DATE(createtime))= 0 来源: https://www.cnblogs.com/passedbylove/p/11320561.html

Best way to check for “empty or null value”

ε祈祈猫儿з 提交于 2019-11-26 04:58:55
问题 What is best way to check if value is null or empty string in Postgres sql statements? Value can be long expression so it is preferable that it is written only once in check. Currently I\'m using: coalesce( trim(stringexpression),\'\')=\'\' But it looks a bit ugly. stringexpression may be char(n) column or expression containing char(n) columns with trailing spaces. What is best way? 回答1: The expression stringexpression = '' yields: TRUE .. for '' (or for any string consisting of only spaces

What is the “??” operator for? [duplicate]

不羁的心 提交于 2019-11-26 04:25:33
问题 This question already has answers here : What do two question marks together mean in C#? (17 answers) Closed 6 years ago . I was wondering about ?? signs in C# code. What is it for? And how can I use it? What about int? ? Is it a nullable int? See also: ?? Null Coalescing Operator —> What does coalescing mean? 回答1: It's called the "null coalescing operator" and works something like this: Instead of doing: int? number = null; int result = number == null ? 0 : number; You can now just do: int

Oracle Differences between NVL and Coalesce

◇◆丶佛笑我妖孽 提交于 2019-11-26 00:40:19
问题 Are there non obvious differences between NVL and Coalesce in Oracle? The obvious differences are that coalesce will return the first non null item in its parameter list whereas nvl only takes two parameters and returns the first if it is not null, otherwise it returns the second. It seems that NVL may just be a \'Base Case\" version of coalesce. Am I missing something? 回答1: COALESCE is more modern function that is a part of ANSI-92 standard. NVL is Oracle specific, it was introduced in 80 's