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
Renaming a Column in Ms SQL Server 2005
Referencing a Calculated Column in the Where Clause SQL
Sqlite Get Name of Attached Databases
Are There Downsides to Using Prepared Statements
Creating Sumif Function in SQL Server 2012
Why Can't I Group by 1 When It's Ok to Order by 1
Using Variables for Asc and Desc in Order By
Disable SQL Cache Temporary in Rails
Rename Single Column in Select * in SQL, Select All But a Column
Passing Lists or Tuples as Arguments in Django Raw SQL
Create SQL Server Table Based on a User Defined Type
Conversion Failed When Converting from a Character String to Uniqueidentifier
Characters That Must Be Escaped in T-Sql
4 Byte Unsigned Int in SQL Server
H2 SQL Database - Insert If the Record Does Not Exist
Is There a Tool for Refactoring SQL, a Bit Like a Resharper for SQL
Removing Duplicates from a SQL Query (Not Just "Use Distinct")