Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

NULL all blank recordsets in my SQL Sever 2005 table

Posted on 2008-06-17
4
Medium Priority
?
847 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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

916 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