Insert Empty String into Int Column for SQL Server

How to insert empty string (' ') to decimal or numeric in SQL Server?

Based off your comments you just need a case statement 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 ...

Unable to insert empty integer value in Microsoft SQL Server

You have put '' as the value to insert into that column. You should be inserting NULL.

For example:

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) 
VALUES(7369,'SMITH','CLERK',7902,17-Dec-80,800,NULL,20)

Insert Empty string From Excel To SQL(int)

Please, try replacing

& Range("B" & i) &

with

& IIf(Range("B" & i).value = "", "'Null'", Range("B" & i).value) &

Put blank in sql server column with integer datatype

You can put the CASE expression into the query, and replace STR with a CAST, like this:

SELECT
CASE WHEN position < 0 THEN '' ELSE CAST(column1 as VARCHAR(10)) END as position
, column2
FROM myTable

Inserting empty values as string to sql table

I think I understand what is happening. Your variable NumberOfPackage contains numbers but since you want to insert NULL sometime, you made it a string.

NumberOfPackage = "23"
NumberOfPackage = "NULL"

"INSERT INTO table (column) VALUES (" & NumberOfPackage & ");"

But since you use a string, you might be trying to do

"INSERT INTO table (column) VALUES ('" & NumberOfPackage & "');"

Which would cause the error on NULL since your column is a number and it is trying to do

"INSERT INTO table (column) VALUES ('NULL');"

Stick with my first example (without the ') if you really want to concatenate strings but everything would be much easier if you used parameters. By using parameters, it would be easier to keep NumberOfPackage as a decimal or a decimal? and do proper math with it.

Why does inserting empty string into date column produce 1900-01-01

A NULL date is NULL (unknown value). An empty string, on the other hand, evaluates to 0, which in SQL Server is implicitly an integer representing the number of days since 1900-01-01. You can reference this post.

So when we try to insert '' to a date type column, it is equals to insert 1900-01-01.



Related Topics



Leave a reply



Submit