Wednesday, March 28, 2012

How can I check for Null or Empty in an Insert/Select Statement

Is there a way in the following Query to check if the _vocon.COMPANY is
null/empty and if so replace _vocon.Id + 10200000 with null (as shown in
statement 2) as each record is processed. I would like to use something like
IIF( IsNull(_vocon.COMPANY), Null, _vocon.Id + 10200000)
INSERT INTO Contact (ContactID, ContactCode, ContactFormat,Title,
FirstName, MiddleName, LastName, notes, CompanyName, Position, CompanyID,
ContactOwner, CreateDate, CreatedBy, LastChangeDate, LastChangedBy,
LanguageCode, Sex, CurrentBalance, Flag19 ) SELECT _vocon.Id + 10100000,
_vocon.Id + 10100000, 'I', _vocon.TITLE, _vocon.FIRSTNAME,
_vocon.OTHERNAMES, _vocon.LASTNAME, NULL, _vocon.COMPANY, NULL, _vocon.Id +
10200000, 1, '01/08/2003', NULL, '01/08/2003', 0, 1, 'U', 0, 1 FROM _vocon
If the company name is null
INSERT INTO Contact (ContactID, ContactCode, ContactFormat,Title,
FirstName, MiddleName, LastName, notes, CompanyName, Position, CompanyID,
ContactOwner, CreateDate, CreatedBy, LastChangeDate, LastChangedBy,
LanguageCode, Sex, CurrentBalance, Flag19 ) SELECT _vocon.Id + 10100000,
_vocon.Id + 10100000, 'I', _vocon.TITLE, _vocon.FIRSTNAME,
_vocon.OTHERNAMES, _vocon.LASTNAME, NULL, _vocon.COMPANY, NULL, NULL, 1,
'01/08/2003', NULL, '01/08/2003', 0, 1, 'U', 0, 1 FROM _vocon
Regards
Jeff
--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.554 / Virus Database: 346 - Release Date: 20/12/2003Hi
You don't give the version of SQLServer, it is also better to post DDL
(Create table statements etc...), example data (as Insert statements) to
avoid ambiguities.
The behaviour of concatenating with null can be set with the
CONCAT_NULL_YIELDS_NULL option see:
mk:@.MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\tsq
lref.chm::/ts_set-set_2z8s.htm
To update your values you could use:
UPDATE _vocon
SET Company = Id + 10200000
WHERE Company IS NULL
OR LEN(RTRIM(Company)) = 0
John
"Jeff Williams" <jeff.williams@.hardsoft.com.au> wrote in message
news:uqveIhNzDHA.2568@.TK2MSFTNGP09.phx.gbl...
> Is there a way in the following Query to check if the _vocon.COMPANY is
> null/empty and if so replace _vocon.Id + 10200000 with null (as shown in
> statement 2) as each record is processed. I would like to use something
like
> IIF( IsNull(_vocon.COMPANY), Null, _vocon.Id + 10200000)
> INSERT INTO Contact (ContactID, ContactCode, ContactFormat,Title,
> FirstName, MiddleName, LastName, notes, CompanyName, Position, CompanyID,
> ContactOwner, CreateDate, CreatedBy, LastChangeDate, LastChangedBy,
> LanguageCode, Sex, CurrentBalance, Flag19 ) SELECT _vocon.Id + 10100000,
> _vocon.Id + 10100000, 'I', _vocon.TITLE, _vocon.FIRSTNAME,
> _vocon.OTHERNAMES, _vocon.LASTNAME, NULL, _vocon.COMPANY, NULL, _vocon.Id
+
> 10200000, 1, '01/08/2003', NULL, '01/08/2003', 0, 1, 'U', 0, 1 FROM
_vocon
> If the company name is null
> INSERT INTO Contact (ContactID, ContactCode, ContactFormat,Title,
> FirstName, MiddleName, LastName, notes, CompanyName, Position, CompanyID,
> ContactOwner, CreateDate, CreatedBy, LastChangeDate, LastChangedBy,
> LanguageCode, Sex, CurrentBalance, Flag19 ) SELECT _vocon.Id + 10100000,
> _vocon.Id + 10100000, 'I', _vocon.TITLE, _vocon.FIRSTNAME,
> _vocon.OTHERNAMES, _vocon.LASTNAME, NULL, _vocon.COMPANY, NULL, NULL, 1,
> '01/08/2003', NULL, '01/08/2003', 0, 1, 'U', 0, 1 FROM _vocon
> Regards
> Jeff
>
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.554 / Virus Database: 346 - Release Date: 20/12/2003
>

No comments:

Post a Comment