Very short question. Can anyone say why this query
select LENGTH(' '::char || ' '::char), LENGTH(' '::text || ' '::char), LENGTH(' ' || ' '), LENGTH('a'::char || 'b'::char);
returns
0 1 2 2
Is space a special character witch don't concatenate with other strings?
Documentation says only this:
Unless otherwise noted, all of the functions listed below work
on all of these types, but be wary of potential effects of
automatic space-padding when using the character type.
Why I do this? Because i'm building string char by char in stored procedure, and when i try to concatenate varchar with char nothing happens.
The CHAR type is "fixed-length, blank padded". This means that if you store "foobar" into a char(10) field, Postgres actually stores "foobar " (that's four trailing spaces, SO does not preserve adjacent whitespace). When you fetch back your value, any trailing whitespace is stripped out. The same happens with ' '::char — its trailing whitespace is stripped, leaving only a zero-length string.
Related
I'm trying to match a certain text that includes a single quote (i.e. 'company's report...')
normally I would have used the E' literal + ' or double single quotes.
but when it gets to using the LIKE '%' operator, things got complicated.
what is the best approach to match a text with a single quote?
You can escape single quote with another single quote. Example:
WHERE column LIKE 'RSNboim''s'
From https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
To include a single-quote character within a string constant, write two adjacent single quotes, e.g., 'Dianne''s horse'. Note that this is not the same as a double-quote character (").
You can use Dollar-quoted String Constants at Lexical Structure
Your condition should be something like below;
select * from atable
where afield like $$Dianne's %$$
I load data from a text file and it seems that it does not contain leading space, however when I SELECT from a table, I see the leading space but cannot remove it with a LTRIM function:
SELECT ltrim(DATA) FROM MYTABLE WHERE LineNumber = 4
I'm getting the following:
T000000000004
with a single leading space before T
When I do select convert(varbinary,data) from mytable, that's what I get:
0x0A54303030303030303030303034
In the file it looks ok: T000000000004 - no leading space and it starts from the first character in the file. However, in the table it's inserted with a leading space.
How can I fix it?
As HABO mentioned, your value doesn't start with a space, it doesn't actually have any white space in it at all, it has a leading Line Feed (character 10, or 0X0A).
To remove these, and any carriage returns you might have too, you can use REPLACE:
REPLACE(REPLACE(data,CHAR(10),'')),CHAR(13),'')
(L/R)TRIM only remove leading/trailing white space. Nothing else.
If there could be a range of leading characters, and you want to remove all of them up to say the first alphanumerical character, you can use PATINDEX and STUFF:
SELECT STUFF(V.[data],1,PATINDEX('%[A-z1-9]%',V.[data])-1,'')
FROM (VALUES(CHAR(10) + CHAR(13) + ' -T000000129B'))V([data])
How does one escape characters in string literals in Avaloq scripts? I have been unable to find a definitive answer.
I am trying to include the new line character along with a quote in translation in the AMI
Escaping characters is the same as pl/sql, using '' (double single quotes)
And to access the return character, use util.rtn
Like this: '''it''s time''' || util.rtn || 'right?';
I have a long sql query that I am attempting to put into VBA for Excel. VBA for Excel has a limit to the amount of text that can go on a line and it seems to be about 1000 chars. What I want to do is copy the query to a text file and run it through a perl script and output to the text file with it formatted the way I need it for VBA.
I need Perl to count chars to 1000 then write (" & _) then a line break then (") then repeat the process till the end of the file. Spaces or type of char do not matter. Any help is greatly appreciated. I will check back frequently to see if anyone needs more information. THANKS!!!
A one-liner:
perl -lape 's/(.{1000})(?=.)/$1" &_\n"/g;' < input > output
If you have the text already in a string, one way to do this would be:
$string =~ s/(.{1000})/$1" & _\n"/sg;
print '"', $string, '"';
or, perhaps better:
my #chunks = unpack '(A1000)*', $string;
s/\"/""/g, s/\n/" & vbCrLf & _\n"/g for #chunks; # escape special characters
print '"', join(qq(" & _\n"), #chunks), '"';
If you're reading the input from a file, it's also possible to do this without reading all the input into memory, by setting the input record separator:
{
local $/ = \1000; # read input in chunks of 1000 chars
print '"';
while ( <> ) {
s/\"/""/g; s/\n/" & vbCrLf & _\n"/g; # escape special characters
print $_, qq(" & _\n");
}
print '"';
}
(Some of these methods can sometimes leave a pointless "" on the last line — in fact, the last method will always do that — but I assume that shouldn't be a problem.)
Finally, note that neither of these methods will do anything to escape any special characters (like double quotes) that might appear in the input. If your input might contain such characters, you'll need to deal with them separately. I've marked the points where you could insert code to do that, if you need to.
Edit: I did some Googling, and it looks like the main characters the need to be escaped in double quoted VBA strings are double quotes (which need to be doubled) and newlines (which have no simple encoding, and need to be handled with a kluge like " & vbCrLf & "). I've edited the code above to implement such escaping.
I suppose, if one wanted to be extra sure, one could also escape all non-printable and non-ASCII characters with something like:
s/([^\n -~])/sprintf '" & Chr(%d) & "', ord $1/eg
(Ps. The backslash in s/\"/""/g is there only to avoid confusing SO's syntax highlighter.)
Quoted from perldoc -f split:
As a special case, specifying a PATTERN of space (' ' ) will split on
white space just as split with no arguments does. Thus, split(' ') can
be used to emulate awk's default behavior, whereas split(/ /) will
give you as many initial null fields (empty string) as there are
leading spaces.
The above is all that's mentioned about how split deals with string delimiter, but what's the general case,is the empty leading fields always deleted for string delimiters?
No, only when the delimiter is a string that is a single space. In any other case, the delimiter is interpreted as a regex pattern.