ORACLE SQL "print"-keyword as column name - sql

I have a problem with my Oracle SQL query. It requires naming a column "PRINT" to override a value that was selected by selecting ' * '.
'TRUE' AS "PRINT"
The column should override the oririnal content of the "PRINT" colum that is included in
select *
Unfortunately, "PRINT" is recognised as a keyword (either by Oracle or my DBMS) and so in the resulting output table there is the original "PRINT" column, which is still 'FALSE', and a new column named "PRINT_1" which is the selected constant 'TRUE'.
As you can see, I already used "as" and double quotes in order to try to escape the keyword but somehow it's not working as I thought it would. So how do I do that?
As requested the query:
SELECT H.*
, 'TRUE' as "PRINT"
FROM TABLE H

The problem has nothing to do with print being a keyword (it isn't, by the way, though it is a SQL*Plus command). The problem is that adding an additional column to your projection (the set of columns in your SELECT list) will never "overwrite" another column in the projection even if you try to name them the same. If you want to force the value of PRINT to be 'TRUE', you'd need to explicitly list the columns that you want (other than PRINT) and then add your computed PRINT column.
In other words
SELECT h.col1, h.col2, h.col3, ... h.colN,
'TRUE' as print
FROM table_name h
where col1 - colN omits the PRINT column

Related

Snowflake tables with TO, FROM as column names

Looks like we've loaded some snowflake tables via ELT with "TO, FROM" as column names and they are both classic functions in any sql tool
Whenever I run a query for specifically those columns, there's always an error - how do I fix it apart from changing column names? Don't want to change column names as ELT process always happens from mongoDB via log based replication (stitch data)
select * - works perfectly , all other columns work too. Just "to" , "from" is the issue - should that never be used a columns?
select to, from from table limit 10 ; // tested [to, "to", 'to'] - none work
Error: SQL compilation error: error line 1 at position 7 invalid identifier '"to"'
Any ideas how to fix this apart from source column change or snowflake column changes?
Snowflake uses the standard double quotes to escape identifiers. However, when identifiers are escaped, the case of the letters matters, So, these are not the same:
select "to"
select "To"
select "TO"
You need to choose the one that is correct for your column names.
In addition spaces matter, so these are not the same:
select "to "
select " to"
select "to"
That is, what looks like to might be something else. You need to know what that is to escape the name properly.
If you can't figure them out, there is a trick to create a view to give the table reasonable names. Something like this:
create view v_t (to_date, from_date, . . .) as
select *
from t;
You need to be sure to include all the column names in the table in the column name list, in the same order as they are in the table. Then you can use the view with reasonable names.

SQL DB2 Replace value in a column

I have the following column, B represents boolean and the rest are empty values. I have to change all the values in this column to the word COLUMN A.
COLUMN
-----
B
I have tried different things, for example
SELECT COLUMN
FROM TABLE
WHERE COALESCE(NULLIF(COLUMN,''), 'COLUMN A');
And I receive the error: "Invalid character found in a character string argument of the function "BOOLEAN"." I'm kind of stuck to this question and I'm getting confused with this boolean value. I will be really happy if someone can help me, thanks!
The easiest thing is to use CASE expression. I am not familiar in db2, so you may want to research it further, but in other DBMSs it works like this:
SELECT CASE
WHEN COLUMN = '' THEN 'COLUMN A' -- if COLUMN = '', replace it with 'COLUMN A'
ELSE COLUMN -- otherwise, keep COLUMN as is.
END as 'COLUMN' -- name the column in the result 'COLUMN'
FROM TABLE
This is an article that explains how it works in db2:
https://www.ibm.com/support/knowledgecenter/en/SSEPEK_11.0.0/sqlref/src/tpc/db2z_caseexpression.html
The WHERE clause is unfinished. Compare the COALESCEd value to something:
SELECT COLUMN
FROM TABLE
WHERE COALESCE(NULLIF(COLUMN,''), 'COLUMN A') = 'COLUMN A';
Or better:
SELECT COLUMN
FROM TABLE
WHERE COLUMN IS NULL OR COLUMN = ''
Doesn't require any thinking/calculating to work out your selection logic. More maintainable, nicer for peer developers
*The above is generic advice for usual cases NOT involving boolean datatypes (which typically require some different treatment)
Now, you say you have to change the value to something. That requires an UPDATE statement. If this column is a boolean then it won't have a value of empty string. The blanks will be nulls:
UPDATE TABLE SET COLUMN = (some boolean) WHERE COLUMN IS NULL
If you don't want to permanently change the table data to something, but instead want to select it out as some value where a blank occurs, but keep the blanks stored in the table:
SELECT COALESCE(column, (some boolean)) FROM TABLE
Might be worth noting that not all versions of DB2 can return a boolean in a result set - this is quite typical of database vendors. Convert the boolean to something else representable using a case when, if your DB2 version is thus restricted
SELECT CASE WHEN column = TRUE THEN 'true' ELSE 'false' END FROM TABLE

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 restrict entire FTS5 Query to a single column?

Currently, I'm trying to execute an FTS5 query via libsqlite, and need to restrict the query to a specific column. In FTS4, this was possible by doing:
SELECT foo, bar FROM tableName WHERE columnName MATCH ?
and then binding the search string to the statement. However, with FTS5, the LHS of the MATCH operator must be the FTS table name itself, and the column name must be a part of the query:
SELECT foo, bar FROM tableName WHERE tableName MATCH 'columnName:' || ?.
This works when the binded string is a single phrase. However, consider the search text this is great. The query then becomes:
SELECT foo, bar FROM tableName WHERE tableName MATCH 'columnName:pizza is great';
Only pizza is restricted to to the columnName, but the rest of the phrase is matched against all columns.
How can I work around this?
The documentation says:
A single phrase … may be restricted to matching text within a specified column of the FTS table by prefixing it with the column name followed by a colon character.
So the column name applies only to a single phrase.
If you have three phrases, you need to specify the column name three times:
tableName MATCH 'columnName:pizza columnName:is columnName:great'

SQL insert avoid column names

In SQL insert, generally we specify the names of the columns in the SQL. Is there a way to generate that dynamically? basically if we specify the names of columns then tomorrow if a new col is added, there is a code change involved. How can i avoid this?
I am thinking of follg solution -
How about getting the names of the columns via
select column_name,* from information_schema.columns where table_name = '' order by ordinal_position;
& then create the INSERT statement with the columns? This way we do not specify the column names in the SQL...
Any thoughts?
You can only leave out the column names if you fill all values, but that would also break if a new column is added.
If you specify the names, you can add new columns as much as you want. They will automatically be assigned their default value if you leave your query as it is.
If you don't want them to have the default value, you need to edit the code anyway. Sure, you can dynamically generate the SQL and assign a default value, but that is what your RDBMS does anyway! I don't see your problem.
You can use a default value for the new column.
Assuming you're saying that you'll be getting the column values as input from somewhere else, then you could keep the column names in a config file like a properties file. Then if you're willing to assume that the ordering will always match up, just match up the input values to the column names from the file, and adding a column just means getting a new value to match the new column.
I solved this by getting the names of the columns via
select column_name,* from information_schema.columns where table_name = '' order by ordinal_position;
& then create the INSERT statement with the columns. By this I avoided specifying the column names in the query