Return first non-null value

String s = Stream.<Supplier<String>>of(this::first, this::second /*, … */) .map(Supplier::get) .filter(Objects::nonNull) .findFirst() .orElseGet(this::defaultOne); It stops on the first non-null value or else sets the value which is returned from defaultOne. As long as you stay sequential, you are safe. Of course this requires Java 8 or later. The reason why it stops on the first occurrence … Read more

Does Twig have a null coalesce operator?

The null-coalescing operator was formally introduced in Twig 1.24 (Jan. 25, 2016). * adding support for the ?? operator Which means it’s now possible to do this… {{ title ?? “Default Title” }} You can even chain them together, to check multiple variables until a valid non-null value is found. {{ var1 ?? var2 ?? … Read more

Return zero if no record is found

You could: SELECT COALESCE(SUM(columnA), 0) FROM my_table WHERE columnB = 1 INTO res; This happens to work, because your query has an aggregate function and consequently always returns a row, even if nothing is found in the underlying table. Plain queries without aggregate would return no row in such a case. COALESCE would never be … Read more

Changing a SUM returned NULL to zero

Put it outside: SELECT COALESCE( ( SELECT SUM(i.Logged) FROM tbl_Sites s INNER JOIN tbl_Incidents i ON s.Location = i.Location WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0) GROUP BY s.Sites ), 0) AS LoggedIncidents If you are returning multiple rows, change INNER JOIN to LEFT JOIN SELECT COALESCE(SUM(i.Logged),0) FROM tbl_Sites s … Read more

How to use Coalesce in MySQL

How come i saw queries that returns multiple values? isnt it only the first not null value that is returned? Yes, it is only the first non-NULL value that is returned. You must be mistaken about the queries you have seen where you thought that was not the case: if you could show us an … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)