SQL pad column content with tabs instead of spaces - sql

I am dynamically creating a column via a SELECT statement. The content of this column is a string which is then used as content for an e-mail.
The text looks like this:
text 1 | text 2 | text 3 | ...
text 123 | text 223 | text 323 | ...
as you see, the two lines are not aligned. The expected output should be like this:
text 1 | text 2 | text 3 | ...
text 123 | text 223 | text 323 | ...
The obvious solution is to pad each field in order for each field to have the same size. for this i used the following code:
LEFT(field1 + REPLICATE(' ', 20), 20)
My issue is, that in Outlook the text is still not shown properly aligned even though, if I copy the text into Notepad, it is properly aligned. The reason is, that Outlook uses Arial for displaying text-mails and in Arial blank-spaces do not have the same size as the other characters. Hence the padding does not yield the expected result.
My question now is: Is there some other way to use this padding approach in order to get same size displayed fields in Outlook Mails? Probably by padding with tabs? But I think my approach above then does not work anymore...

Regarding the TAB, it dawned on me that you were talking about trailing tabs
Declare #Table table (SomeField varchar(50))
Insert into #Table values
('Some Text'),
('Some Longer Text with more')
Select String = SomeField+replicate(char(9),(30-Len(SomeField))/4)+'|'
From #Table
Returns
Some Text |
Some Longer Text with more |

If you don't want (or can't) create the HTML as #techspider and #Shnugo mentioned, you could simply cast your string as a char(20)
cast(field1 as char(20))
For Example
Select '| '+cast('text1' as char(20))+' | '+cast('text1' as char(20))+'|'
Returns
| text1 | text1 |

Related

How to extract string between quotes, with a delimiter in Snowflake

I've got a bunch of fields which are double quoted with delimiters but for the life of me, I'm unable to get any regex to pull out what I need.
In short - the delimiters can be in any order and I just need the value that's between the double quotes after each delimiter. Some sample data is below, can anyone help with what regex might extract each value? I've tried
'delimiter_1=\\W+\\w+'
but I only seem to get the first word after the delimiter (unfortunately - they do have spaces in the value)
some content delimiter_1="some value" delimiter_2="some other value" delimiter_4="another value" delimiter_3="the last value"
The problem is returning a varying numbers of values from the regex function. For example, if you know that there will 4 delimiters, then you can use REGEXP_SUBSTR for each match, but if the text will have varying delimiters, this approach doesn't work.
I think the best solution is to write a function to parse the text:
create or replace function superparser( SRC varchar )
returns array
language javascript
as
$$
const regexp = /([^ =]*)="([^"]*)"/gm;
const array = [...SRC.matchAll(regexp)]
return array;
$$;
Then you can use LATERAL FLATTEN to process the returning values from the function:
select f.VALUE[1]::STRING key, f.VALUE[2]::STRING value
from values ('some content delimiter_1="some value" delimiter_2="some other value" delimiter_4="another value" delimiter_3="the last value"') tmp(x),
lateral flatten( superparser(x) ) f;
+-------------+------------------+
| KEY | VALUE |
+-------------+------------------+
| delimiter_1 | some value |
| delimiter_2 | some other value |
| delimiter_4 | another value |
| delimiter_3 | the last value |
+-------------+------------------+

Containstable is not working for special characters

I have a table, as an example Fruits, with column 'Name' which is Full Text Indexed
| ID | Name |
| 0 | *Apple |
| 1 | *Banana|
If we use full text search
select * from CONTAINSTABLE (Fruits, Name , '*')
This is ignored, as an escape character. While I have tried to use '/' and '%' to search but no use. Is there any option to search special characters?
I don't want to use LIKE Operator as it more time than Full text search.

How do I search "%" (percentage sign) in text using ILIKE operator in PostgreSQL [duplicate]

This question already has answers here:
How can you find a literal percent sign (%) in PostgreSQL using a LIKE query?
(3 answers)
Closed 2 years ago.
| id | text |
|----|-------|
| 1 | AB |
| 2 | CD%EF |
| 3 | GH |
I have a text column in a table having a value with a "%" sign.
I wanted to extract that value using the following query —
SELECT text FORM table
WHERE text ILIKE '%%%'
expected Output should be:
CD%EF
1 {row}
actual output returns:
AB
CD%EF
GH
3 {rows}
You can use \% to represent a literal percent sign:
SELECT text FORM table WHERE text ILIKE '%\%%'
Like the documentation says:
To match a literal underscore or percent sign without matching other characters, the respective character in pattern must be preceded by the escape character. The default escape character is the backslash but a different one can be selected by using the ESCAPE clause. To match the escape character itself, write two escape characters.

Selenium IDE: how to verify the stored text without some symbol

I store the text in the format "â„–XXX"
storeText | css=span.ng-binding | order_number
I need to verify this text in the other location using:
verifyText | //div[#id='myorders']/div/table/tbody/tr[1]/td | ${order_number}.
The problem is that in that location this text is displayed without the symbol "â„–" - "XXX" only.
How i can to verify the match of this text?
If the text in one location has the leading character, but not in the other, then after you save the string, strip off the first character and compare to that. You can use any number of methods to do that (see Delete first character of a string in Javascript).
Klendathu
I found the solution:
storeText | css=span.ng-binding | order_number
storeEval | storedVars['order_number'].replace('â„–','') | order_number

PostgreSQL String search for partial patterns removing exrtaneous characters

Looking for a simple SQL (PostgreSQL) regular expression or similar solution (maybe soundex) that will allow a flexible search. So that dashes, spaces and such are omitted during the search. As part of the search and only the raw characters are searched in the table.:
Currently using:
SELECT * FROM Productions WHERE part_no ~* '%search_term%'
If user types UTR-1 it fails to bring up UTR1 or UTR 1 stored in the database.
But the matches do not happen when a part_no has a dash and the user omits this character (or vice versa)
EXAMPLE search for part UTR-1 should find all matches below.
UTR1
UTR --1
UTR 1
any suggestions...
You may well find the offical, built-in (from 8.3 at least) fulltext search capabilities in postrgesql worth looking at:
http://www.postgresql.org/docs/8.3/static/textsearch.html
For example:
It is possible for the parser to produce overlapping tokens from the
same of text.
As an example, a hyphenated word will be reported both as the entire word
and as each component:
SELECT alias, description, token FROM ts_debug('foo-bar-beta1');
alias | description | token
-----------------+------------------------------------------+---------------
numhword | Hyphenated word, letters and digits | foo-bar-beta1
hword_asciipart | Hyphenated word part, all ASCII | foo
blank | Space symbols | -
hword_asciipart | Hyphenated word part, all ASCII | bar
blank | Space symbols | -
hword_numpart | Hyphenated word part, letters and digits | beta1
SELECT *
FROM Productions
WHERE REGEXP_REPLACE(part_no, '[^[:alnum:]]', '')
= REGEXP_REPLACE('UTR-1', '[^[:alnum:]]', '')
Create an index on REGEXP_REPLACE(part_no, '[^[:alnum:]]', '') for this to work fast.