Invalid input syntax for type date: "\N" in postgresql when fetching data - sql

I'm using postgresql and getting this sql error:
org.postgresql.util.PSQLException: ERROR:
invalid input syntax for type date: "\N"
Here is my code:
SELECT * FROM person WHERE person.dob = '\N' OR person.dob = '1994-01-16';
How can I allow Null values? so what should I add to this SQL statement?

Try this following code:
SELECT * FROM person WHERE person.dob is null OR person.dob = '1994-01-16';

Related

SQL error when using format() function with pyodbc in Django

I want to execute a command using pyodbc in my Django app. When I do simple update with one column it works great:
cursor.execute("UPDATE dbo.Table SET attr = 1 WHERE id = {}".format(id))
However when I try to use a string as a column value it throws error:
cursor.execute("UPDATE dbo.Table SET attr = 1, user = '{}' WHERE id = {}".format(id, str(request.user.username)))
Here's error message:
('42S22', "[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'Admin'. (207) (SQLExecDirectW)")
Suprisingly this method works:
cursor.execute("UPDATE dbo.Table SET attr = 1, user = 'Admin' WHERE id = {}".format(id))
What seems to be the problem? Why is sql mistaking column value for its name?
As mentioned above, you have your arguments backwards, but if you're going to use cursor.execute(), the far more important thing to do is use positional parameters (%s). This will pass the SQL and values separately to the database backend, and protect you from SQL injection:
from django.db import connection
cursor = connection.cursor()
cursor.execute("""
UPDATE dbo.Table
SET attr = 1,
user = %s
WHERE id = %s
""", [
request.user.username,
id,
])
You've got your format arguments backwards. You're passing id to user, and username to the id WHERE clause.

INSERT INTO WITH SELECT and WHERE (Problem)

I need to perform an insert into with select, but DBEAVER is returning the following error:
SQL Error [904] [42000]: ORA-00904:" NR_CARTEIRA ": invalid identifier
Does anyone know what can it be ?
INSERT INTO recem_nascido(
DT_ATENDIMENTO ,
CD_DECLARACAO_NASCIDO_VIVO,
cd_atendimento,
CD_MULTI_EMPRESA,
cd_paciente,
nm_paciente,
CD_ATENDIMENTO_PAI,
dt_nascimento,
nm_mae,
cd_convenio,
NR_CARTEIRA )
SELECT
a.DT_ATENDIMENTO ,
c.CD_DECLARACAO_NASCIDO_VIVO,
a.cd_atendimento,
a.CD_MULTI_EMPRESA,
a.cd_paciente,
b.nm_paciente,
a.CD_ATENDIMENTO_PAI,
b.dt_nascimento,
b.nm_mae,
a.cd_convenio,
a.NR_CARTEIRA
FROM
DBAMV.ATENDIME a,
dbamv.paciente b,
dbamv.recem_nascido c
WHERE
a.CD_ATENDIMENTO = 33079344
AND c.CD_DECLARACAO_NASCIDO_VIVO = 111
AND A.NR_CARTEIRA = 321321321
AND a.cd_atendimento_pai IS NOT NULL
AND a.CD_PACIENTE = b.CD_PACIENTE
AND c.CD_ATENDIMENTO = a.CD_ATENDIMENTO; ```
There are only two options:
The target table recem_nascido has no column NR_CARTEIRA
The table DBAMV.ATENDIME has no column NR_CARTEIRA
Since you only posted the query, but not the table structure, there is no way to narrow this down further.

regex operator giving error operator does not exist: bigint ~ unknown

I am very new to postgres and trying to find out an issue with the query using a regex.
Query:
select name, ip_address, install_status
from sn_cache_prod.cmdb_ci
where support_group = 'bee96a2135a631007fa2c5751b2c74be'
and u_environment = 5
and install_status ~ '(110|3)';
Error:
ERROR: operator does not exist: bigint ~ unknown
install_status column is defined as bigint.
Can the query work without casting?
A regular expressions applies to string (text) values, it can't be used on numbers. In your case you want the IN operator:
select name,ip_address,install_status
from sn_cache_prod.cmdb_ci
where support_group = 'bee96a2135a631007fa2c5751b2c74be'
and u_environment = 5
and install_status in (110,3);
That's equivalent to
and (install_status = 110 or install_status = 3)

json_extract SQL in Cakephp

I need some help with a json_extract query. I'm trying to execute the following query from Cakephp 2 :
select * from certificates_types where json_extract(params, '$.params.invoice_date') > '2018-05-15'
I use Cakephp's query to do that :
$query = 'SELECT * FROM certificates_types WHERE json_extract("params", "$.params.invoice_date") >= "2008-01-01"';
$types = $this->CertificatesType->query($query);
I only get the same error : "SQL Error: 3141: Invalid JSON text in argument 1 to function json_extract: "The document is empty." at position 0". Did some of you used to face this problem ?
to use sql functions try this
$query = $this->CertificatesType->find();
$query->where(['CAST( JSON_UNQUOTE( JSON_EXTRACT(params,"$.params.invoice_date")) as DATE) >'=>'2018-05-15'])

hive date format error when used with the table

I have fields like date_column = 20140228 in the table 1. When I hard code the number like below it works, but when I specify the column name its failing. With error
H110 Unable to submit statement. Error while compiling statement: FAILED: ParseException line 2:1 cannot recognize input near 'select' 'date_format' '(' in select clause [ERROR_STATUS]
Working:
select date_format(from_unixtime(unix_timestamp(cast('2014022817' as string),'yyyyMMddHH')),'yyyy-MM-dd HH');
Failing:
select
select date_format(from_unixtime(unix_timestamp(cast(date_column as string),'yyyyMMddHH')),'yyyy-MM-dd HH')
from
table1
Why are you repeating the select? Try this:
select date_format(from_unixtime(unix_timestamp(cast(date_column as string
),'yyyyMMddHH'
)
),'yyyy-MM-dd HH'
)
from table1