Back to Code Snippets

MAUVIERE

@eric.mauviere-2


Remove NULL columnsSQL

Clean a table removing columns with only NULL values

Execute this SQL

CREATE OR REPLACE MACRO notnullcols(tablename) AS (
	WITH t1 AS (
			UNPIVOT( FROM query_table(tablename) SELECT max( columns(*)) ) 
			ON columns(*)::varchar
	) FROM t1 SELECT list(name) 
) ;

SET VARIABLE notnullcols = notnullcols(t);

FROM t SELECT columns(c -> list_contains(getvariable('notnullcols'), c)) ;

Copy code

MAUVIERE

Expand

Share link


Delete NULL lines SQL

Clean a table removing lines where all cols values are NULL

Execute this SQL

WITH t AS (
VALUES (NULL, NULL),
  ('john', 'smith'),
  ('mike', NULL),
  (NULL, 'jones'),
  (NULL, NULL)
) FROM t
EXCEPT
FROM t
WHERE COLUMNS(*) IS NULL ;

Copy code

MAUVIERE

Expand

Share link