I ran this query on a PostgreSQL table:
select * wkt from table where column <>'' and column is not null
..and unexpectedly received several rows with no visible value in that column. Why is this? Is there some 'hidden' value in that column for those rows, or a corrupted table, or something else?
t=# select ascii(chr(9));
ascii
-------
9
(1 row)
thus
select ascii(column) from table where column <>'' and column is not null
should give the idea
https://www.postgresql.org/docs/current/static/functions-string.html
ASCII code of the first character of the argument. For UTF8 returns
the Unicode code point of the character. For other multibyte
encodings, the argument must be an ASCII character.
Related
Long story short, lots of bogus data entered the SQLite database over time, where there is valid data before a null character in a varchar column - and then bogus data afterwards. I'm using \u0000 to represent the null character here.
validData\u0000bogusData
I need to clean the dataset so that the data after null character (and the null character) is gone. Unfortunately the data doesn't fit a pattern so only way is to look for the null character.
I've been trying to query the bad data so that I can clean it up.
SQL that I have tried:
SELECT id FROM table WHERE field LIKE "%" || CHAR(0) || "%"
Result: This returns all rows in the table. I need just the rows with null character in them.
String sql = "SELECT id FROM table WHERE field LIKE \"%\u0000%\"";
Result: SQLite stops processing SQL string when it encounters null char \u0000, Error: SQL syntax error near LIKE "%
Any help is appreciated, all I could find was info on getting rows with NULL value.
Use INSTR() to get the rows which contain CHAR(0):
SELECT * FROM tablename WHERE INSTR(field, CHAR(0));
and if you want you can update the table to remove the bogus data:
UPDATE tablename
SET field = SUBSTR(field, 1, INSTR(field, CHAR(0)) - 1)
WHERE INSTR(field, CHAR(0));
This question already has answers here:
What is the major difference between Varchar2 and char
(7 answers)
Closed 6 years ago.
I have a table named tab1 with a column as col1 and data type as varchar2(10) and another table named tab2 with a single column as col2 and data type as char(20)
with following data:
tab1 tab2
a a
b b
c c
when I run the following query
select tab1.*,tab2.*
from tab1 full join tab2
on tab1.col1 = tab2.col2;
I get the following output:
col1 col2
null a
null b
null c
a null
b null
c null
I know that char occupies fixed memory but should oracle not join on string comparison?
varchar2(10) occupied only required space.
char(20) will pad blank at end if the text is of smaller length.
Hence, in tab1 col1, value a is stored as a
but in tab2 col2, value a is stored as a
and hence no match.
Char is blank padded to its full width, so you are comparing
'a ' with 'a'
and they are not the same
Straight from the Oracle Documentation...
https://docs.oracle.com/database/122/SQLRF/Data-Type-Comparison-Rules.htm#SQLRF30027
Blank-Padded and Nonpadded Comparison Semantics
With blank-padded semantics, if the two values have different lengths,
then Oracle first adds blanks to the end of the shorter one so their
lengths are equal. Oracle then compares the values character by
character up to the first character that differs. The value with the
greater character in the first differing position is considered
greater. If two values have no differing characters, then they are
considered equal. This rule means that two values are equal if they
differ only in the number of trailing blanks. Oracle uses blank-padded
comparison semantics only when both values in the comparison are
either expressions of data type CHAR, NCHAR, text literals, or values
returned by the USER function.
With nonpadded semantics, Oracle compares two values character by
character up to the first character that differs. The value with the
greater character in that position is considered greater. If two
values of different length are identical up to the end of the shorter
one, then the longer value is considered greater. If two values of
equal length have no differing characters, then the values are
considered equal. Oracle uses nonpadded comparison semantics whenever
one or both values in the comparison have the data type VARCHAR2 or
NVARCHAR2.
The results of comparing two character values using different
comparison semantics may vary. The table that follows shows the
results of comparing five pairs of character values using each
comparison semantic. Usually, the results of blank-padded and
nonpadded comparisons are the same. The last comparison in the table
illustrates the differences between the blank-padded and nonpadded
comparison semantics.
Edit: After reading the other answers, you can know what is wrong. You can overcome it by using below syntax.
select tab1.*,tab2.*
from tab1 full join tab2
on trim(tab1.col1)=trim(tab2.col2);
I have a scenario where I have to convert varchar column into number column. While doing that I'm getting error invalid number. After debugging the values some has whitespaces and some other values entered as 56.678.90. Below is the query I tried to convert varchar into number,
select cast('45.56.78' as number) from dual or
select cast (' ' as number) from dual
Both the values which I have entered in the above query will be there under column 'lddfc' in table entry_header. Column lddfc has records as 456.99, 456.89.43, and whitespace. How can I display these values as number?
You haven't mentioned what variant of SQL you are using but if it's T-SQL you could remove leading and trailing spaces using LTRIM(RTRIM(yourValue)). Not sure about syntax for PL/SQL.
So your code would be select cast(LTRIM(RTRIM('45.56.78')) AS NUMBER) FROM DUAL
I don't think that you can convert '45.56.78' to a number though without removing one of the decimal points.
I designed a table with one field of type char(10).
I then I input data to this field but data is less than 10 characters.
Why is it that when I select the data, it returns the data appended with spaces to 10 characters?
char(n) is padding data with spaces ( completed to n)
I have a column in a table that is blank. The weird thing is that it does not appear to be null or an empty string. So, if I do this:
SELECT *
FROM TABLE
WHERE column IS NULL
...or:
SELECT *
FROM TABLE
WHERE column = ''
I get nothing. Thoughts?
Issue this query:
SELECT column, DUMP(column, 1016)
FROM table
It'll show the exact contents.
Related: Oracle does not allow empty strings; they're silently converted to NULL.
Maybe the column contains only spaces?
Did you try
select *
from table
where trim(column) is null
Oracle's got a basically permanent annoyance that empty strings are treated as null. However, are you sure that's an empty string? It could be an otherwise invisible character, such as a space or tab/linebreak/linefeed/etc... What does the string length show when you do select length(column) from table?
Try this:
SELECT *
FROM TABLE
WHERE TRIM(column) IS NULL
If your column is not VARCHAR2 but CHAR(N) then insertion of an empty string is padded. See what Tom Kyte tells about this