SQL truncating long string in insert statement - sql-server

I have a sql query which is generating from C# code. This sql query insert a record into table. But some times the value of one of the columns is very long (more than 4000) characters. And the datatype of that column into table is NVARCHAR(MAX).
But this query is not inserting the whole string into that column it's truncating the string.
So can anyone suggest me how I can insert long string into a table's column as nvarchar(max) can take upto 2GB of data.
INSERT INTO TABLE1 VALUES(1,'XYZ','ABCADF;LKAJSDFLKAJLK;LK2342J4K2J4;LKJA;SJDFSAJDKFAJDLK;FJASD;FLKJASDFJ.............................MORE THAN 4000 CHARACTERS')

I could not give exact answer to your question, but here is a concept from which you can insert values greater than 8000. Complete value is stored and it is not truncated in column by this way. you can cast the string as follows while inserting. Here is the concept.
Declare #value nvarchar(max)
set #value ='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABCDEF123456'
INSERT INTO table_name VALUES(Cast(#value As varchar(max)))

Related

storing and processing 128-bit values in SQL Server

I'm going to create a table with an specific column which stores 128-bit unsigned values.
I have below constraints:
I should query this column for previous duplicate values in shortest possible time (Note: this is not a uniqueness constraint).
I should insert values with low timing overhead.
Number of records may be around tens of millions (e.g. 90M).
I want to display the column value as hex.
I want to query the column value using hex input strings.
According to this answer, using binary[16] is suggested for storing 128-bit values (without considering about constraints).
So my questions is which data type can be suitable for my column data type? (Char(32), Binary(16), ...).
I'm currently using char(32) to store 128-bit value in hex representation and I want to know If I can improve my database performance or not? I guess since numbers are stored and processed as text values, it decreases the DB query performance and there might be data-types with better performance.
binary(16) is the most appropriate column data type for 128-bit unsigned values since SQL Server does not have a 128-bit unsigned type. This will reduce storage and memory requirements by half compared to char(32).
I should query this column for previous duplicate values in shortest
possible time (Note: this is not a uniqueness constraint).
Create an index on the column to avoid a full table scan.
I should insert values with low timing overhead.
There is a minor insert performance cost with the above index but will be more than offset by the runtime savings.
Number of records may be around tens of millions (e.g. 90M).
A 100M row table will only be a few GB (depending on row size) and will likely be memory resident on an adequate sized SQL instance if the table is used often.
I want to display the column value as hex.
As with all display formatting, this is a task best done in the presentation layer but can be done in T-SQL as well.
I want to query the column value using hex input strings.
Ideally, one should match the column data types when querying but a hex string can be explicitly converted to binary if needed.
Example T-SQL:
CREATE TABLE dbo.YourTable (
YourTableID int NOT NULL CONSTRAINT PK_YourTable PRIMARY KEY CLUSTERED
, BinaryValue binary(16) NOT NULL INDEX idx_BinaryValue NONCLUSTERED
, OtherData varchar(50) NOT NULL
);
INSERT INTO dbo.YourTable VALUES(1,0X000102030405060708090A0B0C0D0E0F,'example 1');
INSERT INTO dbo.YourTable VALUES(2,0X000102030405060708090A0B0C0D0E00,'example 2');
INSERT INTO dbo.YourTable VALUES(3,0X000102030405060708090A0B0C0D0E0F,'example 3 duplicate value');
--example query values
DECLARE #BinaryValue binary(16) = 0X000102030405060708090A0B0C0D0E0F;
DECLARE #CharValue char(32) = '000102030405060708090A0B0C0D0E0F';
--matching type query
SELECT YourTableID, BinaryValue, OtherData, CONVERT(char(16), BinaryValue, 2) AS DisplayValue
FROM dbo.YourTable
WHERE BinaryValue = #BinaryValue;
--query with hex string
SELECT YourTableID, BinaryValue, OtherData, CONVERT(char(16), BinaryValue, 2) AS DisplayValue
FROM dbo.YourTable
WHERE BinaryValue = CONVERT(binary(16), #CharValue, 2);
GO

How to store unicode characters in SQL Server?

In SQL Server, I am trying to create a table that can store unicode characters. Specifically this one
https://www.fileformat.info/info/unicode/char/0144/index.htm
However if I pick nvarchar as the column type, then store it and then select it, it shows as a regular n.
How can I get it to store properly?
This works fine
DECLARE #t TABLE
(
InputChar NVARCHAR(10)
)
INSERT INTO #t (InputChar)
VALUES (N'ń')
INSERT INTO #t (InputChar)
VALUES ('ń')
SELECT * FROM #t
Are you making sure that when you inserting your strings you are specifying that the string is unicode ? e.g.N'yourstring'

SQL Server changes characters in insert

My SQL Server changes same characters in the Strings.
Example:
insert into TEST_TABLE
values ('árvíztűrő tükörfúrógép2');
inserts
árvízturo tükörfúrógép
The TEST_TABLE has only one column with type varchar(50).
How can I insert strings into a table in SQL Server?
You will most likely need an nvarchar (rather than varchar) data type on the column that allows unicode characters.
You will probably need to specify it being unicode too by prefixing the value with an "N".
insert into TEST_TABLE
values (N'árvíztűrő tükörfúrógép2');
Change the column datatype to nvarchar and try this:
insert into TEST_TABLE
values (N'árvíztűrő tükörfúrógép2');
Run the following code on your table:
ALTER TABLE TEST_TABLE ALTER COLUMN *ColumnName* nvarchar(50)
After that, it works like a charm (even without prefixing the inserting string with N).

Alter Column: option to specify conversion function?

I have a column of type float that contains phone numbers - I'm aware that this is bad, so I want to convert the column from float to nvarchar(max), converting the data appropriately so as not to lose data.
The conversion can apparently be handled correctly using the STR function (suggested here), but I'm not sure how to go about changing the column type and performing the conversion without creating a temporary column. I don't want to use a temporary column because we are doing this automatically a bunch of times in future and don't want to encounter performance impact from page splits (suggested here)
In Postgres you can add a "USING" option to your ALTER COLUMN statement that specifies how to convert the existing data. I can't find anything like this for TSQL. Is there a way I can do this in place?
Postgres example:
...ALTER COLUMN <column> TYPE <type> USING <func>(<column>);
Rather than use a temporary column in your table, use a (temporary) column in a temporary table. In short:
Create temp table with PK of your table + column you want to change (in the correct data type, of course)
select data into temp table using your conversion method
Change data type in actual table
Update actual table from temp table values
If the table is large, I'd suggest doing this in batches. Of course, if the table isn't large, worrying about page splits is premature optimization since doing a complete rebuild of the table and its indexes after the conversion would be cheap. Another question is: why nvarchar(max)? The data is phone numbers. Last time I checked, phone numbers were fairly short (certainly less than the 2 Gb that nvarchar(max) can hold) and non-unicode. Do some domain modeling to figure out the appropriate data size and you'll thank me later. Lastly, why would you do this "automatically a bunch of times in future"? Why not have the correct data type and insert the right values?
In sqlSever:
CREATE TABLE dbo.Employee
(
EmployeeID INT IDENTITY (1,1) NOT NULL
,FirstName VARCHAR(50) NULL
,MiddleName VARCHAR(50) NULL
,LastName VARCHAR(50) NULL
,DateHired datetime NOT NULL
)
-- Change the datatype to support 100 characters and make NOT NULL
ALTER TABLE dbo.Employee
ALTER COLUMN FirstName VARCHAR(100) NOT NULL
-- Change datatype and allow NULLs for DateHired
ALTER TABLE dbo.Employee
ALTER COLUMN DateHired SMALLDATETIME NULL
-- Set SPARSE columns for Middle Name (sql server 2008 only)
ALTER TABLE dbo.Employee
ALTER COLUMN MiddleName VARCHAR(100) SPARSE NULL
http://sqlserverplanet.com/ddl/alter-table-alter-column

Changing Datatype from int to bigint for tables containing billions of rows

I have couple tables with millions, and in some table billions, of rows, with one column as int now I am changing to bigint. I tried changing datatype using SSMS and it failed after a couple of hours as transaction log full.
Another approach I took is to create a new column and started updating value from old column to new column in batches, by setting ROWCOUNT property to 100000, it works but it very slow and it claims full server memory. With this approach, it may take a couple of days to complete, and it won't be acceptable in production.
What is the fast\best way to change datatype? The source column is not identity column and duplicate, and null is allowed. The table has an index on other columns, shall disabling index will speed up the process? Will adding Begin Tran and Commit help?
I ran a test for the ALTER COLUMN that shows the actual time required to make the change. The results show that the ALTER COLUMN is not instantaneous, and the time required grows linearly.
RecordCt Elapsed Mcs
----------- -----------
10000 184019
100000 1814181
1000000 18410841
My recommendation would be to batch it as you suggested. Create a new column, and pre-populate the column over time using a combination of ROWCOUNT and WAITFOR.
Code your script so that the WAITFOR value is read from a table. That way you can modify the WAITFOR value on-the-fly as your production server starts to bog down. You can shorten the WAITFOR during off-peak hours. (You can even use DMVs to make your WAITFOR value automatic, but this is certainly more complex.)
This is a complex update that will require planning and a lot of babysitting.
Rob
Here is the ALTER COLUMN test code.
USE tempdb;
SET NOCOUNT ON;
GO
IF EXISTS (SELECT * FROM sys.tables WHERE [object_id] = OBJECT_ID('dbo.TestTable'))
DROP TABLE dbo.TestTable;
GO
CREATE TABLE dbo.TestTable (
ColID int IDENTITY,
ColTest int NULL,
ColGuid uniqueidentifier DEFAULT NEWSEQUENTIALID()
);
GO
INSERT INTO dbo.TestTable DEFAULT VALUES;
GO 10000
UPDATE dbo.TestTable SET ColTest = ColID;
GO
DECLARE #t1 time(7) = SYSDATETIME();
DECLARE #t2 time(7);
ALTER TABLE dbo.TestTable ALTER COLUMN ColTest bigint NULL;
SET #t2 = SYSDATETIME();
SELECT
MAX(ColID) AS RecordCt,
DATEDIFF(mcs, #t1, #t2) AS [Elapsed Mcs]
FROM dbo.TestTable;
a simple alter table <table> alter column <column> bigint null should take basically no time. there won't be any conversion issues or null checks - i don't see why this wouldn't be relatively instant
if you do it through the GUI, it'll probably try to create a temp table, drop the existing table, and create a new one - definitely don't do that
In SQL Server 2016+, this alter table <table> alter column <column> bigint null statement will be a simple metadata change (instant) if the table is fully compressed.
More info here from #Paul White:
https://sqlperformance.com/2020/04/database-design/new-metadata-column-changes-sql-server-2016
Compression must be enabled:
On all indexes and partitions, including the base heap or clustered index.
Either ROW or PAGE compression.
Indexes and partitions may use a mixture of these compression levels. The important thing is there are no uncompressed indexes or partitions.
Changing from NULL to NOT NULL is not allowed.
The following integer type changes are supported:
smallint to integer or bigint.
integer to bigint.
smallmoney to money (uses integer representation internally).
The following string and binary type changes are supported:
char(n) to char(m) or varchar(m)
nchar(n) to nchar(m) or nvarchar(m)
binary(n) to binary(m) or varbinary(m)
All of the above only for n < m and m != max
Collation changes are not allowed

Resources