SQL Server varchar(max) is not working properly [duplicate] - sql-server

This question already has answers here:
SQL Server reducing the length of the string to 8000 characters
(2 answers)
Closed 2 years ago.
In the following example, the txt column length is not correctly shown.
CREATE TABLE dbo.test
(
Id int IDENTITY,
txt varchar(max)
);
insert into test(txt) select REPLICATE('0',8000);
insert into test(txt) select REPLICATE('00',8000);
select id, len(txt) from test;
drop table test;
The result shown for both were 8000.
Could any body help?

If you want to insert something into a varchar(max) column - your inserting code needs to explicitly convert it to varchar(max) - try this:
insert into test(txt) select REPLICATE(cast('00' as varchar(max)), 8000);
That should give you a string of 16'000 characters length in the table.

Related

SQL Server- Get all column names in the table [duplicate]

This question already has answers here:
How can I get column names from a table in SQL Server?
(22 answers)
Closed 6 months ago.
I have a table name as 'tblOrder', having three columns. I would like to print the column names.
I want a script that should return a result as:
OrderId Name OrderTrackingNo
SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('tblOrder')
Try this

SQL Server FORMAT create varchar with very large lenght [duplicate]

This question already has answers here:
Why does VARCHAR need length specification?
(7 answers)
Closed 1 year ago.
It not really a problem but i am curious to know why.
I am using sql server and using FORMAT in a number returns a varchar with 4000 lenght. Why?
select format(1,'00')
This query returns "01"
Consider a query like
drop table if exists #t
create table #t(val int, fmt varchar(20))
insert into #t(val,fmt) values (1,'00')
insert into #t(val,fmt) values (1,'0000000')
insert into #t(val,fmt) values (1,'0000000000000000000')
select format(val,fmt) formatted from #t
What data type should formatted be? It can't change per row, so a single type must be chosen to hold all the values. And nvarchar(4000) is a reasonable choice, as it can hold any string with up to 4000 characters.

Concatenate multiple rows to one row in SQL Server [duplicate]

This question already has an answer here:
How FOR XML PATH('') works when concatenating rows
(1 answer)
Closed 4 years ago.
I have a SQL Server output which returns the following values with one column:
RepresentID
---------------
111122222
3333344444
5555566666
000000090909
7777788888
9999999999
121212131313
141414151515
Output :
RepresentID
---------------
111122222
3333344444
5555566666
000000090909
7777788888
9999999999
121212131313
141414151515
need to concatenate all the rows in to one row.
use xml path like below
SELECT STUFF((
SELECT
','+cast(representid as varchar(30))
FROM
table
FOR
XML PATH('')
),1,1,''
) AS comma_seperated_list;
On sql2017 , you can use string_aggregate like below
select string_agg(cast(representid as varchar(40)),',')
from table

Ms Sql Convert and insert between 2 databases

I have two databases.I insert data from DATABASE_2 TO DATABASE_1 however i need to convert some column.
I must convert Customer_Telephone_Number from varchar to bigint after that insert it.
So,
My Question is in below.
SET IDENTITY_INSERT DATABASE_1.dbo.CUSTOMER_TABLE ON
INSERT INTO DATABASE_1.dbo.CUSTOMER_TABLE
(
Customer_Id,
Customer_Telephone_Number
)
Select
Customer_Id,
Customer_Telephone_Number // This is varchar so i need to convert it to Big int.
from
DATABASE_2.DBO.CUSTOMER_TABLE
Any help will be appreciated.
Thanks.
If the data stored is without spaces or other non numeric symbols:
Select
Customer_Id,
CONVERT(BIGINT,Customer_Telephone_Number)
from
DATABASE_2.DBO.CUSTOMER_TABLE
For instance, if there is value with (222)-3333-333 it would fail. If the value was 2223333333 it would succeed

How to extract SQL Server image column data into readable format and store into another column?

I have a SQL Server database in which there are around 5 lac records in a table called message_mst, here is the table structure
Table name : message_mst
Columns:
message_id int
message_body image
I am not the person who built this database but whoever built this used image column to store all the message text which is simply plain text. But if we select the records, message_body prints all the text in HEX format. I want to convert it into readable format and then store into new field called message_body_readable.
How can I do that?
You can do it converting the field first to varbinary and than to varchar.
declare #t table (i image)
insert into #t values('some text')
select i, CAST(cast(i as varbinary(max)) as varchar(max))
from #t
Can you try this once..?
SELECT CONVERT(VARCHAR(1000), message_body, 0) FROM message_mst

Resources