The final character for the string '<img src="data:image\' contains '\' - salesforce

The following code gives me an error because the final character for the first string contains '\'
How do I fix this?
myLog.EmailHTML__c.replaceAll('<img src="data:image\', '<img src="data:image/');

Instead of using single '\' use double '\'
myLog.EmailHTML__c.replaceAll('<img src="data:image\\', '<img src="data:image/');
Hopefully it will work!!

Related

changing type of column to array Postgresql

I am trying to convert text column to array[text] column in table i have column entry like
['Nikolai Rimsky-Korsakov', 'Jascha Heifetz', 'Arpárd Sándor']
but this is one string or text format I want to convert it into a real array of a string so that I can access a particular name in the above column.
I tried converting the type from this link by setting it to type to text[] but the column is just becoming one element of an array like this.
[ "['Nikolai Rimsky-Korsakov', 'Jascha Heifetz', 'Arpárd Sándor']" ]
But what I wanted is to type Array[text] for tat column to able to access particular names.
Use the function translate() to replace square brackets with curly ones and remove single-quotes:
translate(str, '[]''', '{}')::text[]
See the full example in Db<>fiddle.
Section 8.15.2. Array Value Input of PostgreSQL documentation describes the general look of array to be
'{ val1 delim val2 delim ... }'
So you need to trim your '[' and ']' characters and replace them with '{' and '}'.
Then you can cast to text array (text[]) and enjoy the results.
SELECT
replace(
replace(
'{'||trim(BOTH '[]' FROM test.sample)||'}',
'\',
'\\'
),
'"',
'\"'
)::text[] AS names
FROM
(
SELECT '[''Nikolai Rimsky-Korsakov'', ''Jascha Heifetz'', ''Arpárd Sándor'', ''Joe "Wingy" Manone'']'::text AS sample
) test
EDIT 2
To handle cases when there " and '' characters in your input we must escape it with \.
SELECT
replace(
replace(
'{'||trim(BOTH '[]' FROM test.sample)||'}',
'\',
'\\'
),
'"',
'\"'
)::text[] AS names
FROM
(
SELECT '[''Nikolai Rimsky-Korsakov'', ''Jascha Heifetz'', ''Arpárd Sándor'', ''Joe "Wingy" Manone'']'::text AS sample
) test
EDIT 3
To remove quotes from names:
SELECT
replace(
replace(
replace(
'{'||trim(BOTH '[]''' FROM test.sample)||'}',
'\', '\\' ),
'"', '\"'),
''', ''', ',')::text[] AS names
FROM
(
SELECT '[''Nikolai Rimsky-Korsakov'', ''Jascha Heifetz'', ''Arpárd Sándor'', ''Joe "Wingy" Manone'']'::text AS sample
) test
The problem solved by removing nested square bracket from my CSV file before populating table and that using translate function with little bit of change thank
alter artist type text[] using translate(artist, '[]"', '{}')::text[] ; ```

Find first instance of space in string

I am trying to return the first 'word' in a string by finding the first instance of a space ' ' in the string field Part_Comment. Examples of strings in the field Part_Comment are:
13088V21 () (FAB)
G16707 (FOLD) ()
16636U01.01
I have tried:
substring(Part_Comment from 1 for position(' ' in Part_Comment)-2) as "AssyNo",
which comes up with an error "Incorrect syntax near the keyword 'from'." But it works fine when I just use Part_Comment by itself.
substring(Part_Comment from 1) as "AssyNo",
Same error as above
left(Part_Comment,10) as "AssyNo",
This works, but I need to use the position function or something else to find the ' ' substring. But apparently the position function returns 0 when more than one instance occurs.
I imagine this is a pretty common thing that users want, so there must be an easy solution.
You can do it with LEFT and POSITION like so:
LEFT(Part_Comment, POSITION(' ' in Part_Comment)-1))
EDIT
as #Arioch 'The in comment suggested, safety need to be implemented
CASE POSITION(' ' in Part_Comment)
WHEN 0 THEN 'Part_Comment'
ELSE LEFT(Part_Comment, POSITION(' ' in Part_Comment)-1)
END

Illegal character sequence \p' in string literal

When executing the following code:
string query = 'select id from ClientData__c';
string Point05 = '%\\file-01\projects\Internal Audit\Internal Audit
Team\FY18\SOX\Testing%';
query += ' WHERE Point05__c LIKE \'' + Point05 + '\'';
List<ClientData__c> clientData = database.query(query);
I get the following error:
Line: 2, Column: 18
Illegal string literal: Invalid string literal '%\file-01\projects\Internal Audit\Internal Audit Team\FY18\SOX\Testing%'. Illegal character sequence \p' in string literal.
Backslash (\) is used normally as an escape character. Just like you used in query line to escape single quote (').
Here, in order to use in your string, you need to double backslash to escape the line backslash. Just change to:
string Point05 = '%\\\\file-01\\projects\\Internal Audit\\Internal Audit Team\\FY18\\SOX\\Testing%';
You may find its use here.

Include XML multi string in C program xCode

I'm wondering how to include a multi line string like an XML data inside a C string? I need to replace some text (MY_REPLACED_TEXT) inside the XML file.
So I include the xml like this:
char myXMLString =
'<?xml version="1.0" encoding="UTF-8"?>'
' <painting>'
' <img src="madonna.jpg" alt="Foligno Madonna, by Raphael"/>'
' <caption>MY_REPLACED_TEXT'
' <date>1511</date>-<date>1512</date>.</caption>'
'</painting>'
However, this doesnt seem to work (I get
"Expected ; after top level declarator" error
).
I've also tried escaping with \ at the end of the line, instead of the apostrophe ' but still not working (I get
"use of undeclared identifier" error
)
Is there a better way to include a string into a compiled C program? Can I include the XML as a file, which is local to the compiled program and search/replace that?
like this
char *myXMLString =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<painting>"
" <img src=\"madonna.jpg\" alt=\"Foligno Madonna, by Raphael\"/>"
" <caption>MY_REPLACED_TEXT"
" <date>1511</date>-<date>1512</date>.</caption>"
"</painting>";

Returning non-printable sybol (enter) to SQL Server from C# via CLR

I have the following CLR function:
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString PrintText(SqlString text)
{
// Put your code here
return new SqlString(text.Value);
}
And I want to get an enter symbol when passing '\r\n'
to it. But insteat I get '\r\n'
Could you please tell me what's wrong with this code.
Thank you.
In T-SQL you don't write a line break as \r\n. Instead you just use a line break:
'this
is a
string
in SQL
with
line breaks'
If you pass a string with \r\n to the C# code, nothing magical happens, it doesn't automatically get converted. The backslash character is just a character like any other. It's when you use the backslash in a literal string in the code that the compiler uses it as an escape code, and puts the control characters in the actual string.
You could always:
return new SqlString(text.Value.Replace("\\r\\n", "\r\n"));
From the SQL side, you could insert char(13) + char(10) into your T-SQL literals like so:
DECLARE #text varchar(100)
SET #test = 'this' + char(13) + char(10)
+ 'is a' + char(13) + char(10)
+ 'string' + char(13) + char(10)
+ 'in SQL' + char(13) + char(10)
+ 'with' + char(13) + char(10)
+ 'line breaks'
Though this works, it is much more verbose than Guffa's answer.
However, this technique can also be used to insert any character of the default code page into a string. The Function CHAR(int) accepts any integer between 0 and 255. Values outside that range cause it to return null. The function NCHAR(int) accepts values up to 65535 and inserts the corresponding unicode characters into a unicode string. Functions ASCII(char) and UNICODE(nchar) perform the inverse operations.

Resources