Azure SQL character with return to line (the équivalent of \n in SQL) - string

I'm inserting data from Salesforce to Azure SQL incrementally using Python. In the source I have some columns that contains characters in multiple lines (with back to new line), as shown bellow:
Name
Familly_Name
Age
My python code by default generates a '\n' for this. And from my part, I have tried to replace it by CHAR(13) and CHAR(13)+CHAR(10) and CHAR(10) but none of them worked; example (bellow) on a simple select on Azure SQL:
select 'hi' + char(13) + 'there'
Output:
hi there
What I need is:
hi
there
Best regards,

CHAR(13) is a carriage return. Most applications will treat that as a new line, but you should really be using CHAR(13) + CHAR(10), which is a carriage return followed by a line break:
SELECT 'Hello' + CHAR(13) + CHAR(10) + 'Goodbye' AS WithNewLine;
Outputs the below:
WithNewLine
-----------
Hello
Goodbye
db<>fiddle

Related

Deleting a string between two carriage returns tsql

Very new to SQL so I appreciate your patience in advance.
I have a column in a table that stores a particular set of instructions; each instruction is encapsulated by a carriage return.
eg: char(13)+ #instruction1 + char(13)...
#Instruction1 is a string of variable length but I do know a certain part of the string eg: #instruction1 = some string + #knownstring + some string.
So we have char(13) + (some string + #knownstring + some string) +char(13).
I want to replace this entire line with ''.
Identifying it just using the #knownstring.
Is this possible?
Thanking you all again, I really appreciate your assistance
select replace(replace(column,#knownsting,''),char(13),'')
from table
where key=1235
Replaces only the #knownstring but I also need to replace the surrounding text between the two char(13)
You might try something along this:
DECLARE #KnownString VARCHAR(50)='Keep This'
DECLARE #YourString VARCHAR(MAX)='blah' + CHAR(13) + 'dummy keep this dummy more' + CHAR(13) + 'Something without the known part' + CHAR(13) + 'Again with Keep THIS';
SELECT STUFF(
(
SELECT CHAR(13) + CASE WHEN CHARINDEX(#KnownString,LineText)>0 THEN #KnownString ELSE LineText END
FROM (SELECT CAST('<x>' + REPLACE(#YourString,CHAR(13),'</x><x>') + '</x>' AS XML)) A(Casted)
CROSS APPLY Casted.nodes('/x') B(fragment)
OUTER APPLY (SELECT fragment.value('text()[1]','nvarchar(max)')) C(LineText)
FOR XML PATH(''),TYPE
).value('.','nvarchar(max)'),1,1,'');
The result
blah
Keep This
Something without the known part
Keep This
The idea
The string is transformed to XML by replacing the line breaks with XML tags. Now we can query all text lines separately, check them for the known string, do the needed manipulation, and finally reconcatenate all fragments using the XML-trick (together with STUFF to get rid of the leading CHAR(13)).
Remarks
Using v2016 I'd use the split-string approach with OPENJSON and starting with v2017 there is STRING_AGG() to make the reconcatenation easier.

How to replace Ctrl+M character from spark dataset using regexp_replace()?

Hi have a Spark dataset with one of its column having a Ctrl+M char present in the column's data, as a result that record is getting split into two records, and data corruption.
Even though I have added the code for handling regex newline \r\n, but I am not sure if this same code will be able to handle Ctrl+M, i.e. ^M:
filtered = filtered.selectExpr(convertListToSeq(colsList))
.withColumn(newCol, functions.when(filtered.col(column).notEqual("null"), functions.regexp_replace(filtered.col(column), "[\r\n]", " ")));
Will the code functions.regexp_replace(filtered.col(column), "<ascii for Ctrl+M>", " "); work ? ..I don't know the ascii value of Ctrl+M.

how to modify textfile using U-SQL

I have a large file of around 130MB containing 10 A characters in each line and \t at the end of 10th "A" character, I want to extract this text file and then change all A's to B's. Can any one help with its code snippet?
this is what I have wrote till now
USE DATABASE imodelanalytics;
#searchlog =
EXTRACT characters string
FROM "/iModelAnalytics/Samples/Data/dummy.txt"
USING Extractors.Text(delimiter: '\t', skipFirstNRows: 1);
#modify =
SELECT characters AS line
FROM #searchlog;
OUTPUT #modify
TO "/iModelAnalytics/Samples/Data/B.txt"
USING Outputters.Text();
I'm new to this, so any suggestions will be helpful ! Thanks
Assuming all of the field would be AAAAAAAAAA then you could write:
#modify = SELECT "BBBBBBBBBB" AS characters FROM #searchlog;
If only some are all As, then you would do it in the SELECT clause:
#modify =
SELECT (characters == "AAAAAAAAAA" ? "BBBBBBBBBB" : characters) AS characters
FROM #searchlog;
If there are other characters around the AAAAAAAAAA then you would use more of the C# string functions to find them and replace them in a similar pattern.

python3 replace ' in a string

I am trying to clean text strings containing any ' or &#39 (which includes an ; but if i add it here you will see just ' again. Because the the ANSI is also encoded by stackoverflow. The string content contains ' and when it does there is an error.
when i insert the string to my database i get this error:
psycopg2.ProgrammingError: syntax error at or near "s"
LINE 1: ...tment and has commenced a search for mr. whitnell's
the original string looks like this:
...a search for mr. whitnell&#39s...
To remove the ' and &#39 ; I use:
stripped_content = stringcontent.replace("'","")
stripped_content = stringcontent.replace("&#39 ;","")
any advice is welcome, best regards
When you try to replace("&#39 ;","") it literally searching for "&#39 ;" occurrences in string. You need to convert "&#39 ;" to its character equivalent. Try this:
s = "That's how we 'roll"
r = s.replace(chr(int('&#39'[2:])), "")
and with this chr(int('&#39'[2:])) you'll get ' character.
Output:
Thats how we roll
Note
If you try to run this s.replace(chr(int('&#39'[2:])), "") without saving your result in variable then your original string would not be affected.

text with redundant side chars formatting in emacs

I did lots of search without luck. I think even this is easy but it could help, so here it goes.
Here the goal is to format a kind of Java String to plain text.
For example, consider a String in java code,
logger.LogText( "Hi, this is 1st line " + "\n" +
"speak sth. in 2nd line " + "\n" +
"answered...? ");
and i want to copy from the whole String and paste to my plain text file, then run
M-x some-format-function-by-template-on-selection
and i got a result
Hi, this is 1st line
speak sth. in 2nd line
answered...?
Is there a built-in command for this?
It's not have to use template, but don't you think it's cool?
Currently i try to use 'align' to work around.
The built-in commands are the regexp functions :-)
(defun my-reduce-to-string (start end)
"Extract a quoted string from the selected region."
(interactive "r")
(let* ((text1 (replace-regexp-in-string ".*?\"\\([^\"]+\\)\"[^\"]*" "\\1"
(buffer-substring start end)))
(text (replace-regexp-in-string "\\\\n" "\n" text1)))
(delete-region start end)
(insert text)))
Note that this is a destructive function -- it replaces the text in the buffer as requested.

Resources