I am new to PostgreSQL and trying the below two scenarios:
Select a column name which already has a forward slash(/) in database (Road/Highway)
Using case on same column along with index
Select Road/Highway,
case
when index(upcase(Road/Highway), 'xturn') > 0 then 2
else 0
end as preferred_road
from abc_data;
But I am getting syntax error near index and for slash it is only taking Road.
Generally / means "division", so your column name is non-standard, much like working with keyword column names, column names with special characters must be quoted with double quotes. Use "Road/Highway" when referring to the column.
Related
I have a table Department with 2 columns, DEPT_CODE the PK and DEPT_NAME and both have data types name. I inserted:
INSERT INTO public."Department"("DEPT_CODE", "DEPT_NAME")
VALUES ('CIS', 'Computer Info Systems');
then I want to delete it so I wrote:
DELETE FROM public."Department"
WHERE 'DEPT_CODE' = 'CIS';
This didn't delete the row (I used the delete script provided by pgadmin). If I don't use quote for the 'DEPT_CODE', it gave an error:
ERROR: column "dept_code" does not exist
LINE 2: WHERE DEPT_CODE = 'CIS';
Idk what to do. Every tutorial I see seemed to be able to delete just fine, why not mine? This is my first time using pgadmin-postgres.
As documented in the manual identifiers (column & table names) need to be enclosed in double quotes. Single quotes are only for string constants.
And because you used the dreaded double quotes for the table and column names when you created the table, you now need to use them always:
DELETE FROM public."Department"
WHERE "DEPT_CODE" = 'CIS';
^ ^
| column name | string constant
It's strongly recommended to avoid those dreaded quoted identifiers completely. So never use double quotes when you create tables, then you never need to use them when working with the tables
I have a Postgres query that returns a column that contains multiple language words. I want to get only the results those contains A-Z and 0-9 only. How can I get the result?
Select name from table;
This should do:
SELECT name FROM table WHERE name ~* '\A[A-Z0-9]*\Z';
If you only want uppercase letters (not clear from your question), then use the case sensitive regular expression operator:
SELECT name FROM table WHERE name ~ '\A[A-Z0-9]*\Z';
If you want at least one character, i.e. you don't want empty strings, change the * to +.
I'm trying to run the following query to get a list of table names that match a pattern. I have tables in my db that has names ends with T, but the following query doesn't work. It doesn't return me any table names. If I get rid of T, only leave % in the quotes, it gives me all the table names in the db.
select rdb$relation_name
from rdb$relations
where rdb$relation_name like '%T';
The problem is that the datatype of RDB$RELATION_NAME is CHAR(31) (CHAR(63) in Firebird 4), which means it is padded with spaces. Comparisons with LIKE do not ignore trailing spaces, contrary to equality comparison which does ignore trailing spaces.
For correct comparisons you can TRIM the trailing spaces from the value:
where trim(trailing from rdb$relation_name) like '%T'
or use a SQL regular expression with SIMILAR TO:
where rdb$relation_name similar to '%T *'
Which is similar to the like, but specifies it is followed by zero or more spaces.
I want to select a column value and trim away a suffix matching a regular expression, all inside PostgreSQL, how can this be done?
-- somewhat like performing s/_bar$// on "foo_bar"
select regexp_replace('foo_bar', '_bar$', '')
Simple question, is there any way to omit the double quote in PostgreSQL?
Here is an example, giving select * from A;, I will retrieve ERROR: relation "a" does not exist, and I would have to give select * from "A"; to get the real result.
Is there any way not to do the second and instead do the first on PostgreSQL?
Your problem with this query started when you created your table. When you create your table, don't use quotes.
Use this:
CREATE TABLE a ( ... );
Not this:
CREATE TABLE "A" ( ... );
The latter will make it so that you always have to quote it later. The former makes it a normal name and you can use SELECT * FROM a; or SELECT * FROM A;
If you can't just recreate your table, use the ALTER TABLE syntax:
ALTER TABLE "A" RENAME TO a;
double quotes are required if you include capital letters in your table name in postgres
to avoid the requirements name your table "a"
Postgresql has some particular behaviour in regard to quoting and case sentivity: it folds every non-quoted identifier to lower case (also at creation time) and then works case-sensitively.
Double quotes in identifiers are only needed when the identifier (table name, column name, etc) was defined (at schema creation time) with uppercase letters (some or all) and between double quotes.
In that case (which I advice against), when you use that identifier, you must type it in the same way: case sensitively (type upper/lower case letter exactly as defined) and between double quotes.
In other cases, you can use non-quoted identifiers and work always case-insensitively.
Don't use upper case letter in your table name or it's column name, if you are using such thing then the postgres will required double quote for accessing it.
Please see the detailed description of what is happening here.
The PostgreSQL server table names are case-sensitive, but forced to be lower-case by default: when you type CREATE TABLE AAA, it will become CREATE TABLE aaa before the query execution.
Double-quoted names keep their case as it was, so after CREATE TABLE "AaA" you get the table AaA and have to write it double-quoted again and again.
Have no idea why did they do so :)