CQL Query on a column with a whitespace - sql

I wish to do a SELECT query but unfortunately, the database I am working on has a column with whitespace.
Example of query (partial) :
"WHERE My Column = value"
CQL says that
the column name 'My' doesn't exist in the database.
How can I make CQL understand that it has to look for the column "My Column"?
Thank you in advance.

you can execute query with following change
"WHERE [My Column] = value"
SQL will take care of blank space between column name. As for blank space column or table name can be assigned to database but you can use it with square bracket ([]).

I found what worked for me :
'WHERE "My Column" = value'

Related

Tag name is not right

Trying to learn SQL queries, I practice on the data explorer.
I try this query:
Select *
FROM Tags
WHERE TagName = "scala"
but have this error:
Invalid column name 'scala'
Any suggestion what is it going wrong?
use single quotes ('') instead of double quotes ("") for string constants ?
Tags must be a table of columns. Table which contain column TagName. And you are trying to find a record with 'scala' value in TagName column. But this column doesn't exist.
That all about the process. Now if you want some more help give us a context (examples).

ORACLE SQL "print"-keyword as column name

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

Hibernate - Raw Query execution_KEY Words Issue in query?

The setup consists of Hibernate 3. Am trying to execute the raw query as it is. The setup works fine for other simple queries , db inserts & updates.
The query in issue is :
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 29 [
SELECT keyMain, value FROM (select distinct K.[key] as keyMain,
( SELECT value FROM com.trans.dto.Resources as L WHERE L.[key] = K.[key]
and L.lang_code = 'A11' ) as value from com.trans.dto.Resources as K )
as test order by keyMain ]
Resources is the table & has mapping setup in hibernate.cfg.xml
I was under a thought "KEY" is name of one of the column which can not be changed. How do i escape key words ?
If not 1, then is the multi selects in sub query.
Please advise. Any suggestion is of great help.
From here:
You can force Hibernate to quote an identifier in the generated SQL
by enclosing the table or column name in backticks in the mapping document.
Hibernate will use the correct quotation style for the SQL Dialect.
This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.
So, try to escape your field with double quotes or with square parenthesis('[key]').

Rename a column with space in it

Someone mistakenly created a table in which all the column names has a leading space in it. For example: 'accountid' is now ' accountid'.
I am going to write a SQL statement to rename these columns. The one I wrote is:
ALTER TABLE mytable RENAME COLUMN ' accountid' TO 'accountid';
However, I got the following error:
Error : ERROR: syntax error at or near "' accountid'"
Can someone instruct me how to rename these? How to change my statement to make it runnable? I use PostgreSQL.
Many thanks.
In PostgreSQL, you use double-quotes for identifiers (if necessary): "
ALTER TABLE mytable RENAME COLUMN " accountid" TO "accountid";
See here and browse to 4.1.1
You can even put other characters:
select c.comment "actor.comment" from post p join comment c on p.id = c.post_id;

Removing [] around column in SQL Server 2005

when I was renaming the column in SQL Server, I accidentally inserted the square brackets around the column. The actual statement that I used was:
SP_RENAME 'customer.[EMPLOYEENAMES]', '[EMPLOYEENAME]', 'COLUMN'
But when I try to retrieve the data it just says and I even tried with out square brackets, it gives the same error
Invalid column name '[EMPLOYEENAME]'.
How should I remove the square brackets.
This will restore order in your database:
EXEC SP_RENAME 'customer."[EmployeeName]"', 'EmployeeName','COLUMN'
You cannot use double brackets because it returns syntax error. Quotes circumvent this limitation.
As you've now got a column with square brackets in the name itself, you can access that column by:
SELECT [[EmployeeName]]]
FROM Customer
Yes, all those extra square brackets are a bit unwieldy :)
So, I'd rename your column again to remove the brackets:
EXEC SP_RENAME 'customer.[[EmployeeName]]]', 'EmployeeName','COLUMN'
So you can then reference "normally":
SELECT EmployeeName
FROM Customer
None of the given alternatives worked for me, so I had to create a new column with the right name, do an update setting newName = [[badName]]], and then drop the wrongly named column.