Question: I have the following mumbers in column B

```
4025015659
4025015660
4025015661
4025015662
-266490.78
-266491.78
-266492.78
-266493.78
0
0
0

```

I need to the delete the all numbers except negative numbers. Is it possible to have a formula or vba code to do so.

Answer: 
The problem appears to have to do with the `NULL` values in some of your columns. In particular, the `SPARE1` and `SPARE2` columns can take on `NULL` values, and your intention is that such `NULL` values should equate. In the query below, I wrapped them on both sides of the join with `COALESCE`, and I am getting the expected output now.

```
SELECT B.BEN, B.Budhol, B.COCODE, B.FIELD, B.OBJ, B.SPARE1, B.SPARE2,    
    SUM(B.value) AS Value,
    Code = STUFF((SELECT ', ' + Code FROM temp2 t
                  WHERE t.BEN = B.BEN AND t.Budhol = B.Budhol AND t.COCODE = B.COCODE AND
                        t.FIELD = B.FIELD AND
                        COALESCE(t.SPARE1, '') = COALESCE(B.SPARE1, '') AND
                        COALESCE(t.SPARE2, '') = COALESCE(B.SPARE2, '')
                 FOR XML PATH('')), 1,1, '')
FROM temp2 B
GROUP BY B.BEN, B.Budhol, B.COCODE, B.FIELD, B.OBJ, B.SPARE1, B.SPARE2;

```

[![enter image description here](https://i.stack.imgur.com/oEjca.png)](https://i.stack.imgur.com/oEjca.png)

[Demo
----](http://rextester.com/KEGUH44249)
You are using the same alias (B) for the inner and the outer query. You have to use different alias (B1 & B2) to differentiate them :

```
SELECT B1.BEN, B1.Budhol, B1.COCODE, B1.FIELD, B1.OBJ, B1.SPARE1, B1.SPARE2,    
       SUM(B1.value) as Value ,
       STUFF((SELECT ', ' + Code 
              FROM #temp2 b2 
              WHERE b2.BEN = B1.BEN and b2.Budhol = B1.Budhol and b2.COCODE = B1.COCODE and b2.FIELD = B1.FIELD and 
                    isnull(b2.SPARE1,'') = isnull(B1.SPARE1,'') and isnull(b2.SPARE2,'') = isnull(B1.SPARE2,'')
              FOR XML PATH('')), 1,1, '') as Code
FROM #temp2 B1
GROUP BY B1.BEN,B.Budhol, B1.COCODE, B1.FIELD, B1.OBJ, B1.SPARE1, B1.SPARE2;

```

**Update:** 
Additionally, as Tim Biegeleisen has seen, **b2.spare2 = b1.spare2** will return false when both are null (null is an empty state, not a value), so you should convert those null states to some values that can be compared, using the coalesce or isnull functions : **isnull(b2.spare2,'') = isnull(b1.spare2,'')**