What is the right way to handle type string null values in SQL's Bulk Insert? - sql

For example, I have a column with type int.
The raw data source has integer values, but the null values, instead of being empty (''), is 'NIL'
How would I handle those values when trying to Bulk Insert into MSSQL?
My code is
create table test (nid INT);
bulk insert test from #FILEPATH with (format="CSV", firstrow=2);
the first 5 rows of my .csv file looks like
1
2
3
NIL
7

You can replace the nil with " (empty string) directly in your data source file or insert the data into a staging table and transform it:
BULK INSERT staging_sample_data
FROM '\\data\sample_data.dat';
INSERT INTO [sample_data]
SELECT NULLIF(ColA, 'nil'), NULLIF(ColB, 'nil'),...
Of course if your field is for example a numeric, the staging table should have a string field. Then, you can do as Larnu offers: 'TRY_CONVERT(INT, ColA)'.
*Note: if there are default constraints you may need to check how to keep nulls

Related

SQL String or binary data would be truncated. while inserting into table type parameter

I have a scenario where there is a table and i nned to pass table values param inside one of the stored procedure to peform certain actions . Basic table structure is as follows.
CREATE TABLE [dbo].[CitytTax] (
[CountryCode] [int] NOT NULL,
[TaxType] [varchar](255) NULL
)
As you see, Taxtype column is varchar type and takes upto 255 chars. However I will create on table type as below in part of application code and pass this table type as param to one of sp.
DECLARE #TaxDetails as [CitytTax]
Now i will insert some dummy values into it and pass this table type to one of the SP.
INSERT INTO #TaxDetails ([CountryCode],TaxType )
VALUES (6047,'Codfggtuioanoio charge to fund the liquidation of insurancevalues')
but getting error as below :
String or binary data would be truncated
The question here is table value type is having a column which is similar to actual database table. SO when i insert above script, it fails. However if i insert any value to [taxtype] which is less than 50 characters, it will insert successfully. but fails for more than 50 chars. IM wondering why it fails,it is supposed to take upto 255 characters right??
If your data length is indeed shorter than the field length. Then you're having having another table filled by a trigger on the main table, where the column size also had to be changed.
Also, replace all single quotes of your insert into query to double quotes and pass it into the stored procedure.
Well I figured out something which is a quick fix: just added below line before insert query:
SET ANSI_WARNINGS OFF .

Bulk Insert - How to tell SQLServer to insert empty-string and not null

This seems like a trivial question. And it is. But I have googled for over a day now, and still no answer:
I wish to do a bulk insert where for a column whose datatype is varchar(100), I wish to insert an empty string. Not Null but empty. For example for the table:
create table temp(columnName varchar(100))
I wish to insert an empty string as the value:
BULK INSERT sandbox..temp FROM
'file.txt' WITH ( FIELDTERMINATOR = '|#', ROWTERMINATOR = '|:' );
And the file contents would be row1|:row2|:|:|:. So it contains 4 rows where last two rows are intended to be empty string. But they get inserted as NULL.
This question is not the same as the duplicate marked question: In a column, I wish to have the capacity to insert both: NULL and also empty-string. The answer's provided does only one of them but not both.
Well instead of inserting empty string explicitly like this why not let your table column have a default value of empty string and in your bulk insert don't pass any values for those columns. Something like
create table temp(columnName varchar(100) default '')

How to insert into type text

I wish to insert into an SQL table in a field whose data type is text. However I am informed of an error saying ' check datatype' my Name field is of type nvarchar and my job field is of type text.
INSERT INTO Table1 (Name, Job) VALUES ('John', 'Clerk')
In MS SQL Server, you wont be able to insert string values(with more than 1 characters) in table if the column of type nvarchar. You can only insert only one character using nvarchar.
If you wish to insert some text, please specify the some size with nvarchar.
For example in your case:
Create table Table1(Name nvarchar(5), Job Text)
Insert into Table1(Name, Job) values ('John','Clerk')
This will work.
Hope it will help you out.

Insert empty string into INT column for SQL Server

A SAMPLE table has only one column ID of type int, default null.
In Oracle when I do:
insert into SAMPLE (ID) values ('');
the new record is added with blank value. But in SQL Server 2008, when I run the same insert statement, the new record has the value of 0.
Is there a way to force SQL Server 2008 to default blank string to NULL instead of 0 (for numerical type of columns)?
Assuming that your INSERT statement is part of a stored procedure re-used in many places of your application (or, perhaps, is a batch always constructed by the same part of the client code) and that the inserted value is a number passed as a string argument, you could modify the INSERT like this:
INSERT INTO SAMPLE (ID) VALUES (NULLIF(#argument, ''));
Use NULL instead.
insert into SAMPLE (ID) values (NULL);
How about another idea - define an INSTEAD OF INSERT Trigger.
Despite the fact that you're trying to insert a string, with this the operation is "intercepted", empty string is replaced by NULL, and the insert succeeds.
If you define this trigger on your table, then you can continue to insert empty string as before, with no other changes.
Edit: As Martin Smith points out, this effectively is a comparison to 0 (the equivalent of empty string as an int) meaning you won't be able to store 0 in this table. I leave this answer here in case that's acceptable to your situation - either that or re-do all your queries!
CREATE TRIGGER EmptyStringTrigger
ON [SAMPLE]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO [SAMPLE](ID)
SELECT CASE
WHEN ID = '' THEN NULL
ELSE ID
END
FROM inserted
END
SQL Fiddle example
You can't insert a 'string' into a int column. Oracle must be just handling that for you.
Just try inserting NULL if that's what you need.
insert into SAMPLE (ID) values (NULL);
One more option
insert into SAMPLE (ID) values (DEFAULT)

Passing default values to a column in Bulk insert

I am trying to get data from a csv file with the following data.
Station code;Priority vehicle;DateBegin;DateEnd
01;y;20100214;20100214
02;n;20100214;20100214
03;;20100214;20100214
Now I want a value 'n' in the table when no data is provided for the column 'Priority vehicle' in csv file.
I am writing the query as
BULK INSERT dbo.#tmp_station_details
FROM 'C:\station.csv'
WITH (
FIELDTERMINATOR ='';'',
FIRSTROW = 2,
ROWTERMINATOR = ''\n''
)
Check the full explanation here:
http://msdn.microsoft.com/en-us/library/ms187887.aspx
"By default, when data is imported into a table, the bcp command and BULK INSERT statement observe any defaults that are defined for the columns in the table. For example, if there is a null field in a data file, the default value for the column is loaded instead. "
My suggestion is to specify a default value for the Priority vehicle column and the Null value from the csv file will be overwritten to your SQL table with the default value specified in the table design.