SQL Text line break at random points - tsql

I have a nvarchar(max) field that has a text that should display as screen1.
But when I select the text and put that on a email body I get it like screen2. How can I format the string in select query to solve this?
When entering the data to the sql field in the table we have added a new line character after every line.

Related

How to update records in a table to trim whitespaces in JSON datatype of Postgres for a specific field

I have a requirement to update a table by TRIM whitespace for a specific column which is a JSON datatype.
In the screenshot is the data structure of the table and the column I have to update is SCOPE
Inside SCOPE I have to TRIM the field SITES and as example a screenshot of the testing table
In the above screenshots, I have to TRIM only sites using an update as I need to build a migration function which for every row will TRIM from SITES the white spaces.
I have no clue in JSONB type how to do it.
UPDATE screenshot of the leading whitespace in data
Use jsonb_array_elements_text on the sites, trim each value, then jsonb_aggregate them back to an array.
UPDATE data
SET scope = jsonb_set(scope, '{sites}', (
SELECT jsonb_agg(trim(site))
FROM jsonb_array_elements_text(scope->'sites') AS s(site)
));
(online demo)
Since Postgres 14, you can also use more comfortable subscripting to update json values, instead of jsonb_set:
UPDATE data
SET scope['sites'] = (
SELECT jsonb_agg(trim(site))
FROM jsonb_array_elements_text(scope['sites']) AS s(site)
);
(online demo)

search string and insert value in table

i have table having below records.The below product description are given by user in textbox in
front end(asp.net).The product description will come with single quotes or double quotes.i want to insert in the table.so how can we check whether single quotes or double quotes are exists in the
the input and insert value in the table.please help.
String s=Textbook
CREATE TABLE Product_Details(Product_Description varchar(50))
Required Output
Product_Description
-------------------
STORE('COVERED)
STEEL("ROOFED)
Insert statement will be differ for above two string?.
I'm not sure what exactly what you are looking for. You should be able to store text with quotes or double quotes without any trouble (note, I'm testing on Postgresql 9.4, don't have 9.2).
The problem is sometimes creating the text with the single quotes. In those cases it is common to have two single quotes like this insert into product_details values ('STORE(''COVERED)') The double quotes (") should not be a problem. You can use the syntax E'STORE(\'COVERED)' instead of the two quotes. Sometimes more readable.
If you just want to check if there are ' or "" in the input, this check is convenient:
select length(replace(product_description,'''',''))!=length(product_description),
length(replace(product_description,'"',''))!=length(product_description)
which return true/false columns telling if single-quote exists in string in first column and double-quote in the latter.
To delete the quotes in string you can do:
select replace(replace(product_description,'"',''),'''','')
Best regards,
Bjarni

Does a new line character affect Postgres LIKE text search?

So I have the following stored in a text column in Postgres...
This is a sentence.\nAnd here we start a new sentence.\nThen finally we have a third sentence.
Will those \n affect a LIKE query on that column?
i.e. SELECT "tracks".* FROM "tracks" WHERE (info LIKE '%sentence%')
The reason I ask is I want the line breaks for properly formatting the output of the text, but obviously don't want them screwing up search.
As long as you store line breaks as line breaks it should not mess up with your query.
CREATE TABLE example(
id serial PRIMARY KEY,
text TEXT NOT NULL
);
INSERT INTO example (text) VALUES (E'This is a sentence.\nAnd here we start a new sentence.\nThen finally we have a third sentence.')
SELECT * FROM example WHERE text LIKE '%nAnd%'; -- 0 records returned
SELECT * FROM example WHERE text LIKE '%And%'; -- 1 record returned
Please note the E before the string when I inserted it into the table.

Load period-separated text file into db2

I need to load an entire text file (contains only ASCII text) into the database (DB2 Express ed.). The table has only two columns EXAMPLE_TABLE (ID, TEXT). The ID column is PK, with auto generated data, whereas the text is VARCHAR(50).
Now I need to use the load/import utility to save each sentence in the text face into the EXAMPLE_TABLE, that is, we have a row for each sentence. The row-id should be auto generated, but that is already specified in table creation time. The import utility should consider the period '.' as delimiter (otherwise I don't know how to extract sentences).
How can this be done in DB2?
Thanks in advance!
When using delimited files, the standard DB2 import and load utilities do not have the ability to specify a row record terminator. The LF character (or CRLF on Windows) is the only record terminator you can use.
So, you would need to pre-process your file (to either replace each period (.) with a newline or insert a newline after each period) before you can use import or load, resulting in a file with each sentence on a separate line.
You can do this with tr:
cat file | tr '.' '\n' > file.load
db2 "import from file.load of del insert into example_table (text)"
Keep in mind that you will probably also need to account for spaces after the period, so you don't end up with leading spaces at the beginning of each "sentence" in your table, and you may also want to account for additional whitespace (i.e. empty lines between each paragraph).

How to removing spacing in SQL

I have data in DB2 then i want to insert that data to SQL.
The DB2 data that i had is like :
select char('AAA ') as test from Table_1
But then, when i select in SQL after doing insert, the data become like this.
select test from Table_1
result :
test
------
AAA
why Space character read into box character. How do I fix this so that the space character is read into.
Or is there a setting I need to change? or do I have to use a parameter?
I used AS400 and datastage.
Thank you.
Datastage appends pad characters so you know that there are spaces there. The pad character is 0x00 (NUL) by default and that's what you're seeing.
Research the APT_STRING_PADCHAR environment variable; you can set it to something else if you want.
The 0x00 characters are not actually in your database. The short answer is, you can safely ignore it.
When you said:
select char('AAA ') as test from Table_1
You were not actually showing any data from the table. Instead you were showing an expression casting a constant AAA as a character value, and giving that result column the name test which coincidentally seems to be the name of a column in the table, although that coincidence doesn't matter here.
Then your 2nd statement does show the contents of the database column.
select test from Table_1
Find out what the hexadecimal value actually is.