SQL Server insert number with commas - sql-server

I created a database table:
I run this query
INSERT INTO dbo.Stats (Date_of_Record, Rack_Code, Total_MB, Schools_MB, Percent_Schools, Central_MB, Percent_Central)
VALUES (CAST(GETDATE() AS DATE), '78Q425', 45, 297, 1, 361, 0, 12, 0)
and I get an error
Msg 110, Level 15, State 1, Line 1
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
However, this works, but why should I NOT insert number with commas?
INSERT INTO dbo.Stats (Date_of_Record, Rack_Code, Total_MB, Schools_MB, Percent_Schools, Central_MB, Percent_Central)
VALUES (CAST(GETDATE() AS DATE), '78Q425', 45297, 1361, 0, 12, 0)

Commas delimit values. By using commas, you are telling SQL Server to expect more columns of data. SQL Server is not white space sensitive, so white space is not used as a delimiter.

Related

how to insert random timestamp in snowflake timestamp field

I am trying the following SQL but I get an error:
CREATE TABLE slipstream( visit_timestamp time);
INSERT INTO slipstream values(dateadd(second, uniform(1, 10, random()), current_time()));
Error:
Invalid expression [DATE_ADDSECONDSTOTIME(CAST(UNIFORM(1, 10, RANDOM()) AS NUMBER(2,0)), '12:52:29.050000000')] in VALUES clause
Please advise
Using INSERT - SELECT pattern:
INSERT INTO slipstream (visit_timestamp)
SELECT dateadd(second, uniform(1, 10, random()), current_time());
Output:
The VALUES requires constants and allows for simple casting that the SQL parser can do, thus in the values line you can have '2022-06-21'::date and that will correctly cast to DATE.
SELECT column1, system$typeof(column1) FROM VALUES
('2022-06-21'::date),
('2022-05-21'::date);
gives:
COLUMN1
SYSTEM$TYPEOF(COLUMN1)
2022-06-21
DATE[SB4]
2022-05-21
DATE[SB4]
And thus for this workflow it would have been valid to use:
INSERT INTO slipstream VALUES
('2022-06-21'::timestamp),
('2022-05-21'::timestamp);
number of rows inserted
2
But complex function calls need to be executed SQL, thus as Lukasz mentioned the need for the INSERT/SELECT pattern.
Now you can be clever and put a VALUES on the SELECT to data drive the parameters to those functions (I was hoping to be clever as using the values to uniform, but those need to be constants, so settled for an offset)
CREATE TABLE slipstream( visit_timestamp time);
INSERT INTO slipstream
SELECT dateadd(second, uniform(1, 10, random() ) + column1, current_time() )
FROM VALUES
(0),
(-10),
(-20);
number of rows inserted
3
SELECT * FROM slipstream;
VISIT_TIMESTAMP
16:18:04
16:17:55
16:17:42

Why insert into select field1, field2 ... from table doesn't work with decimals?

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)

SQL Server 2014 inconsistent behavior when handling unicode

My goal is to enforce case-insensitive uniqueness on a column that may contain unicode characters on any unicode planes. But I encountered some inconsistent behavior when testing against SQL Server 2014(build 12.0.4213.0). Here is the test:
CREATE TABLE unicodetest1 (
id int,
/* this collation is supposed to support all supplementary unicode characters */
a nvarchar(10) COLLATE Latin1_General_100_CI_AS_SC
);
CREATE UNIQUE INDEX idx_a_1 ON unicodetest1(a);
I can insert many single unicode characters, both on BMPs and SMP (Plane 1):
INSERT INTO unicodetest1(id, a) VALUES(1, 't'); -- U+0074
INSERT INTO unicodetest1(id, a) VALUES (100, N'👸'); -- U+1F478
INSERT INTO unicodetest1(id, a) VALUES (101, N'👹'); -- U+1F479
INSERT INTO unicodetest1(id, a) VALUES (104, N'⭐'); -- U+2b50
SELECT id, a, len(a) FROM unicodetest1;
The length of the single character strings is 1, which is expected.
When I concatenate some unicode characters to ascii strings, it still works:
INSERT INTO unicodetest1(id, a) VALUES (111, N'test'); -- ok
INSERT INTO unicodetest1(id, a) VALUES (112, N'test👸'); -- ok
INSERT INTO unicodetest1(id, a) VALUES (113, N'test👹'); -- ok
So far so good.
But, when I insert N'test⭐', I get a duplicate key violation:
INSERT INTO unicodetest1(id, a) VALUES (114, N'test⭐'); -- dup
Msg 2601, Level 14, State 1, Line 21
Cannot insert duplicate key row in object 'dbo.unicodetest1' with unique index 'idx_a_1'. The duplicate key value is (test⭐).
The character "⭐" has a code point U+2b50, it is on BMP!
It seems that SQL Server thinks N'test⭐' and N'test' are identical!
My question is: what makes "⭐" special that it violates uniqueness constraint when it is concatenated to a string?
Thanks
James

SQL Insert Datetime temp table truncated error

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)

SQL Server Insert Command Error

INSERT INTO BORCODEME
( BORCODEME.IslemTarihi, BORCODEME.IslemAciklamasi,BORCODEME.IslemTutari)
VALUES(
(SELECT BORCLAR.BorcTarih,BORCLAR.BorcAciklama,BORCLAR.BorcTutari FROM BORCLAR WHERE BORCLAR.BorcMusteriID=6),
(SELECT ODEMELER.OdemeTarihi,ODEMELER.OdemeAciklama,ODEMELER.OdemeTutar FROM ODEMELER WHERE ODEMELER.OdemeMusteriID=6)
)
My SQL command is this, and I have these errors;
Msg 116, Level 16, State 1, Line 4
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Msg 116, Level 16, State 1, Line 6
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Msg 109, Level 15, State 1, Line 1
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
Not sure what you're really looking for - are you trying to insert the three columns from the two tables? Then write your INSERT like this:
INSERT INTO BORCODEME(IslemTarihi, IslemAciklamasi, IslemTutari)
SELECT
BORCLAR.BorcTarih, BORCLAR.BorcAciklama, BORCLAR.BorcTutari
FROM
BORCLAR
WHERE
BORCLAR.BorcMusteriID = 6
UNION
SELECT
ODEMELER.OdemeTarihi, ODEMELER.OdemeAciklama, ODEMELER.OdemeTutar
FROM
ODEMELER
WHERE
ODEMELER.OdemeMusteriID = 6
So this will insert the three values from BORCLAR and another row with the three values from ODEMELER.
If that's not what you want, then you need to explain in more detail what you really want instead.....
In general, you can either use this syntax:
INSERT INTO dbo.TargetTable (List-of-Columns)
VALUES (List-of-atomic-values)
or if you cannot provide atomic values (literals or T-SQL variables), then you can use
INSERT INTO dbo.TargetTable (List-of-Columns)
SELECT list-of-columns
FROM dbo.SourceTable
(but you cannot mix - you cannot have VALUES and then use SELECT inside of it)
In both cases, the number of columns in the INSERT statement must match exactly with the number of atomic values provided in VALUES or the number of columns selected by the SELECT statement

Resources