This question already has answers here:
How to split a string value based on a delimiter in DB2
(12 answers)
Closed 2 years ago.
how to extract PAXG from the following string
0410|M|PAXG|20181114
in Db2 SQL
Try this for table BILL and its column COL1 with data.
SELECT
COL1
-- since 9.7
, xmlcast(xmlquery('fn:tokenize($s, "\|")[3]' passing BILL.COL1 as "s") as varchar(20)) as one
-- since 11.1
, REGEXP_SUBSTR(BILL.COL1 || '|', '([^\|]*)\|', 1, 3, '', 1) as two
FROM
(VALUES '0410|M|PAXG|20181114', '0410|M||20181114') BILL (COL1)
--BILL
;
If you are using Db2 11.1 or above then REGEXP_EXTRACT will work for you. E.g.
VALUES REGEXP_EXTRACT('0410|M|PAXG|20181114','([^\|]*)\|?', 1, 3, '', 1)
returns
1
------
PAXG
REGEXP_EXTRACT
Related
This question already has answers here:
Pass column name as parameter to PostgreSQL using psycopg2
(2 answers)
Closed 6 months ago.
I'm using psycopg2, not sqlalquemy. Basically, I can pass parameters to other portions of query, except for the column name. I'm trying to query some columns in order for a particular row, where the column names are enumerated in a list. The problem is the postgres query doesn't work for column names as strings. Any suggestions as to how to approach this problem?
cols = ['first', 'second', 'third']
query = """
SELECT %s FROM table_x
WHERE year=2021;
"""
for c in cols:
cur.execute(query, [c])
print(cur.fetchone()[0])
Using sql module from psycopg2. An example that I believe is more on point and cleaner then the answer posted in the comment.
import psycopg2
from psycopg2 import sql
con = psycopg2.connect(dbname="test", host='localhost', user='postgres', port=5432)
cols = ['first', 'second', 'third']
qry = sql.SQL("SELECT {} FROM table_x WHERE year=2021").format(sql.SQL(", ").join(map(sql.Identifier, cols)))
print(qry.as_string(con))
SELECT "first", "second", "third" FROM table_x WHERE year=2021
This question already has answers here:
Are PostgreSQL column names case-sensitive?
(5 answers)
Closed 1 year ago.
below is the SQL query ran in Postgres and its corresponding output.
The data type for the column 'premiseId' is text which is the equivalent of varchar (without any length specifier). So my query is how come the results contain null values in the specified column?
The Postgres version used is 10.11.
Single quotes in SQL denote string literals. 'premiseId' is not the column's name, it's a string literal. Since this literal is not null, all the rows in the table are returned. If you want to use the column in the query, you should refer to it without the quotes:
SELECT * FROM request WHERE premiseId IS NOT NULL
-- No quotes ---------------^-------^
Ah thanks for the replies!! I should have used double quotes instead as the column name has some characters that are upper case.
select * from request where "premiseId" is NOT NULL;
when trying to reproduce your issue:
create table testtable (
id integer primary key,
some_text varchar
)
insert into testtable values
(1, NULL), (2, 'abc'), (3, 'NULL'), (4, '[null]');
select * from testtable where some_text is not null
I got only expected results.
id
some_text
2
abc
3
NULL
4
[null]
as the id 3,4 are texts NULL or [null] and not the actual "value" null. Maybe an issue with your loader and the representation in the source (e.g. csv)
You should specify column names without quotes
SELECT * FROM request WHERE premiseId IS NOT NULL;
Here documentation https://www.postgresql.org/docs/current/sql-syntax-lexical.html
This question already has answers here:
What is the difference between single quotes and double quotes in PostgreSQL?
(3 answers)
Error: Column does not exist in postgresql for update [duplicate]
(1 answer)
postgres column "X" does not exist
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Closed 2 years ago.
I am running the query SELECT * FROM app_user WHERE login_id = "john"; and getting -
ERROR: column "john" does not exist
LINE 1: SELECT * FROM public.app_user WHERE login_id = "john";
^
SQL state: 42703
Character: 48
I have also tried SELECT * FROM public.app_user WHERE login_id = "john"; and I still get the same error.
The same error also occurs with any other column but the id column(id is the only non-VARCHAR column and is the primary key).
So, SELECT * FROM app_user WHERE id = 5; is working as expected .
A snapshot of the table follows.
If you write the string in double quotes, postgres will interpret it as a column name in the WHERE clause, just use single quotes:
SELECT * FROM public.app_user WHERE login_id = 'john';
This question already has answers here:
Postgres error updating column data
(2 answers)
PostgreSQL "column "foo" does not exist" where foo is the value
(1 answer)
postgres column "X" does not exist
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Error: Column does not exist in postgresql for update [duplicate]
(1 answer)
Closed 2 years ago.
Insert into Customer(
Customerid,
Username,
Fname,
Lname,
Street1,
Street2,
City,
State,
Zip
) VALUES(42, “Guitarhero”, “Wes”, “Montgomery”, “Mainstreet”, “Manhattan”, “NY”, 12304);
ERROR: column "“guitarhero”" does not exist
You're using double quotes for strings instead of single quotes. Double quotes indicate a column name, and single quotes indicate a string. Try this:
Insert into Customer (Customerid, Username, Fname, Lname, Street1, Street2, City, State, Zip)
VALUES (42, 'Guitarhero', 'Wes', 'Montgomery', 'Mainstreet', 'Manhattan', 'NY', 12304);
Also note that you're using uppercase letters in your table and column names, but those will be changed to lowercase by postgres unless you put them in double quotes.
This question already has answers here:
Postgres: check if array field contains value?
(4 answers)
How to make a select with array contains value clause in psql
(3 answers)
Search in integer array in Postgres
(3 answers)
Closed 4 years ago.
I have a table with a column 'sample_column' which is an array.
Can anyone tell me how can I select data based on 'sample_column' in postgresql?
Example of data in sample_column: ["one","two","three"]
I want to get all data if sample_column has value "three"
Here is what I have done:
Select * from sample_table where sample_column contains 'three'
I am getting ERROR.
Any help will be appreciated.
I assume you have table as:
CREATE TABLE table_name
(
sample_column text[]
);
and you have insert data as:
insert into table_name(sample_column) values (array['one','two','three']);
insert into table_name(sample_column) values (array['yes','no']);
insert into table_name(sample_column) values (array['red','white','blue']);
now you want to find recored based on the array element:
select * from table_name where 'three' = ANY(sample_column);
I hope it helps.
Demo