select subset of column in IBM DB2 - sql

I am not being able to perform select query on a subset of columns of a database in IBM DB2.
select * from user
This works. But
select username from user
doesn't work. Here's the screenshot.

username is a reserved word. The "proper" solution would probably be to have a column name that isn't a reserved word, such as user_name. If changing the column name isn't an option, you could use double-quotes (") to escape it:
SELECT "username" FROM user

Related

Why do you only need double quotation marks in SQL for particular cases?

I have a column in my database table named UID. For some reason queries fail unless I surround the column name with double quotation marks (" "). None of the other columns require these quotation marks.
For example, this doesn't work:
SELECT user_name FROM user_table WHERE UID = '...'
But this does:
SELECT user_name FROM user_table WHERE "UID" = '...'
Is UID some kind of keyword? Why is it only happening to that column? How do I know if I need to use double quotes for other columns?
By the way, I'm running JDK 1.8_221 and using an oracle JDBC driver if that makes a difference.
Yes, it is about keywords. You can double quote everything (tables, columns) to avoid this but I can understand you don't want to do this.
To have a list of standard keywords: SQL Keywords
But you can see UID is not in this list as I assume it is a reserved keyword by your database implementation. I had the same problem with a table called "order" as it contains orders. ORDER is a keyword so I had to quote it each time.
So best is to test your statements using a SQL client tool.
Since you mention Oracle: Oracle keywords: "You can obtain a list of keywords by querying the V$RESERVED_WORDS data dictionary view."
If your create table command for user_table looks something like this:
create table user_table ("UID" varchar2(10))
then you will have to use quotes around UID in your query. This query:
select * from user_table where UID = 'somestring'
means to use the Oracle predefined UID pseudo column and your table's UID column will not be accessed.
If your table doesn't have a user-defined UID column, then using "UID" should fail.
My guess is your table does indeed have a UID column and when you say it "doesn't work" without using the quotes you probably mean it motivates an ORA-1722.
The type of failure, when using UID without quotes, depends on the content of the string 'somestring'. If the content of that string can be cast as a number then you probably won't get the rows you expect. If it cannot be cast as a number then you'll get an ORA-1722.
As an aside, if you try to execute this, then you'll get an ORA-904:
create table user_table (UID number)
Yes, it is keywords and return
UID returns an integer that uniquely identifies the session user (the
user who logged on).
By default, Oracle identifiers (table names, column names, etc.) are case-insensitive. You can make them case-sensitive by using quotes around them when creating them (eg: SELECT * FROM "My_Table" WHERE "my_field" = 1). SQL keywords (SELECT, WHERE, JOIN, etc.) are always case-insensitive.
You can use it for more information here.

SQL server select query with parenthesis

I created a table in sql server
called user,
but when i tried to query the table
it only works like this approch (with parenthesis):
Select * from [user]
when i try:
select * from user
it dosent seems to work 'incorrect synax near the keyword 'user'
why i need to add those parenthesis i know i need to add Parenthesis if there is a space between like colum name "first name"
why it is not working?
You can write SELECT statement without square brackets for database tables, columns. When you use square brackets, then you explicitly says to SQL Server engine to select data from table user:
Select * from [user]
But when you write select * from user, then SQL engine thinks not about table user, but about reserved keyword user. The correct query to see database user name looks like this:
select user

Asp Classic & Firbird Sql without quotation marks

I have a script in ASP Classic that uses a Firebird database. I would like to execute a query without "quotation marks"
Now I must write this:
SQL = "SELECT ""TArticoli"".""IDArticolo"",""TArticoli"".""Desc"" FROM ""TArticoli"";"
I would write this:
SQL = "SELECT TArticles.IDArticle, TArticles.Desc FROM TArticles;"
The first one is accepted the second not, how can I do this?
You can't. DESC is a reserved word in Firebird, so to be able to use it as a column name (or any other object name for that matter), you will need to enclose it in quotes.
A second problem is that you are currently using
SELECT "TArticoli"."IDArticolo","TArticoli"."Desc" FROM "TArticoli"
And this means both your table name and the column names are case sensitive, and in that case, quoting those object names is mandatory. Unquoted object names are case insensitive, but are mapped to object names in upper case. This means that select * from TArticoli will select from a table called TARTICOLI, while select * from "TArticoli", selects from a table called TArticoli.
So unless you are going to rename or recreate all your tables or columns, you will not be able to get rid of quotes. The only thing you can do to reduce the number of quotes, is by not prefixing the columns with the table names (in the query shown it isn't necessary), or otherwise use a case insensitive alias for the table, eg
SELECT "IDArticolo", "Desc" FROM "TArticoli"
or
SELECT a."IDArticolo", a."Desc" FROM "TArticoli" AS a

how to retrieve sql column includes special characters and alphabets

How to retrieve a column containing special characters including alphabets in SQL Query. i have a column like this 'abc%def'. i want to retrieve '%' based columns from that table.
Please help me in this regard.
Is abc%def the column name? or column value? Not sure what you are asking but if you mean your column name contains special character then you can escape them which would be different based on specific RDBMS you are using
SQL Server use []
select [abc%def] from tab
MySQL use backquote
select `abc%def` from tab
EDIT:
Try like below to fetch column value containing % character (Checked, it works in Ingres as well)
select * from tab where col like '%%%'
Others suggest that like '%%%' works in Ingres. So this is something special in Ingres. It does not work in other dbms.
In standard SQL you would have to declare an escape character. I think this should work in Ingres, too.
select * from mytable where str like '%!%%' escape '!';

resultset is making problems when column name is integer

I have a database which is storing temperature values so my database's column name are 100 150 200
Now the problem is when I trying to fetch values under column name 100 it shows error
My query looks like
Select 100 from user where name='me';
Then
rs.get string("100"); this line gives errors
Can anyone plz suggest me how to retrieve values under these type of column names??
The standard SQL way to escape column names is to enclose within double qoutes.
SELECT "100" from user where name='me';
This is supported in most of DBMS - Oracle, PostgreSQL, MySQL, MSSQL and SQlite.
Apart from it , there are databse specific ways to escape column names
SQL Server - Square brackets []
SELECT [100] from user where name='me';
MySQL - Backticks ``
SELECT `100` from user where name='me';
Note :
Double qoutes act as escaping reserved words in MSSQL when
QUOTED_IDENTIFIER is ON.
Double qoutes ac as escaping reserved words in MySQL if it is in ANSI
mode
So for MSSQL , do first
SET QUOTED_IDENTIFIER;
If your query use a any keyword or numerical column name then use [] for MS-SQL and `` for MySQL.
MS-SQL:
Select [100] from user where name='me';
My-SQL:
Select `100` from user where name='me';