Problem in changing datatype of columns with null values in sql server 2008

Shweta

Well-known Member
Joined
Jun 5, 2011
Messages
514
Hello All,

I have imported some data from a text file into SQL server 20008 R2. All the columns of the data have been imported as varchar(50). Now I am trying to change the datatype of columns using alter table statement. But the problem is, there are few numeric columns with missing values (as I got this data from SAS, it shows dot(.) in numeric columns where the value is missing). When I try to convert them from varchar to float, SQL throws an error "Error converting data type varchar to float.". Kindly suggest.

Regards,
Shweta Jain
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Can't you just run update statements to make them numeric before trying to change the data type?
 
Upvote 0
Yes, you will have to clean up that data first. Perhaps update them to Null is another way to go.
 
Upvote 0
I have used below query to update my columns. Please suggest if there is any other way to do it instead of updating columns one by one.

update Car_sales
set [4-year resale value]= NULL WHERE [4-year resale value] = '.'

update Car_sales
set [price in thousands] = null where [price in thousands]='.'

update Car_sales
set [engine size] = null where [engine size]='.'

update Car_sales
set horsepower = null where horsepower = '.'

update Car_sales
set wheelbase = null where wheelbase ='.'

update Car_sales
set width = null where width ='.'

update Car_sales
set [length]=null where [length]='.'

update Car_sales
set [curb weight]=null where [curb weight]='.'

update Car_sales
set [fuel capacity]=null where [fuel capacity]= '.'

update Car_sales
set [fuel efficiency]=null where [fuel efficiency]='.'
 
Last edited:
Upvote 0
I don't see anything particularly bad with that, since it's a one time cleanup.
A more complex script (that you need to be careful with to make sure it's 100% correct) would be like this, all in one go:

Code:
update 

	Car_sales

set 

	[4-year resale value] = case when [4-year resale value] = '.' then null else [4-year resale value] end,
	[price in thousands]  = case when [price in thousands]  = '.' then null else [price in thousands]  end,
	[engine size]         = case when [engine size]         = '.' then null else [engine size]         end,
	horsepower            = case when horsepower            = '.' then null else horsepower            end,
	wheelbase             = case when wheelbase             = '.' then null else wheelbase             end,
	width                 = case when width                 = '.' then null else width                 end,
	[length]              = case when [length]              = '.' then null else [length]              end,
	[curb weight]         = case when [curb weight]         = '.' then null else [curb weight]         end,
	[fuel capacity]       = case when [fuel capacity]       = '.' then null else [fuel capacity]       end,
	[fuel efficiency]     = case when [fuel efficiency]     = '.' then null else [fuel efficiency]     end

where

(
	[4-year resale value]   = '.'
	or [price in thousands] = '.'  
	or [engine size]        = '.'
	or horsepower           = '.'   
	or wheelbase            = '.'
	or width                = '.'
	or [length]             = '.'
	or [curb weight]        = '.' 
	or [fuel capacity]      = '.'
	or [fuel efficiency]    = '.'
	
)
 
Upvote 0

Forum statistics

Threads
1,221,672
Messages
6,161,199
Members
451,688
Latest member
Gregs44132

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top