4

I create insert query for Organization table.

select 'Insert into Organizations(Name, IndustryId, ContactPerson, Email, Website, LocationId, ContactNumber, Mobilenumber) values(''' + 
       IsNull(Nameofthecompany, 'NULL') + ''',' + 
       Isnull(IndustryType, 'NULL') + ',''' +
       Isnull(Nameofthepersonresponsibleforrecruitment, 'NULL') + ''', ''' +
       Isnull(EmailId, 'NULL') + ''', ''' +
       Isnull(websiteaddress, 'NULL') + ''',' +
       Isnull(Location, 'NULL') + ',' +
       Isnull(PhoneNumber, 'NULL') + ',' +
       Isnull(MobileNumber, 'NULL') + ')' 
from Organization

Here I have the result set

Insert into Organizations(Name, IndustryId, ContactPerson, Email, Website, LocationId, ContactNumber, Mobilenumber)
values('username', industry, 'Name', 'NULL', 'NULL', place, NULL, 999999999)

I don't want the NULL value within quotes. If I remove the quotes means I get error. Please Help me find out the problem..

6
  • 2
    What is the error? Perhaps those columns are not nullable! Commented Apr 10, 2013 at 4:57
  • Well, what else do you want to have instead of 'NULL' ? Just replace those with whatever you want to add ..... Commented Apr 10, 2013 at 5:00
  • Show me your column definition. You can only insert Null if your column allow Null Commented Apr 10, 2013 at 5:01
  • 1
    show us the table structure of table Organizations. Commented Apr 10, 2013 at 5:12
  • I want instead of 'Null' show my resultset NULL. Commented Apr 10, 2013 at 5:18

2 Answers 2

2

If a value is NULL, then adding it to a string will produce a NULL. This allows us to add the quotes in the ISNULL check and just produce NULL in the true value of the check, producing the correct syntax for nulls or not nulls as necessary.

select 'Insert into Organizations(Name, IndustryId, ContactPerson, Email, Website, LocationId, ContactNumber, Mobilenumber) values(' + 
       IsNull(''''+Nameofthecompany+'''', 'NULL') + ', ' + 
       Isnull(''''+IndustryType+'''', 'NULL') + ', ' +
       Isnull(''''+Nameofthepersonresponsibleforrecruitment+'''', 'NULL') + ', ' +
       Isnull(''''+EmailId+'''', 'NULL') + ', ' +
       Isnull(''''+websiteaddress+'''', 'NULL') + ', ' +
       Isnull(''''+Location+'''', 'NULL') + ', ' +
       Isnull(PhoneNumber, 'NULL') + ', ' +
       Isnull(MobileNumber, 'NULL') + ')' 
from Organization
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to use NULL (as a literal - not a string) for your NULL values, then the creation of the INSERT statement gets a lot more complicated; if the value is NULL, then you need to add the literal NULL without a leading and trailing '.

For each column where you want to do this, you'll need to use a CASE statement - something like this:

select 'INSERT INTO Organizations(.....) ' + 
       'VALUES(' + 
       CASE 
           WHEN NameOfTheCompany IS NOT NULL 
              THEN '''' + NameOfTheCompany + ''', '
              ELSE 'NULL, ' 
       END + 
       CASE 
           WHEN IndustryType IS NOT NULL 
              THEN '''' + IndustryType + ''', '
              ELSE 'NULL, ' 
       END +
       ..... and so on ......
       + ')'

... and so on, for each column you need this CASE statement ....

2 Comments

bizarrely this seems to be the only bit of information about how to insert null deliberately I could find online! Suppose it's just assumed knowledge
The solution with the case statements is useful where some of the column values are null and others contain data. If the entire column is null, then simply omit that column from the insert and the values will remain null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.