Solved

Inserting null values

Posted on 2011-03-24
13
266 Views
Last Modified: 2012-06-27
iM DOING AN INSERT iNTO..SELECT STATEMENT.
Id like to know that if under the select statement the column field is null, Id like to insert NULL in the coulumn- if there is no value.

How is the select statemnt
0
Comment
Question by:zachvaldez
[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
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 35207196
if the select returns a NULL, a NULL will be inserted in the new table.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35207204
you can still select the column by name, if the value is null, indeed a NULL will be inserted.
0
 

Author Comment

by:zachvaldez
ID: 35207259
so by default it places NULL not  empty string or ''
0
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.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35207264
exactly.
0
 

Author Comment

by:zachvaldez
ID: 35207299
If I do in the selct part...

Select Tid,uId, '70' as K_code,tdate from tbal where Tid = 3

Id like to insert Null if '70' is null
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 35207308
if the select returns a null, the resulting table will have a null

if you want to replace empty values with null, you can do something like this:
insert into ...
select field1, field2, case field3 when '' then null else field3 end
from ...
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 35207325
>>Id like to insert Null if '70' is null

'70' is a constant and it is never null! it always have the value '70'
0
 

Author Comment

by:zachvaldez
ID: 35207570
Oh, It's not my intention to place a constant value there. I got wrong logic
So Id like to change that logic then
that if  field dtCode is not null insert '70' as K_code  else null.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 35207594
insert into ...
select field1, field2, case when dtCode is not null then '70' else null end
from ...
0
 

Expert Comment

by:karthik_a1
ID: 35207686
Use NVL function in the select statement then it will be possible enter 70 when NULL comes in the table
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 35207757
isn't NVL an Oracle function? The question here is about MS SQL Server 2005
0
 
LVL 2

Accepted Solution

by:
CoolBurn28 earned 250 total points
ID: 35229797
CREATE TABLE tbl_tmp1(idn int,product varchar(30));
INSERT INTO tbl_tmp1 values(1,'coke');
INSERT INTO tbl_tmp1 values(2,'sprite');
INSERT INTO tbl_tmp1(idn) values(3);
INSERT INTO tbl_tmp1 values(4,'coke');
INSERT INTO tbl_tmp1(idn) values(5);
--
CREATE TABLE tbl_tmp2(idn int,product varchar(30));
--
INSERT INTO TBL_TMP2
SELECT IDN,ISNULL(PRODUCT,'')FROM tbl_tmp1;
--
SELECT * FROM tbl_tmp2;

Open in new window

0
 

Author Closing Comment

by:zachvaldez
ID: 35343364
isnull works!
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

627 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