Solved

sql server : Table has blank values in some fields and need to replace with 'NULL'

Posted on 2013-01-29
7
850 Views
Last Modified: 2013-01-29
I have table which has blank space in some fields. I need to replace with NULL i.e. instead of blank spaces I need null values
0
Comment
Question by:sonawanekiran
  • 3
  • 2
  • 2
7 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 38830766
update mytable set mycolumn = null where len(ltrim(rtrim(mycolumn))) = 0
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38830769
Hi,

Do you want to check specific fields, or do you want to check for every field of the table?

Giannis
0
 
LVL 17

Author Comment

by:sonawanekiran
ID: 38830797
@jyparask : For every field
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 25

Expert Comment

by:Lee Savidge
ID: 38830804
Modify my above statement to include your list of fields and it'll work just fine.
0
 
LVL 17

Author Comment

by:sonawanekiran
ID: 38830807
Or better way if I can do like this..

Select "name" -> if name fields is blank then select null
0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 38830913
Hi,

The code bellow is reusable and it takes as a parameter the table name.

It searches for string type fields and updates them. I hope it gives you an idea of what to do if you want to search for other datatypes.

This is for view only. If you want it to actually do some work you may replace the 'PRINT' with 'EXEC'

DECLARE	@TableName VARCHAR(100)
SET		@TableName = 'YourTableName'
DECLARE	@TableField VARCHAR(100)

DECLARE UpdateTableFields CURSOR
FOR		SELECT	C.Name
FROM	SYS.OBJECTS O
		JOIN SYS.COLUMNS C
			ON C.OBJECT_ID = O.OBJECT_ID
		JOIN SYS.TYPES T
			ON C.USER_TYPE_ID = T.USER_TYPE_ID
WHERE	O.NAME = @TableName
		AND T.Name IN
			(
				'text',
				'ntext',
				'varchar',
				'char',
				'nvarchar',
				'nchar',
				'sysname'				
			)

OPEN UpdateTableFields

FETCH NEXT FROM UpdateTableFields 
INTO @TableField

WHILE @@FETCH_STATUS = 0
BEGIN
	
	PRINT
	(
		'UPDATE ' + @TableName + ' SET ' + @TableField + ' = NULL WHERE ' + @TableField + ' = '''''
	)

    FETCH NEXT FROM UpdateTableFields 
	INTO @TableField
END 
CLOSE UpdateTableFields
DEALLOCATE UpdateTableFields

Open in new window


Giannis
0
 
LVL 17

Author Closing Comment

by:sonawanekiran
ID: 38831134
Thanks @jyparask:
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question