I was writing a demo code snippet in JAVA on database values as NULL
.
I wrote some simple expressions using NULL
in them and gave alias names for those expressions., but without the AS
key word. I am well aware that it is optional.
When I ran the SQL query at MySQL
command line and also using JAVA
the results were same.
But strangely, (though, it may not be for some of you), I found that on one of the expressions it did not use the defined alias name but it used entire expression along with the expression name, with a space char in between them. That space was a separator between the expression and alias name in the sql statement.
And when I used AS
keyword explicitly on the same expression, result was as expected.
SQL statement used:
select
length( null ) null_len -- to check if null data has any length
, length( '' ) empty_len -- to check length of an empty string
, null = '' 'n=e?' -- to check if null is equal to an empty string
, null = '' "n=e?" -- same as above but alias name within dbl quotes
, null = '' as 'n=e??' -- same with AS keyword on alias name used
, '' is null 'e=n?' -- to check if empty is equal to null
from dual;
Results on MySQL command line console:
+----------+-----------+------------------+------------------+-------+------+
| null_len | empty_len | null = '' 'n=e?' | null = '' "n=e?" | n=e?? | e=n? |
+----------+-----------+------------------+------------------+-------+------+
| NULL | 0 | NULL | NULL | NULL | 0 |
+----------+-----------+------------------+------------------+-------+------+
Results using Java with ResultSetMetaData
:
for( int i=1; i <= numberOfColumns; i++ ) {
String columnLabel = rsmd.getColumnLabel( i );
String columnName = rsmd.getColumnLabel( i );
//String columnType = rsmd.getColumnTypeName( i );
//String columnClassName = rsmd.getColumnClassName( i );
System.out.println( "Column Label: " + columnLabel );
System.out.println( "Column Name : " + columnLabel );
//System.out.println( "Column Type : " + columnType );
//System.out.println( "Column Class: " + columnClassName );
System.out.println();
} // for count of columns
Output:
Column Label: null_len
Column Name : null_len
Column Label: empty_len
Column Name : empty_len
Column Label: null='' 'n=e?'
Column Name : null='' 'n=e?'
Column Label: null='' "n=e?"
Column Name : null='' "n=e?"
Column Label: n=e??
Column Name : n=e??
Column Label: e=n?
Column Name : e=n?
Issue (May not be !)
Why not the alias name is recognized without AS
keyword?
Is that a known fact and issue?
Note: I have used JAVA, just to test and demonstrate the problem. Problem is not with behaviour of JAVA. And hence, answers need not be JAVA
specific.