4

Is there a way to insert an empty string (' ') into a column of type decimal?

This is for the example :

create table #temp_table 
(
    id varchar(8) NULL,
    model varchar(20) NULL,
    Plandate date NULL,
    plan_qty decimal (18, 0) NULL,
    Remark varchar (50) NULL,
)

insert into #temp_table (id, model, Plandate, plan_qty, Remark)
values ('pn-01', 'model-01', '2017-04-01', '', 'Fresh And Manual')

I get an error

Error converting data type varchar to numeric.

My data is from an Excel file and has some blank cells.

My query read that as an empty string.

If there is a way, please help.

Thanks

2
  • What are you using to insert the data from an Excel file to your Table? Commented Aug 11, 2017 at 2:48
  • I use Bulk Copy to a temporary table and then insert the data to destination table. My query used by my developer in VB.Net and get error while compiling. but its already resolved. thanks for your concern. Commented Aug 11, 2017 at 6:19

3 Answers 3

3

Based off your comments you just need a case expression to your insert from Excel, which I assume is using open query or bulk insert.

Insert into #temp_table (id, model, Plandate, plan_qty, Remark)
Select
...
Case when someColumn = '' then null else someColumn end
...
From
-- your Excel file via openquery or whatever ...

    
Sign up to request clarification or add additional context in comments.

Comments

1

You can't insert an empty string into a numeric field. You can insert NULL:

insert into #temp_table (id, model, Plandate, plan_qty, Remark)
    values ('112325', '10TPB220MC', '2017-04-01', NULL, 'Fresh And Manual');

Of course, the default default value is NULL, so you can also just leave it out:

insert into #temp_table (id, model, Plandate, Remark)
    values ('112325', '10TPB220MC', '2017-04-01', 'Fresh And Manual');

1 Comment

thank you, I have try this one and this is OK. but my data is from excel file and there is blank cell and my query read it as empty string.
0

As it was mentioned, could insert NULL instead of empty string so one more option is to use NULLIF. In your case:

insert into #temp_table (id, model, Plandate, plan_qty, Remark)
values ('pn-01', 'model-01', '2017-04-01', NULLIF(value_which_may_be_empty, ''), 'Fresh And Manual')

Related: Insert empty string into INT column for SQL Server

Comments

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.