I'm using PHP PDO to query from DB2.
The following works.
$sql = "SELECT COL1,
CONCAT(COL2,COL3) cat
FROM TABLE
WHERE COL4 = $param";
$statement = $dbConnection->prepare($sql);
$statement->execute();
But...I want to insert a '-' in between COL2 & COL3 in the CONCAT
But..doing
$sql = "SELECT COL1,
CONCAT(COL2,'-',COL3) cat
FROM TABLE
WHERE COL4 = $param";
$statement = $dbConnection->prepare($sql);
$statement->execute();
Does not work. I even tried escaping the quotes for '-'. Any idea how to insert a String into the CONCAT params?
select col1, col2 || '-' || col3 as cat from table where col4 = $param
Related
begin;
create table test101(col1 int default 2, col2 text default 'hello world');
insert into test101 values (1,default);
insert into test101 values (default,default);
insert into test101 values (default,'dummy');
insert into test101 values (5,'dummy');
commit;
update: OK.
update test101 set col2 = default where col1 = 4;
select, delete not OK.
select * from test101 where col1 = COALESCE (col1,default);
delete from test101 where col1 = COALESCE (col1,default);
error code:
ERROR: 42601: DEFAULT is not allowed in this context
LINE 1: delete from test101 where col1 = COALESCE (col1,default);
^
LOCATION: transformExprRecurse, parse_expr.c:285
also tried: delete from test101 where col1 = default;
default value is not easy to find.
Get the default values of table columns in Postgres? Then select/delete operation with default operation is not that weird.
In the question you linked to they do:
SELECT column_name, column_default
FROM information_schema.columns
WHERE (table_schema, table_name) = ('public', 'test101')
ORDER BY ordinal_position;
which produces something like:
column_name | column_default
-------------+---------------------
col1 | 2
col2 | 'hello world'::text
Maybe, you can combine this query with your query? (But I would not recommend it, because ... )
I thought I knew PostgreSQL pretty well and have done this many times before, but I have here
\copy MyTable FROM ... WITH CSV NULL AS ''
and despite that a row such as:
"FooBar","","","1","0","","29505;29505",""
will not get its second column imported as NULL but as empty string!
SELECT count(1) FROM MyTable WHERE col2 IS NULL
== 0
SELECT count(1) FROM MyTable WHERE col2 = ''
>= 1
What am I doing wrong? Such a simple thing!
Here is a completely self-sufficient test to prove this:
CREATE TABLE Test(col1 text, col2 int);
\copy Test FROM PROGRAM 'printf '',\na,2\n''' WITH DELIMITER ',' NULL ''
SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
(SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
(SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
TRUNCATE Test;
\copy Test FROM PROGRAM 'printf ''"",""\n"a","2"\n''' WITH CSV NULL ''
\copy Test FROM PROGRAM 'printf ''"","0"\n"a","2"\n''' WITH CSV NULL ''
SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
(SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
(SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
DROP TABLE Test;
And here is how it runs:
foo=# CREATE TABLE Test(col1 text, col2 int);
SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
(SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
(SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
TRUNCATE Test;
\copy Test FROM PROGRAM 'printf ''"",""\n"a","2"\n''' WITH CSV NULL ''
\copy Test FROM PROGRAM 'printf ''"","0"\n"a","2"\n''' WITH CSV NULL ''
SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
(SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
(SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
DROP TABLE Test;CREATE TABLE
foo=# \copy Test FROM PROGRAM 'printf '',\na,2\n''' WITH DELIMITER ',' NULL ''
COPY 2
foo=# SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
foo-# (SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
foo-# (SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
col1null | col2null | col1empty
----------+----------+-----------
1 | 1 | 0
(1 row)
foo=# TRUNCATE Test;
TRUNCATE TABLE
foo=# \copy Test FROM PROGRAM 'printf ''"",""\n"a","2"\n''' WITH CSV NULL ''
ERROR: invalid input syntax for integer: ""
CONTEXT: COPY test, line 1, column col2: ""
foo=# \copy Test FROM PROGRAM 'printf ''"","0"\n"a","2"\n''' WITH CSV NULL ''
COPY 2
foo=# SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
foo-# (SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
foo-# (SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
col1null | col2null | col1empty
----------+----------+-----------
0 | 0 | 1
(1 row)
foo=# DROP TABLE Test;
DROP TABLE
So what can I do to get "" CSV as in text columns interpreted as NULL?
I got it now
\copy Test FROM PROGRAM 'printf ''"",""\n"a","2"\n''' WITH(FORMAT CSV, DELIMITER ',', NULL '', FORCE_NULL(col1,col2))
Turns out the COPY syntax is very lax, but if you really want to make it work, you must use the WITH with parentheses and do it all correctly. Weird.
I am trying to make regular expression search on a table in PostgreSql . But i am not able to find right syntax to do so using single statement .
I don't know if i create index column will i be able to do search based on regular expression or not .could any one help me please
The query is like this
SELECT *
FROM flight_info
where to_tsvector('english',event_id || ' ' || type || ' ' || category || ' ' || number || ' '|| make || ' '|| model ) ## to_tsquery('Incident');
Here Incident is matched exactly based on text But I need to make search base on regular expression like how we do using LIKE .
Or Using like clause on all the column is the only way ?
Or is there a way to give like clause that applies to many columns in query
You will not be able to mix Regex and full text search. It is not possible to use a Regex within to_tsquery()
You can use regexp_matches() instead:
SELECT regexp_matches('foobarbequebaz', '(bar)(beque)');
regexp_matches
----------------
{bar,beque}
(1 row)
SELECT regexp_matches('foobarbequebazilbarfbonk', '(b[^b]+)(b[^b]+)', 'g');
regexp_matches
----------------
{bar,beque}
{bazil,barf}
(2 rows)
More info at https://www.postgresql.org/docs/9.6/static/functions-matching.html
Update:
To use a Regex in whe WHERE clause, use the ~ operator:
-- Sample schema
CREATE TABLE sample_table (
id serial,
col1 text,
col2 text,
col3 text
);
INSERT INTO sample_table (col1, col2, col3) VALUES ('this', 'is', 'foobarbequebazilbarfbonk');
INSERT INTO sample_table (col1, col2, col3) VALUES ('apple foobarbequebazequeb', 'rocky', 'sunny');
INSERT INTO sample_table (col1, col2, col3) VALUES ('not', 'a', 'match');
-- Query
SELECT *
FROM sample_table
WHERE (col1 || col2 || col3) ~ '(bar)(beque)';
->
id | col1 | col2 | col3
----+---------------------------+-------+--------------------------
1 | this | is | foobarbequebazilbarfbonk
2 | apple foobarbequebazequeb | rocky | sunny
(2 rows)
You can use ~* instead to make it case insensitive.
More info: https://www.postgresql.org/docs/current/static/functions-matching.html
Anyone know how string_agg results need to be "massaged" so they can be used in an IN statement?
The following is some sample code. Thanks for your time.
P.S: Before scratching your head and asking what the hell. I'm only using this code to show the problem of the string_agg b/c as you can see the query otherwise is a bit pointless.
Henry
WITH TEMP AS
(
SELECT 'John' AS col1
UNION ALL
SELECT 'Peter' AS col1
UNION ALL
SELECT 'Henry' AS col1
UNION ALL
SELECT 'Mo' AS col1
)
-- results that are being used in the IN statement
--SELECT string_agg('''' || col1::TEXT || '''',',') AS col1 FROM TEMP
SELECT col1 FROM TEMP
WHERE col1 IN
(
SELECT string_agg('''' || col1::TEXT || '''',',') AS col1
FROM TEMP
)
You can't mix dynamic code with static code. Your example is not very clear as to what exactly is it that you want to do. Your sample could be written as:
WITH TEMP(col1) AS (values ('John'), ('Peter'), ('Henry'), ('Mo'))
SELECT col1 FROM TEMP
WHERE col1 IN (SELECT col1 FROM TEMP)
or using an array:
WITH TEMP(col1) AS (values ('John'), ('Peter'), ('Henry'), ('Mo'))
SELECT col1 FROM TEMP
WHERE col1 = ANY (SELECT ARRAY(SELECT col1 FROM TEMP))
or simply (in this case since the main from and the subselect are the same table without any filters):
WITH TEMP(col1) AS (values ('John'), ('Peter'), ('Henry'), ('Mo'))
SELECT col1 FROM TEMP
I have 2 select statements inside of strings setup like this:
set #sql = 'Select.......' -- (Returns col1, col2, col3)
exec (#sql)
set #sql = 'Select.......' -- (Returns col4, col5, col6)
exec (#sql)
I want to Join these 2 exec statements so that the columns will appear like this as a result:
col1 | col2 | col3 | col4 | col5 | col6
Any tips? Thanks.
Turn on DATA ACCESS on your local server:
exec sp_serveroption #server = 'YourServerName'
,#optname = 'DATA ACCESS'
,#optvalue = 'TRUE'
And use OPENQUERY:
select
*
from (
select * from openquery(YourServerName, 'select 1 as a')
) t1
full join (
select * from openquery(YourServerName, 'select 3 as b')
) t2
on t1.a = t2.b
DECLARE #SQL1 VARCHAR(MAX)
DECLARE #SQL2 VARCHAR(MAX)
SET #SQL1='SELECT COL1,COL2,NULL AS COL3,NULL AS COL4 FROM #TEST1'
SET #SQL2='SELECT NULL,NULL, COL3, COL4 FROM #TEST2'
EXEC (#SQL1 +' UNION ALL '+ #SQL2)
I got the result that I wanted by removing the EXEC and keeping them as plain Select statements. Then I performed an Inner Join to avoid NULLS. Thanks all for the help.