I can't see leading/trailing whitespace in the following following SQL statement executed with psql:
select name from my_table;
Is there a pragmatic way to see leading/trailing whitespace?
One of option is to use format() function.
With given query case: select format( '"%s"', name ) from my_table;
PoC:
SELECT format( '"%s"', name )
FROM ( VALUES ( ' a ' ), ( ' b ' ) ) v(name);
format
--------
" a "
" b "
(2 rows)
Turn off "aligned mode" in psql: \a
\a
select * from my_table;
id|col1|col2
12|foo|bar
I'd append surrounding quotes:
select '"' || name || '"' from my_table;
If you don't mind substituting all whitespace characters whether or not they are leading/trailing, something like the following will do it:
SELECT REPLACE(REPLACE(REPLACE(REPLACE(txt, ' ', '_'),
E'\t', '\t'),
E'\r', '\r'),
E'\n', '\n') AS txt
FROM test;
This is using an underscore to mark the spaces but of course you are free to choose your own. See SQL fiddle demo.
If you strictly only want to show up the leading/trailing ones it will get more complex - but if this is really desired, something may be possible using regex_replace.
You can try this:
select name from my_table
where name like '% '
Related
How do I use an ORACLE REGEX function to remove all leading and trailing line break characters and spaces?
For example, assume I have the following string where refers to actual invisible carriage return line feed characters. Here's the input:
"
SELECT *
FROM
TABLE
"
And here's the desire output:
"SELECT *
FROM
TABLE"
This would do it if regex_replace() is a requirement:
select regexp_replace('
SELECT *
FROM
TABLE
', '^\s*|\s*$', '') as hello
from dual
See https://www.techonthenet.com/oracle/functions/regexp_replace.php for documentation.
A single regexp_replace is sufficient, eg.
select regexp_replace('
select frut
from prut
','^[[:space:]]*(.*[^[:space:]])[[:space:]]*$','\1',1,1,'mn') from dual;
results in
select frut
from prut
I have a record which has value with leading and space in a column.
eg: column value is ' D0019 '
I want to pass this particular column in where clause.
select * from my_table where my_column='D0019';
Since the value has space, it doesn't detect from the where clause.
How can I select the record even it has leading and trailing spaces in the value?
My DB is ORACLE
========================================
UPDATE :
I get value only when I try
select * from my_table where my_column like '%D0019%'
not even with ' %D0019% '
=============================================
UPDATE 2 :
SELECT my_column ,DUMP(my_column) FROM my_table WHERE my_column like '%D0019';
output is
" D0019" Typ=1 Len=6: 9,68,48,48,49,57
it's not the normal space you have to remove. It's Horizontal Tab character . (Ascii 9).
The below regexp would strip all the charcters from ASCII range 0-32 , which are associated with the white space symbols.
select * from my_table
WHERE
REGEXP_REPLACE(my_column,'['||chr(1)||'-'||chr(32)||']' ) = 'D0019';
More on ASCII table
select *
from my_table
where trim(my_column) = 'D0019';
Edit:
Based on the output of the dump() function your values not just contain (leading) spaces but also a tab character. the trim() function will only remove space characters, but not other whitespace.
In order to get rid of any whitespace at the beginning you will need to use regexp_replace()
select *
from my_table
where regexp_replace(my_column,'^(\s)+','') = 'D0019'
If you need to get rid of leading and trailing whitespace, the regex needs to be expanded:
select *
from my_table
where regexp_replace(my_column,'^(\s)+|(\s)+$','') = 'D0019'
SQLFiddle example: http://sqlfiddle.com/#!4/3326a/1
Seems as simple as (if I get it):
select * from TAB where REGEXP_LIKE (COL,'\s*D0019')
returns values such ' D0019', 'D0019', ' D0019', ' D0019' ...etc.
Try like this;
select * from my_table where my_column like '%D0019';
you can also use this if you want only spaces at start for the select:
select * from my_table where my_column like '% %D0019';
or
select * from my_table where my_column like ' %D0019';
I need a query that could remove unnecessary characters (a not-so-needed trailing comma as an example) from the string stored in my database table.
So that
EMAIL_ADD
abc#gmail.com,
abc#yahoo.com,def#example.org,
abs-def#ac.uk,
would update it into something like this:
EMAIL_ADD
abc#gmail.com
abc#yahoo.com,def#example.org
abs-def#ac.uk
Using TRIM() function with TRAILING option removes a specific unwanted character from end of string , in your case being a comma present at end.
UPDATE tableName
SET EMAIL_ADD = TRIM(TRAILING ',' FROM EMAIL_ADD)
See documentation here TRIM()
If you have a specific list of characters to filter out at the start and end use trim functions:
select ltrim(ltrim(rtrim(rtrim(email_add, ','), ' '), ','), ' ')
from tableX
Here I nested ltrim and rtrim to remove leading and trailing , and .
Or using trim:
select trim(trim(both ',' from email_add))
from tableX
if you only whant to remove the last character of a string you can use
update mytable set my_column = substr(my_column ,0,len(trim(my_column)-1) where mycolumn like '%,'
It is an untested example.
I have used REPLACE function in order to delete email addresses from hundreds of records. However, as it is known, the semicolon is the separator, usually between each email address and anther. The problem is, there are a lot of semicolons left randomly.
For example: the field:
123#hotmail.com;456#yahoo.com;789#gmail.com;xyz#msn.com
Let's say that after I deleted two email addresses, the field content became like:
;456#yahoo.com;789#gmail.com;
I need to clean these fields from these extra undesired semicolons to be like
456#yahoo.com;789#gmail.com
For double semicolons I have used REPLACE as well by replacing each ;; with ;
Is there anyway to delete any semicolon that is not preceded or following by any character?
If you only need to replace semicolons at the start or end of the string, using a regular expression with the anchor '^' (beginning of string) / '$' (end of string) should achieve what you want:
with v_data as (
select '123#hotmail.com;456#yahoo.com;789#gmail.com;xyz#msn.com' value
from dual union all
select ';456#yahoo.com;789#gmail.com;' value from dual
)
select
value,
regexp_replace(regexp_replace(value, '^;', ''), ';$', '') as normalized_value
from v_data
If you also need to replace stray semicolons from the middle of the string, you'll probably need regexes with lookahead/lookbehind.
You remove leading and trailing characters with TRIM:
select trim(both ';' from ';456#yahoo.com;;;789#gmail.com;') from dual;
To replace multiple characters with only one occurrence use REGEXP_REPLACE:
select regexp_replace(';456#yahoo.com;;;789#gmail.com;', ';+', ';') from dual;
Both methods combined:
select regexp_replace( trim(both ';' from ';456#yahoo.com;;;789#gmail.com;'), ';+', ';' ) from dual;
regular expression replace can help
select regexp_replace('123#hotmail.com;456#yahoo.com;;456#yahoo.com;;789#gmail.com',
'456#yahoo.com(;)+') as result from dual;
Output:
| RESULT |
|-------------------------------|
| 123#hotmail.com;789#gmail.com |
I designed a table with a column whose data contains \n character (as the separator, I used this instead of comma or anything else). It must save the \n characters OK because after loading the table into a DataTable object, I can split the values into arrays of string with the separator '\n' like this:
DataTable dt = LoadTable("myTableName");
DataRow dr = dt.Rows[0]; //suppose this row has the data with \n character.
string[] s = dr["myColumn"].ToString().Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);//This gives result as I expect, e.g an array of 2 or 3 strings depending on what I saved before.
That means '\n' does exist in my table column. But when I tried selecting only rows which contain \n character at myColumn, it gave no rows returned, like this:
--use charindex
SELECT * FROM MyTable WHERE CHARINDEX('\n',MyColumn,0) > 0
--use like
SELECT * FROM MyTable WHERE MyColumn LIKE '%\n%'
I wonder if my queries are wrong?
I've also tested with both '\r\n' and '\r' but the result was the same.
How can I detect if the rows contain '\n' character in my table? This is required to select the rows I want (this is by my design when choosing '\n' as the separator).
Thank you very much in advance!
Since \n is the ASCII linefeed character try this:
SELECT *
FROM MyTable
WHERE MyColumn LIKE '%' || X'0A' || '%'
Sorry this is just a guess; I don't use SQLite myself.
Maybe you should just be looking for carriage returns if you arent storing the "\n" literal in the field. Something like
SELECT *
FROM table
WHERE column LIKE '%
%'
or select * from table where column like '%'+char(13)+'%' or column like '%'+char(10)+'%'
(Not sure if char(13) and 10 work for SQLite
UPDATED: Just found someone's solution here They recommend to replace the carriage returns
So if you want to replace them and strip the returns, you could
update yourtable set yourCol = replace(yourcol, '
', ' ');
The following should do it for you
SELECT *
FROM your_table
WHERE your_column LIKE '%' + CHAR(10) + '%'
If you want to test for carriage return use CHAR(13) instead or combine them.
I've found a solution myself. There is few way (with some dedicated function) to convert ascii code to symbol in SQLite at the moment (CHAR function is not support and using '\n' or '\r' directly doesn't work). But we can convert using CAST function and passing in a Hex string (specified by append X or x before the string) in SQLite like this:
-- use CHARINDEX
SELECT * FROM MyTable WHERE CHARINDEX(CAST(x'0A' AS text),MyColumn,0) > 0
-- use LIKE
SELECT * FROM MyTable WHERE MyColumn LIKE '%' || CAST(x'0A' AS text) || '%'
The Hex string '0A' is equal to 10 in ascii code (\r). I've tried with '0D' (13 or '\n') but it won't work. Maybe the \n character is turned to \r after being saved in to SQLite table.
Hope this helps others! Thanks!