Solved

NULL all blank recordsets in my SQL Sever 2005 table

Posted on 2008-06-17
4
835 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
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

803 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