Questions tagged [coalesce]

COALESCE is a SQL function that returns the first non-NULL expression among its arguments. COALESCE() is ANSI standard and may be used instead of vendor-specific alternatives such as ISNULL(), NVL() or IFNULL().

Return the first non-NULL argument. The COALESCE(expression1, expression2... expressionN) function is variety of the CASE expression.

Examples

Consider the expression:

CASE
   WHEN (expression1 IS NOT NULL) THEN expression1
   WHEN (expression2 IS NOT NULL) THEN expression2
   ...
   ELSE expressionN

It is equivalent to

COALESCE(expression1, ... expressionN);

Another usage:

SELECT
  field1,
  COALESCE(SUM(customers), 0), -- if the SUM is NULL, it would be calculated as 0
  field3,
FROM
  ...

References

932 questions
-1
votes
1 answer

make data all data return in one row

I have a table and my data look like this: ID name contact 1234 company XYZ jane@xyz.com 1234 company XYZ john@xyz.com 1234 company XYZ sue@xyz.com 1234 company XYZ mark@xyz.com 1234 company XYZ jack@xyz.com 1234 …
Kia
  • 1
  • 3
-1
votes
4 answers

How to display a message when all columns values are null

SELECT NVL(T1.C1, 'NO RECORDS'), T1.C2, T2.C3, T1.C4 FROM TEMP T1, TEMP_1 T2 WHERE T1.C2 = T2.C2 AND T1 IN ( 'T001','T002') ; I'm getting all null values for the columns, what can I do to display a message, I tried using Coalesce it did…
aajmee
  • 25
  • 1
  • 3
-1
votes
4 answers

How Coalesce works in sql server?

Create table test(Names varchar(100) primary key ) insert into test values('Hugeman') insert into test values('Jack') insert into test values('William') insert into test values('Kevin') insert into test values('Peter') Query 1: declare @sql…
Vignesh M
  • 168
  • 3
  • 14
-1
votes
3 answers

COALESCE STRING IN sql STATEMENT

I have a COALESCE statement extracting data from a table: DECLARE @Output NVARCHAR(MAX) SELECT @Output = COALESCE(@Output + ', ', '') + '''' + tCCGroup + '''' FROM tblActivityPerPPCC_Matrix WHERE tPPCC = 'NSEA0101' SELECT @Output OUTPUT with…
-1
votes
1 answer

How to properly collapse MySQL rows into a single list

I have a query of the form SELECT pub.id, topic.id, pub.title, etc... FROM pub, topic, link WHERE (matching logic) which returns in the form pub.id | topic.id | pub.title --------------------------------- x1 | a | y1 x1 | …
-1
votes
1 answer

SQL simplify coalesce query

I've created the following query: select k.vname, k.nname, coalesce( to_char(( select a1.ablaufdat from kunde k1 natural join abonnement a1 where k.vname = k1.vname and k.nname = k1.nname and a1.ablaufdat <…
user3573256
  • 219
  • 1
  • 2
  • 12
-1
votes
1 answer

Coalesce for fetching ids

Here is the coalesce function I want to use to fetch all the contract ids separated by comma with contract title in where clause. declare @tempContractID int SELECT @tempContractID = COALESCE(@tempContractID,'') + ContractID + ',' FROM…
Srivastava_Ajita
  • 95
  • 2
  • 3
  • 9
-1
votes
6 answers

SQL Server calculation, how to modify?

We are doing the following sql server calculation: SELECT SUM(COALESCE(ceiling(xm.datapoints / 8) + 1, 1) Problem is that we shouldn't always add + 1 to the ceiling value. If xm.datapoints % 8 > 0 then we should add + 1. If xm.datapoints % 8 = 0…
user2500179
  • 291
  • 1
  • 2
  • 6
-1
votes
1 answer

count coalesce to count from two columns

I have two columns product productactual and the following data: shoes NULL slippers NULL shoes sandals slippers NULL sandals shoes I have to count the Number of products I am using Count…
-1
votes
2 answers

Using Coalesce For Comma Delimited List

Coalesce will return the first non-null value among its arguments documentation says. I can also use it like below: DECLARE @ColumnValue VARCHAR(MAX); SELECT @ColumnValue = COALESCE(@ColumnValue+',','') + ColumnValue FROM dbo.TableA SELECT…
lbrahim
  • 3,710
  • 12
  • 57
  • 95
-1
votes
3 answers

Complex Full Outer Join

Sigh ... can anyone help? In the SQL query below, the results I get are incorrect. There are three (3) labor records in [LaborDetail] Hours / Cost 2.75 / 50.88 2.00 / 74.00 1.25 / 34.69 There are two (2) material records in…
-1
votes
1 answer

Update only changed values using coalesce and dynamic SQL

I created a stored procedure to update only the rows of a table that have changed I used the SQLSERVER coalesce function. CREATE PROCEDURE update_only_changed @FName varchar(50) = NULL, @LName varchar(50) = NULL, …
Ricardo Binns
  • 3,228
  • 6
  • 44
  • 71
-1
votes
2 answers

COALESCE with a Potential Empty Field - SQL Server 2005

Here's the issue: When a Patient comes into the doctor's office, they can have their blood pressure taken 3 times. The first time is bp1; the second is bp2 and the last time is bp3. I need to find the last time a patient had their blood pressure…
DataGirl
  • 429
  • 9
  • 21
-2
votes
1 answer

My older simple code to coalesce two data frames in R is no longer working despite not changing

I'm back to back to an older R script I wrote a year ago where I was coalescing two data frames with the same column names. I was using simple code: temp_2_1 <- dplyr::coalesce(tp2, tp1) tp2 and tp1 are both data frames that have 4 columns with the…
-2
votes
1 answer

sparse to dense array javascript

I have a sparse array in Javascript const sparseArray = [ {"id":3, "value":"banana"}, {"id":7, "value":"coconut"} ]; const denseArraySize=10; I would like to get : const denseArray = [ {"id":0, "value":undefined}, {"id":1,…
Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28