How do I deal with SQL tablenames with hyphen (-) when writing raw queries? i.e project-users - sql

I have a table called project-users and want to write a SQL query like SELECT * FROM project-users I get this error ERROR: syntax error at or near "-".
I cannot change the table name at this point.

According to http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html, you should use double quotes.
In your case, for PostgreSQL the query should be:
SELECT * FROM "project-users";
It is good practice to avoid the use of characters that need escaping or that contain spaces in identifiers.

Related

How to write the LIKE syntax where the search term includes a percentage (%)?

I am using SQL Server 2012 and I need to perform a search on a specific field, called Notes. The search criteria is to find all rows where the term 8% is mentioned in that specific field.
The WHERE clause of my T-SQL query looks like this:
WHERE [Notes] like '%[8%]%'
However, the query is not filtering correctly based on the above syntax. It is also including rows where the term 8 is mentioned.
I had a look at the answers proposed in the question below, but they are still not giving me the correct answer.
SQL 'LIKE' query using '%' where the search criteria contains '%'
A single character class represents a single character. So [%] means a literal percent symbol, and 8[%] means literal 8%. Try this:
SELECT * FROM yourTable WHERE [Notes] like '%8[%]%'
Demo
you need to escape % in query for example below
SELECT columns FROM table
WHERE column LIKE '%\%%' ESCAPE '\'
Using SQL Escape Sequences
Below is from MSDN
Pattern Matching with the ESCAPE Clause
You can search for character strings that include one or more of the special wildcard characters. For example, the discounts table in a customers database may store discount values that include a percent sign (%). To search for the percent sign as a character instead of as a wildcard character, the ESCAPE keyword and escape character must be provided. For example, a sample database contains a column named comment that contains the text 30%. To search for any rows that contain the string 30% anywhere in the comment column, specify a WHERE clause such as WHERE comment LIKE '%30!%%' ESCAPE '!'. If ESCAPE and the escape character are not specified, the Database Engine returns any rows with the string 30.
you can try below answer given by #TimBiegeleisen that is also easy way.
just change your where clause as
WHERE `Notes` LIKE '%8!%%' ESCAPE '!

Square bracket in table/column name is not supported?

Is square bracket in table name, column name or datatype is not supported in postgresql ?
I am getting below error while running the query in pgadmin:
CREATE TABLE [Test];
ERROR: syntax error at or near "["
SQL state: 42601
In PostgreSQL you can use double quotes :
CREATE TABLE "Test";
Same for columns, square brackets are used in SQL-Server.
If you mean the table name would be [Test] with brackets included then you would use "[Test]".
Create table "[Test]" ...;
If you meant it as an identifier, you could simply use without brackets or double quotes as Test.
Create table Test ...;
This way, you could refer to it as Test or test or tESt without double quotes in subsequent queries, ie:
select * from test;
If you use "Test" then postgreSQL would treat it as case sensitive and you would always use "Test".
Create table "Test" ...;
If you were trying to emphasize the identifier name by using square brackets, then sagi's answer is correct. On the other hand, if you really want to use square brackets in your table name, postgresql supports this as "[Test]". In this case your table name will include square brackets. You can get additional info from postgresql documentation

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 '!';

How to compare a varchar field having "(" character

If a field value in the table of SQL Server is like A(B) and if I to write a query
SELECT * FROM MyTable WHERE MyField = 'A(B)'
it is not returning any result. How to handle this situation?
Your query should work fine, if you want to specify a different escape parameter, you can use ESCAPE.
WHERE column LIKE '%A#(B#)%' ESCAPE '#'
Also, if you want to match anything that contains "A(B)", don't forget to surround it by percetages symbols.

Use like in T-SQl to search for words separated by an unknown number of spaces

I have this query:
select * from table where column like '%firstword[something]secondword[something]thirdword%'
What do I replace [something] with to match an unknown number of spaces?
Edited to add: % will not work as it matches any character, not just spaces.
Perhaps somewhat optimistically assuming "unknown number" includes zero.
select *
from table where
REPLACE(column_name,' ','') like '%firstwordsecondwordthirdword%'
The following may help: http://blogs.msdn.com/b/sqlclr/archive/2005/06/29/regex.aspx
as it describes using regular expressions in SQL queries in SQL Server 2005
I would definitely suggest cleaning the input data instead, but this example may work when you call it as a function from the SELECT statement. Note that this will potentially be very expensive.
http://www.bigresource.com/MS_SQL-Replacing-multiple-spaces-with-a-single-space-9llmmF81.html