Using (-) in fields - sql

I'm stuck on my application. I have named my table and fields name using ( - ) as glue instead of using ( _ ). Since i love the - method i was figuring out how to keep it.
So i fixed my sql queries's table as follow:
First:
SELECT * FROM cool-table
Then:
SELECT * FROM `cool-table`
And it worked. But now with the fields name i don't really know hot to make this work:
SELECT * FROM `cool-table` WHERE cool-id = 1
The error i'm getting is related to the - of cool-id.
Any help?
EDIT
I swear i've tried to put
`cool-id`
but it seemed not to work for me. Now it does. So sorry for my stupid question.

Do
SELECT * FROM `cool-table` WHERE `cool-id` = 1
Note the single ticks (called identifier quotes, at least by me) added to the column name.
PS. This is a really bad idea, but you probably know that already.

Have you tried:
SELECT * FROM `cool-table` WHERE `cool-id` = 1
I'm pretty sure this should work.

You should be able to use the backtick just like you would for the table name, like so:
SELECT * FROM `cool-table` WHERE `cool-id` = 1

Although MySQL allows hyphens in identifiers, standard SQL doesn't. By using hyphens, you pretty much guarantee that
a lot of applications simply won't work with your data,
you won't be able to move to another platform should you need to,
and anyone who uses your app will have to install MySQL.
Don't do it.

Related

Remove spaces from string and select using that string in sql server

In SQL Server, I am trying to select some of the records using a string which has space so I trim and use but something is wrong please correct me where something is missed by me.
SELECT * FROM projects where str_id=ltrim(rtrim(' artf130 ')) --- No rows selected
SELECT * FROM projects where str_id='artf130' -- one row selected
Update: I copied the first line from google spread sheet.
Maybe that was my bad. People keep helping.
I think my comment was enough, cause I linked to a very similar problem, which got a answer. So here for everyone:
You can see the answer to that question here.

PLSQL to SQL - DatabaseLink Select

straight forward question, but interesting enought, I didn't find anything. Probably I'm searching for the wrong keywords:
We have 2 Databases, one Oracle, one SQL, connected via Links.
I'd like to check, if some data is in the Oracle-Part, but not in the SQL one.
Selecting it from the PLSQLDev is pretty straightforward:
SELECT * from Core.Event#Link
But as soon as I try to select specific fields like :
SELECT Id from Core.Event#Link
It tells me the qualifier is invalid.
I tried all shennanigans like alias select:
SELECT ie.Id from Core.Event#Link ie
But it keeps telling me the qualifier is invalid.
Is there a special syntax I have to keep in mind?
Thanks in advance and a good weekend.
Matthias

How to shorten paths when accessing linked-server objects in SQL

Is there a way to shorted access paths in SQL? I am thinking something similar to alias's but I don't know how to ask this question in google to get the appropriate application of alias'ing
This:
select * from ServerName.DBName.dbo.TableName
To:
declare #RDB as RemoteDatabaseObject
set #RDB = ServerName.DBName.dbo
select * from #RDB.TableName
I know this doesnt work, but I want to know if there is a way to alias objects that have long paths.
Maybe you are looking for SYNONYM?
http://msdn.microsoft.com/en-us/library/ms177544.aspx
Aliasing works for what you're wanting here.
SELECT * FROM ServerName.DBName.dbo.TableName AS myAlias
The cool thing is that you can almost always use AS in the middle of sql statements.
http://www.w3schools.com/sql/sql_alias.asp
Other than using dynamic SQL, I dont think of any way which makes this possible.

Search in every column

I'm building an abstract gem. i need a sql query that looks like this
SELECT * FROM my_table WHERE * LIKE '%my_search%'
is that possible?
edit:
I don't care about querys performance because it's a feature function of a admin panel, which is used once a month. I also don't know what columns the table has because it's so abstract. Sure i could use some rails ActiveRecord functions to find all the columns but i hoped to avoid adding this logic and just using the *. It's going to be a gem, and i can't know what db is going to be used with it. Maybe there is a sexy rails function that helps me out here.
As I understand the question, basically you are trying to build a sql statement which should check for a condition across all columns in that table. A dirty hack, but this generates the required Sql.
condition_string = MyTable.column_names.join(' LIKE ? OR ')
MyTable.all(:conditions => [condition_string, '%my_search%'])
However, this is not tested. This might work.
* LIKE '...' isn't valid according to the SQL standards, and not supported by any RDBMS I'm aware of. You could try using a function like CONCAT to make the left argument of LIKE, though performance won't be good. As for SELECT *, it's generally something to be avoided.
No, SQL does not support that syntax.
To search all columns you need to use procedures or dynamic SQL. Here's another SO question which may help:
SQL: search for a string in every varchar column in a database
EDIT: Sorry, the question I linked to is looking for a field name, not the data, but it might help you write some dynamically SQL to build the query you need.
You didn't say which database you are using, as there might be a vendor specific solution.
Its only an Idea, but i think it worth testing!
It depends on your DB you can get all Columns of a table, in MSSQL for example you can use somethink like:
select name from syscolumns where id=object_id('Tablename')
Under Oracle guess its like:
select column_name from USER_TAB_COLUMNS where TABLE_NAME = 'Tablename'
and then you will have to go through these columns usign a procedure and maby a cursor so you can check for each Column if the data your searching for is in there:
if ((select count(*) from Tablename where Colname = 'searchingdata') > 0)
then keep the results in a separated table(ColnameWhereFound, RecNrWhereFound).
The matter of Datatye may be an Issue if you try to compare strings with numbers, but if you notice for instance under SQL-Server the syscolumns table contains a column called "usertype" which contains a number seems to refer to the Datatype stored in the Columne, like 2 means string and 7 means int, and 2 means smallint, guess Oracle would have something similar too.
Hope this helps.

How do I get all the columns of a table besides one

Suppose we have 20 columns in a table and I want to return 19 of them.
How can I do that ?
select *
will give me all of them but I want only 19.
Is there a good solution for that situation ? something like
select * - [columnName]
?!?
Nope, sorry. You can take *, or you can take them one at a time, but you can't take "all of them except for X, Y, or Z."
As has been said, you use SELECT * for all columns or list the columns individually if you don't want them all.
Listing columns does seem like a chore but there is an important reason why it's actually good.
While it's OK for ad hoc queries, it's highly recommended that use don't use SELECT * in code because when the database schema changes you will get different columns in the results returned to your application which is almost certainly not what you want. If you could do select * but address from customer this would have the same problem: changing the DB would change the structure of the results of your query which is bad.
So not only can you not do it, I would recommend not doing it even if you could.
You can explicitly name each column you wish to select. That is the only way to exclude columns.