0

I am novice in sql , I'm trying to find all the columns which are having null values in my database. i have searched a lot answers but not satisfied

Shankar
  • 9
  • 1

2 Answers2

1

Here is the example with MS SQL with later version:

declare @col varchar(255), @cmd varchar(max)

DECLARE getinfo cursor for
SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID
WHERE t.Name = 'YOUR_TABLE_NAME'

OPEN getinfo

FETCH NEXT FROM getinfo into @col

WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @cmd = 'IF NOT EXISTS (SELECT top 1 * FROM YOUR_TABLE_NAME WHERE [' + @col + '] IS NOT NULL) BEGIN print ''' + @col + ''' end'
    EXEC(@cmd)

    FETCH NEXT FROM getinfo into @col
END

CLOSE getinfo
DEALLOCATE getinfo
Ajay Gupta
  • 703
  • 5
  • 11
0

you can try this

SELECT cols
FROM table
WHERE cols IS NULL
Shadiqur
  • 490
  • 1
  • 5
  • 18
  • Please read "[answer]". It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. – the Tin Man Mar 11 '22 at 05:57