Solved

Inserting null values

Posted on 2011-03-24
13
260 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
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
if the select returns a NULL, a NULL will be inserted in the new table.
0
 
LVL 142

Expert Comment

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

Author Comment

by:zachvaldez
Comment Utility
so by default it places NULL not  empty string or ''
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
exactly.
0
 

Author Comment

by:zachvaldez
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
>>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
Comment Utility
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 69

Expert Comment

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

Expert Comment

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

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
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
Comment Utility
isnull works!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now