I'm trying to execute this code:
DECLARE #temp TABLE (Start dateTime, Name nvarchar)
INSERT INTO #temp (Start, Name)
VALUES (GETDATE(), 'Callum')
SELECT * FROM #temp
and I get this error:
Msg 8152, Level 16, State 4, Line 3 String or binary data would be
truncated. The statement has been terminated.
(0 row(s) affected)
I found out that I'm trying to fit too much data in the column, but I'm not sure how....
You need to specify the size in nvarchar. For Eg nvarchar(50)
Related
I'm trying to load a CSV file into a table in order to sort the data. However, the smallmoney column BillingRate (e.g. $203.75) will not convert, with SQL Server producing the following message:
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 4 (BillingRate).
Msg 4864, Level 16, State 1, Line 10
Here is the code I am using in order to do this:
--CREATE TABLE SubData2
--(
--RecordID int,
--SubscriberID int,
--BillingMonth int,
--BillingRate smallmoney,
--Region varchar(255)
--);
BULK INSERT Subdata2
FROM 'C:\Folder\1-caseint.csv'
WITH
(FIRSTROW = 2,
FIELDTERMINATOR = '|', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
ERRORFILE = 'C:\Folder\CaseErrorRows4.csv',
TABLOCK);
A typical line of code in the CSV files looks like:
1|0000000001|1|$233.94|"West"
Apologies if there are any glaring errors here - I'm new to SQL Server :)
Many thanks in advance,
Tom.
This is odd. On a direct insert only the last fails.
declare #t table (sm smallmoney);
insert into #t
values ('$256.6')
insert into #t
values (244.8);
insert into #t
values ('12.8');
insert into #t
values ('$256.5'), ('244.7');
insert into #t
values ($256.5);
insert into #t
values ('$256.5'), (244.7), ('12.12');
select * from #t;
Give it a try removing the $ from the data.
When I try this on SQL Server 2014:
INSERT INTO B_G049
SELECT B049_Z001, B049_Z002, ObjectId1, ObjectId7, 43099 AS value
FROM B_G049
WHERE value = 42734;
with decimals columns, I get an error:
Msg 8115, Level 16, State 8, Line 56
Arithmetic overflow error converting int to data type numeric.
I have never noticed such problem with copy rows between the same structure tables (in this case same table is input and output) using
INSERT INTO table1
SELECT (list of columns)
FROM table1
WHERE (condition)
I have an excel file that I want to bulk insert into temp table:
create table #tmptable
(
Date varchar(10),
Receipt varchar(50),
Description varchar(100),
[Card Member] varchar(50),
[Account #] varchar(17),
Amount varchar(20)
)
bulk insert #tmptable
from 'C:\Transactions\example.xls'
with (FieldTerminator='\t', RowTerminator = '\n')
go
This is my excel file:
When executing bulk statement, getting the following error:
Msg 4863, Level 16, State 1, Line 1 Bulk load data conversion error
(truncation) for row 1, column 1 (Date). Msg 4864, Level 16, State 1,
Line 1 Bulk load data conversion error (type mismatch or invalid
character for the specified codepage) for row 2, column 1 (Date).
Do not know why it happens.
Well, you are actually reading your headers, meaning the the data on the first few rows of your xls are images that's why you are getting a type mismatch error
get the row number of that first row where the data actually is.
then you use this:
create table #tmptable
(
Date date,
Receipt varchar(50),
Description varchar(100),
[Card Member] varchar(50),
[Account #] varchar(17),
Amount varchar(20)
)
bulk insert #tmptable
from 'C:\Transactions\example.xls'
with (FieldTerminator='\t', RowTerminator = '\n', FirstRow = X)
go
where X is the row number where the data actually starts and not the headers
I'm writing data on the Sql Server database.
When data is written, a trigger runs.
TRIGGER
ALTER TRIGGER Alert ON records AFTER INSERT AS
BEGIN
DECLARE #tempmin decimal = 0
DECLARE #current_max_idAlarm int = (SELECT MAX(IdAlarm) FROM alarms)
DECLARE #maxidAlarm int
DECLARE #temp decimal = (SELECT s.lim_inf_temp from sensores s JOIN inserted i ON s.idSensor=i.idSensor )
-- Insert into alares from the inserted rows if temperature less than tempmin
SET IDENTITY_INSERT alarmes On
INSERT alarmes (IdAlarm, desc_alarm,date, idRecord)
SELECT
ROW_NUMBER() OVER (ORDER BY i.idRecord) + #current_max_idAlarm, 'Temp Error', GETDATE(), i.idRecord
FROM
inserted AS i
WHERE
i.Temperature < #temp
end
INSERT
insert into record values ('2014-05-26' ,'14:51:47','Sensor01','---','48.6','9.8');
Whenever I try to record this type of data: '---'
Gives the following error:
Msg 8114, Level 16, State 5, Procedure Alert, Line
Error converting data type varchar to numeric.
I know it is to be in decimal type (DECLARE #temp decimal - TRIGGER), but moving to the type varchar to trigger stops working correctly.
Does someone can help me resolve this error please?
Thank you all.
You are trying to insert --- inside a numeric column, you simply can't do that.
You have mainly 2 options:
Change the data type of the destination column
Choose a different value to insert (like NULL)
Given the following example:
declare #t1 AS VARCHAR(10)
SET #t1 = 'asdf'
SELECT ISNULL(#t1, 0) AS test1
Now if #t1 was NULL, the call to ISNULL would return 0 which is an INT.
So this means you have 2 potential data types being return for the same column correct?
This will work fine Sql server does am implicit Conversion and converts 0 into string '0' because of the datatype of the column being returned.
DECLARE #Test_TABLE TABLE(Column1 VARCHAR(10))
INSERT INTO #Test_TABLE VALUES
('ALPHA'),(NULL),('Beta'),('Gemma')
SELECT ISNULL(Column1, 0)
FROM #Test_TABLE
This will throw an error as when sql server tries to convert to the datatype of the column which is INT it fails and it throws an error
DECLARE #Test_TABLE1 TABLE(Column1 INT)
INSERT INTO #Test_TABLE1 VALUES
(1),(NULL),(2),(3)
SELECT ISNULL(Column1, 'Is null')
FROM #Test_TABLE1
Msg 245, Level 16, State 1, Line 5 Conversion failed when converting
the varchar value 'Is null' to data type int.
Your can see one datatype takes precedence on other and only One data type is returned not two :)