ParseException: mismatched input '2022' expecting {<EOF>, ';'} - pyspark

I have the following PySpark/SQL code:
extracted_at = "2022-01-31 00:00:00"
extracted_at_date = "2022-01-31 00:00:00"
current_fiscal_period = (sqlContext.sql('SELECT CUR_PER FROM TBM_PARMS').first()[0])
current_reporting_fiscal_period = sqlContext.sql('SELECT CUR_PER FROM TBM_PARMS').first()[0]
query = "SELECT PER_END_DATE FROM GLM_PERIOD_END WHERE PERIOD = {}".format(current_fiscal_period)
current_reporting_fiscal_period_end = sqlContext.sql(query).first()[0]
query2 = "SELECT CASE WHEN {} > {} THEN {} ELSE {} END".format(extracted_at_date, current_reporting_fiscal_period_end, current_reporting_fiscal_period_end, extracted_at_date)
current_reporting_date = sqlContext.sql(query2).first()[0]
I am getting the following error:
ParseException: mismatched input '2022' expecting {<EOF>, ';'}(line 1, pos 17)
== SQL ==
SELECT CASE WHEN 2022-01-31 00:00:00 > 2022-05-31 00:00:00 THEN 2022-05-31 00:00:00 ELSE 2022-01-31 00:00:00 END
-----------------^^^
I cannot figure out the error in the SQL. Any pointers would be greatly appreciated.

Your query is missing the '
You may try:
query2 = f"SELECT CASE WHEN '{extracted_at_date}' > '{current_reporting_fiscal_period_end}' THEN '{current_reporting_fiscal_period_end}' ELSE '{extracted_at_date}' END"
or
query2 = "SELECT CASE WHEN '{}' > '{}' THEN '{}' ELSE '{}' END".format(extracted_at_date, current_reporting_fiscal_period_end, current_reporting_fiscal_period_end, extracted_at_date)
Btw, I think it would be a good idea to parse those timestamps as date/time/timestamp and not deal with them as strings.

Related

org.postgresql.util.PSQLException: ERROR: operator does not exist: date = bytea on date null in native query JPA

I would like to have a native query for Postgresql, however I could not make it work with dates. I can receive date as null, or date as date, so I like to work in both cases:
" case when coalesce (?3, null) = ?3 then true " +
" else p.start_date = ?3 end"
if ?3 is the third parameter and can be null and it is of type LocalDate
I tried many variations, but could not make it work for both cases, separately it will work:
if coalesce (?3, null) = ?3 for date param null it will work
if p.start_date = ?3 for date param != null it will work
The column in the database is date
Thanks:)
This worked for me :
case when ?3 <> '' then p.start_date = (cast(?3 as date))
else 1 = 1 end
So, I am passing string parameter and convert it to date, because nothing else worked for me

ERROR: invalid input syntax for type timestamp:

I am getting an Error in my PostgreSQL query as
ERROR: invalid input syntax for type timestamp:
I am setting the value using \set.
\set dueDateEarliest '2018-04-01'
\set dueDateLatest '2018-08-01'
\set dueDateLatest '2018-08-01'
And trying to use these value in my query as below
SELECT DISTINCT(bu.id) as "user_id",c.organization_name as "name",round(i.balance,2) as "amount_due",i.id as "invoice_number",i.due_date as "due_date",CONCAT('collectionMonth', LPAD(cf2.content,2,'0')) as "collection_date" FROM base_user bu,contact c,contact_field cf, invoice i, contact_field cf2 WHERE bu.id = c.user_id AND bu.deleted = 0 AND cf.contact_id = c.id AND cf.type_id = 7 AND cf.content = 'DD' AND i.user_id = bu.id AND i.balance > 0 AND i.is_review != 1 AND i.deleted != 1 AND due_date BETWEEN 'dueDateEarliest' AND 'dueDateLatest' AND cf2.contact_id = c.id AND cf2.type_id = 8 ORDER BY bu.id limit 20;
This is giving error as
ERROR: invalid input syntax for type timestamp:
I am not getting any way to fix it.
And moreover the way I am setting value using \set is it fine ?
Or should I use SET to set the values.
Because in actual when I have to run these command from a shell script I will be calling/setting as
`set dueDateEarliest '$dueDateEarliest'` from shell script.
Which is the best way ?
Attaching the screen shot as well
It's an issue with how you formatted your query. Let's simplify it a bit:
# \set dueDateEarliest '2018-04-01'
# select 'dueDateEarliest'::timestamp;
ERROR: invalid input syntax for type timestamp: "dueDateEarliest"
LINE 1: select 'dueDateEarliest'::timestamp;
It doesn't work, because it's trying to use the string 'dueDateEarliest', not the variable.
Here's the correct way:
# select :'dueDateEarliest'::timestamp;
timestamp
---------------------
2018-04-01 00:00:00
(1 row)

PostgreSQL error witn Laravel

I'm trying to make a request and I have an error when I'm using LIKE on the created_at field.
I'm using Laravel 5.3 with Eloquent (ORM).
$date = Carbon::now();
foreach ($hours as $hour) {
$chart[$hour]['hour'] = $hour;
$chart[$hour]['allowed'] = VisitsAllowed::where('created_at', 'LIKE', Carbon::parse($date)->format('Y-m-d %'))->count();
$chart[$hour]['denied'] = VisitsDenied::where('created_at', 'LIKE', Carbon::parse($date)->format('Y-m-d %'))->count();
}
Error :
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: timestamp without time zone ~~* unknown
LINE 1: ... aggregate from "visits_allowed" where "created_at" LIKE $1
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. (SQL: select count(*) as aggregate from "visits_allowed" where "created_at" LIKE 2017-05-30 %)
Can someone help me to find a solution.
Please try this instead :
foreach ($hours as $hour) {
$chart[$hour]['hour'] = $hour;
$chart[$hour]['allowed'] = VisitsAllowed::where('created_at', '=', Carbon::parse($date)->format('Y-m-d'))->count();
$chart[$hour]['denied'] = VisitsDenied::where('created_at', '=', Carbon::parse($date)->format('Y-m-d'))->count();
}
Not the prettiest solution but testing if the date (say 2017-05-30) is between: yyyy-mm-dd 00:00:00 and yyyy-mm-dd 23:59:59 works.
$chart[$hour]['allowed'] = VisitsAllowed::where('created_at', '>=', Carbon::parse($date )->format('Y-m-d') . ' 00:00:00')
->where('created_at', '<=', Carbon::parse($date )->format('Y-m-d') . ' 23:59:59')
->count();
$chart[$hour]['denied'] = VisitsDenied::where('created_at', '>=', Carbon::parse($date )->format('Y-m-d') . ' 00:00:00')
->where('created_at', '<=', Carbon::parse($date )->format('Y-m-d') . ' 23:59:59')
->count();

Postgres insert syntax error

My SQL query looks like this:
product = 'Huggies Little Movers Diaper Pants for Boys Size 5 (60 Count)'
retailer = 'Target'
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s""" % (product, retailer)
conn = psycopg2.connect("dbname='test1' user='postgres' host='localhost' password='123'")
cur = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
cur.execute(query)
When i execute that i get a error saying:
psycopg2.ProgrammingError: syntax error at or near "Basic"
I am not sure why my syntax is wrong
Your statement;
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s""" % (product, retailer)
...builds a complete string from the query and parameters without any quoting around your strings, which makes the entire string invalid SQL which fails at execute;
SELECT * FROM product_info
WHERE product_name = Huggies Little Movers Diaper Pants for Boys Size 5 (60 Count)
AND retailer = Target
What you're probably trying to do is parameterizing your query which is instead done in execute by passing the parameters in a tuple;
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s"""
...
cur.execute(query, (product, retailer))

'DECFLOAT' error in db2

Lookup Error - DB2 Database Error: ERROR [22018] [IBM][DB2/AIX64] SQL0420N Invalid character found in a character string argument of the function "DECFLOAT".
Query-----
SELECT
MSISDN,
CONTRNO,
TRANSDATE,
TARIFF_GROUP,
ACT_DURATION,
BILLTEXT,
GROSS_AMOUNT,
CASE
WHEN TARIFF_GROUP IN('PAG2')
THEN DECIMAL((DECIMAL(ACT_DURATION,10,4)/10),20,4)*0.01
ELSE 'CHECK'
END RA_RATE
FROM HISTCALLS
WHERE call_type IN (50,
54)
AND TRANSDATE = CURRENT date - 1 DAY
The problem is in your case expression.
A single result column cannot be numeric for some rows and character in others.
SELECT
MSISDN,
CONTRNO,
TRANSDATE,
TARIFF_GROUP,
ACT_DURATION,
BILLTEXT,
GROSS_AMOUNT,
CASE
WHEN TARIFF_GROUP = 'PAG2'
THEN DECIMAL(ACT_DURATION * 0.001, 10,4)
ELSE null
END RA_RATE
FROM HISTCALLS
WHERE call_type IN (50, 54)
AND TRANSDATE = CURRENT date - 1 DAY