Solved

NULL all blank recordsets in my SQL Sever 2005 table

Posted on 2008-06-17
4
844 Views
Last Modified: 2008-06-22
I have a table which I created by importing a flat file (using SSIS).  The table is set to all nvarchar datatypes at the moment.  I would like to convert the columns of the table into specific datatypes mosty numeric.  However before I do this I would like to convert all the blank spaces into NULLS.  

In addition, I would like to be able to perform a CREATE > INSERT a New Table out of this data and cast all the columns to numeric datatypes, while leaving the blanks NULL and not '0'.



0
Comment
Question by:RunnerNwizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 11

Expert Comment

by:CMYScott
ID: 21808649
if you have a few columns to do, it would be like:

UPDATE tableName SET columnName=null WHERE columnName=''

as for the second part of your question it would be something like this..

SELECT CAST(column1 as int) as column1, CAST(column2 as numeric(10,2)) as column2
INTO newTableName
FROM tableName
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 21808659
Blank spaces or blank fields or fields with blank spaces?

To NULL all fields that contain blank spaces:
UPDATE MyTable SET MyField = NULL WHERE TRIM(MyField) = ''
0
 

Author Comment

by:RunnerNwizard
ID: 21809007
In order for me to cast a numeric on the nvarchar datatypes i currently have, I have found that that I have to convert using the following line of code below.

What this also does is turn all the EMPTY recordsets into 0.000000, what I would like is to turn the empty cells into NULL


SELECT cast(cast([COLUMN] AS float) As numeric (15,6)). 

Open in new window

0
 
LVL 2

Accepted Solution

by:
mofoworking earned 500 total points
ID: 21809288
Wrap it in a case statement and only cast the rows with values and turn the blank values into NULLs



SELECT CASE WHEN TRIM(COLUMN) = '' THEN NULL ELSE cast(cast([COLUMN] AS float) As numeric (15,6)) END

Open in new window

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

635 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