Error Inserting values in Dreamer Table - sql

I'm sorry if this is a simple question but I'm rather new to SQL and I'm having an issue trying to insert some values in a table.
The query I'm executing is the following:
INSERT INTO Dreamer VALUES (
'', 'Dreamer name', '0', '1554542121', 'pablogardiazabal#gmail.com',
'Dreamer FB', 'Dreamer TW', 'Dreamer', 'M', '', 0, 0, 'Dreamer DAD',
'Dreamer MOM', '0', '0', '151515131321', '545343512123',
'DreamerDAD#daddreamer.com', 'DreamerMOM#momdreamer.com',
'Dreamer DAD DIR', 'Dreamer MOM DIR', '1515312123123',
'5456453423', 0)
And the table is designed like this:
TABLE This is the Table
Thanks a lot guys and gals!
EDITTED (sorry about the wrong format)
The error I'm getting is the following
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.

There is mismatch in your order of values, It is better to provide your insert script along with column names and corresponding values as below:
INSERT INTO Dreamer (col1, col2...) VALUES (val1,val2...)

There is no shortcut to doing the dirty work of comparing every string value in your select list with the definition of the associated column. And you REALLY need to learn how to write literials of a given datatype. '' is not really a valid integer - if you want zero than use the correct literal. And the integer zero is not the same as NULL. Do not start using a bad habit of equating special values with NULL - it will eventually bite you.
The first problem I see is '151515131321' - which is to be assigned to the column DocNumberP : varchar(10). That is more than 10 characters.

Related

Unable to cast string to date when inserting on Databricks SQL

I have a table created like so :
CREATE TABLE IF NOT EXISTS MyDataBase.Table (
`date` DATE,
`name` STRING,
`isTarget` BOOLEAN
) USING DELTA LOCATION '/mnt/path/to/folder/'
I need to manually insert values in the table using SQL. I tried to do it like so :
INSERT INTO CalendrierBancaire.Data VALUES (
('2022-01-01', 'New Year', True)
)
But it fails on this error :
Error in SQL statement: AnalysisException: cannot resolve 'CAST(`col1` AS DATE)' due to data type mismatch: cannot cast struct<col1:string,col2:string,col3:boolean> to date; line 1 pos 0;
I also tried to replace the date string with :
CAST('2022-01-01' AS DATE)
to_date('2022-01-01', 'yyyy-MM-dd')
But neither worked and returned the same error. It looks like the SQL parser wants to convert the whole row to date which is stupid. Do you have any idea how I can do it ?
Thanks.
PS : In real usage I have almost 30 to 40 lines to insert so letting a variable with the CAST expression while be painful to realize for all values.
It looks like the problem is that you have additional brackets around values that you want to insert, so it's interpreted as a single column - you need to use following syntax (see docs):
INSERT INTO CalendrierBancaire.Data VALUES
('2022-01-01', 'New Year', True)
or if you have multiple rows to insert, then list them via comma:
INSERT INTO CalendrierBancaire.Data VALUES
('2022-01-01', 'New Year', True),
('2022-02-01', 'New Year 2', True)
P.S. Although it works for me just fine even with extra brackets

How do I build a SQL insert query that allows me to insert a blank into a numeric field?

I have some values from an Excel workbook that I'm adding into an array. Eventually, I loop through the array and build a sql insert query (using Excel VBA) with the values stored into the array.
I debugged the sql query string I built and it appears as this:
INSERT INTO RAPTOR_BG.Project_Attributes (
P-Key,
Contract_Currency,
FX_Rate,
Rate_Discount,
Study_Award,
BLD,
Submitted_Date
)
VALUES (
'1111111|7-22-2020',
'USD',
'0.91',
'',
'5-29-2019',
'',
'7-22-2020'
);
Both Rate_Discount and BLD are numeric and won't accept ' '. Is there a way to get sql to accept this blank as a null? I did some research and I saw that some people have had success using a case statement, but all of the examples I've seen use variables or something like this (#SomeName) found in the answer here.
I've tried adapting my code to fit the examples I've seen by doing the below, but it kicked an error stating that at least one of the result expressions in a CASE specification must be an expression other than the NULL constant.
*insert statement from before*
VALUES (
'1111111|7-22-2020',
'USD',
'0.91',
CASE WHEN '' is null then null end,
'5-29-2019',
'',
'7-22-2020'
);
I'm going to be running my code on several hundred workbooks and some of these values will be blank and others won't be so I can't necessarily hardcode a null instead of the ' '. Is there a way to work around getting the numeric fields to accept a blank as null?
Ok, I figured this one out. Please find the answer below.
INSERT INTO RAPTOR_BG.Project_Attributes (
P-Key,
Contract_Currency,
FX_Rate,
Rate_Discount,
Study_Award,
BLD,
Submitted_Date
)
VALUES (
'1111111|7-22-2020',
'USD',
'0.91',
CASE WHEN '' is null then 0 end,
'5-29-2019',
CASE WHEN '' is null then 0 end,
'7-22-2020'
);

can't insert numbers with multiple dots into nvarchar field

I am using a c# SqlCommand to insert a version number into the revVersion field of nvarchar(50). This is no problem for values like 1.13 or 1.6, but for value 1.18.1.12 i get a
System.Data.SqlClient.SqlException: 'Incorrect syntax near '.1'.'
This is the faulty command:
"INSERT INTO Revision (revVersion,year,month,day,numLinesAdded,numLinesRemoved,moduleName) VALUES (1.18.1.12,2015,10,29,7,5,'bp-bn-bmfilter-600.m1,v')"
Thank you for your ideas!
You are missing single quotes ' ' (1.18.1.12) & ',' ('bp-bn-bmfilter-600.m1,v') :
INSERT INTO Revision (revVersion, year, month, day, numLinesAdded, numLinesRemoved, moduleName)
VALUES ('1.18.1.12', 2015, 10, 29, 7, 5, 'bp-bn-bmfilter-600.m1', 'v');
you need to use quote '1.18.1.12' as it's datatype is nvarchar
INSERT INTO Revision (revVersion,year,month,day,numLinesAdded,numLinesRemoved,moduleName)
VALUES ('1.18.1.12',2015,10,29,7,5,'bp-bn-bmfilter-600.m1','v');
1.13 or 1.6 both are number but 1.18.1.12 this is not number thats why you got error
You have to change it string format like below
INSERT INTO Revision (revVersion,year,month,day,numLinesAdded,numLinesRemoved,moduleName)
VALUES ('1.18.1.12',2015,10,29,7,5,'bp-bn-bmfilter-600.m1','v');
missing single quote:
INSERT INTO Revision (revVersion,year,month,day,numLinesAdded,numLinesRemoved,moduleName) VALUES ('1.18.1.12',2015,10,29,7,5,'bp-bn-bmfilter-600.m1,v')

Error converting data type varchar to numeric. SQL Server INSERT statement

I am trying to insert some data into this table, but I get this error:
Msg 8114, Level 16, State 5, Line 3
Error converting data type varchar to numeric.
I tried to remove the quotes, but it is still showing me the same error:
USE CobornSalesDB;
GO
INSERT INTO SalesActivity
VALUES ('AC00001', '05-12-2016', 'AG16170', 'C000001', 'P0001', 'S00002'‌​, '1', '200000.00', '',‌ ​'1.2220', '20', '100000.00', '25-12-2016', '30-12-2016', '31-12-2016', 'A00‌​0001', 'PR00001');
GO
In all of your numeric columns, take the commas out of the values you are inserting.
This is your query taken from the comment.
You really should include it in the question as text, not as image.
INSERT INTO SalesActivity VALUES
('AC00001',
'05-12-2016',
'AG16170',
'C000001',
'P0001',
'S00002'‌​,
'1',
'200000.00',
'',‌ -- valueEUR
​'1.2220',
'20',
'100000.00',
'25-12-2016',
'30-12-2016',
'31-12-2016',
'A00‌​0001',
'PR00001');
In the definition of the table we can see that valueEUR column is numeric. You are passing a string there. Not just a string, but a string that can not be converted into a number. An empty string '' can't be converted into a number.
I'm guessing, you want to insert NULL in that field. So, you should
write NULL in the INSERT statement.
Also, you should remove all quotes around numbers, so that the server would not have to convert strings into numbers.
Also, you should write your dates in YYYY-MM-DD format. Otherwise, one day you may be surprised to see that server guessed it incorrectly and swapped month and day.
Also, you should list all column names in the INSERT statement. Otherwise your code will break when you add a new column to the table.
The query should look similar to this:
INSERT INTO dbo.SalesActivity
(Activity_ID,
[Date],
Quatation_Number,
Customer_ID,
Product_ID,
Status_ID,
Quantity,
valueGBR,
valueEUR,
Rate,
Commission,
weightedValue,
estDecisionDate,
currentEstCompletionDate,
originalEstCompletionDate,
Agent_ID,
Probability_ID)
VALUES
('AC00001',
'2016-12-05',
'AG16170',
'C000001',
'P0001',
'S00002'‌​,
1,
200000.00,
NULL,‌
​1.2220,
20,
100000.00,
'2016-12-25',
'2016-12-30',
'2016-12-31',
'A00‌​0001',
'PR00001');
Try entering date in mm/dd/yyyy format like this
INSERT INTO SalesActivity VALUES ('AC00001','12-05-2016','AG16170','C000001',
'P0001','S00002'‌​,1,200000.00,'',‌ ​1.2220, 20,100000.00,
'12-25-2016','12-30-2016','12-31-2016','A00‌​0001','PR00001');

Fetching a MySQL SET column as a list of integer offsets

I have a table in a MySQL database defined similar to the following:
CREATE TABLE doc_types (
id INT UNSIGNED PRIMARY KEY,
types SET('foo','bar','baz','quux',...) NOT NULL
)
I would like to get the value of the types column as a comma-separated list of 1-based integer offsets into that list of set values, e.g. "1,3,4" for a row whose value is "foo,baz,quux". (This format is required by another tool in the system I'm working on, and for various practical reasons I'm not able to modify it to accept something different.) The built-in EXPORT_SET function is not entirely dissimilar, but its return value would be in the form "1,0,1,1,...", essentially a 64-element bit set represented in string form.
Is there any good way to SELECT from doc_types and get the types values back in the offset format I described above, within a single SQL query?
Try this:
SELECT CONCAT_WS(',',
IF(types & 1 > 0, 1, NULL),
IF(types & 2 > 0, 2, NULL),
IF(types & 4 > 0, 3, NULL),
/* fill in rest here */
) as output
FROM doc_types;
Try this:
SELECT MAKE_SET(types+0, '1', '2', '3') as output FROM doc_types;