ERROR: operator does not exist : time without time zone >= bytea - postgresql

I get error like this
ERROR: operator does not exist : time without time zone >= bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
when i try the following sql in java hibernate
select sto.name AS store,
sum(odi.subtotal_price) AS sales, sum(odi.qty) AS qty_sold,
((sum(odi.subtotal_price))/(sum(odi.qty))) AS average,
min(CAST(ord.date_out AS date)) AS start_date,
max(CAST(ord.date_out AS date)) AS end_date,
concat(min(CAST(ord.date_out AS time)), ' - ', max(CAST(ord.date_out AS time))) as time,
sum(odi.cost_of_good_sold*odi.qty) AS COGS, ((sum(odi.cost_of_good_sold*odi.qty))/(sum(odi.qty))) AS average_cogs,
date_trunc('day', ord.date_out) AS trx_day
FROM trx_order_detail_item odi
LEFT JOIN trx_order AS ord on ord.id = odi.order_id
LEFT JOIN mst_store AS sto on sto.id = ord.store_id
WHERE sto.id = :store and ord.date_out between :date1 and :date2
and CAST(ord.date_out AS TIME) BETWEEN :hour1 AND :hour2 and ord.order_status_id IN :orderstatus and ord.void_status = :voidStatus
GROUP BY sto.name, date_trunc('day', ord.date_out)
ORDER BY date_trunc('day', ord.date_out)
the error exist in CAST(ord.date_out AS TIME) BETWEEN :hour1 AND :hour2
before i added that it runs perfectly
any suggestion for this?

Related

PostgreSQL Getting error in order to change explicit type cast

SELECT COUNT(*)
FROM WASADMIN.DAILYTXNSREPORT
WHERE UBACCOUNTID = '01ED10EOD0100'
AND UBTXNAMT = '109.63'
AND UBTYPE = 'I'
AND UBVALUEDTTM LIKE '11/7/2015 12:00:00 AM%'
AND UBTXNAMTCR = '109.63'
AND UBTXNAMTDR = '0.0'
AND UBTXNCODE = 'IAP'
AND UBTXNNARRATION = 'Fixed Narrative:Interest Application'
AND UBTXNSRCBRANCH = '70000001.0'
AND UBTXNBASEEQ = '109.63'
AND UBCHANNELID = 'UXP'
An error occurred when executing the SQL command:
SELECT COUNT(*) FROM WASADMIN.UBTB_DAILYTXNSREPORT WHERE UBACCOUNTID='01ED10EOD0100' AND UBTXNAMT='109.63' AND UBTYPE='I' AND UBVALUEDTTM LIKE '11/7/2...
ERROR: operator does not exist: timestamp without time zone ~~ unknown
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 139
Expected answer: to get the count
LIKE is for comparing string values, not for timestamps.
I am not sure what the LIKE condition is supposed to achieve, but it seem you want to find rows where `` is equal to midnight of 2015-11-07. The following would do that:
AND ubvaluedttm = timestamp '2015-11-07 00:00:00'
A range condition is probably closer to what the LIKE condition would do:
AND ubvaluedttm >= timestamp '2015-11-07 00:00:00'
AND ubvaluedttm <= timestamp '2015-11-07 00:00:00.999999'

case when in where clause postresql

select service_code,service_cat_code,mobile_no,upper(applicant_name_eng) as name,to_char(license_date,'dd/mm/yyyy')as license_from,to_char(license_valid_upto,'dd/mm/yyyy')as license_to,Upper(license_no),district_code,taluk_code,CONCAT(address_building,', ', address_cityvillage,', ',address_locality,', ',address_landmark,', ',address_street) as address
from mst_license
WHERE cast(license_valid_upto as date) = case
WHEN license_valid_upto < now()
THEN
case
when license_valid_upto = '2021-06-30'
then 1 else 0
END
ELSE
case when license_valid_upto > now()
then 1 else 0
End
END
and Upper(license_no)='1SP146924BJP'
I want license valid should be either greater than now or if license valid less than now it must be with the date ''30/06/2021' but when i use above query i get error
ERROR: operator does not exist: date = integer
LINE 3: WHERE cast(license_valid_upto as date) = case
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 418
Help me out guys
The main issue you have is that your case statement returns an integer (1 or 0) but you are trying to compare that to a date, which you cannot do as Postgres is a strict data typing. Even if it did work it would always be false (well except for 1969-12-31/1970-01-01). Moreover the case structure is not needed. The best/correct to compare dates is just use date values. Since you did not indicate the data type for column license_valid_upto so based on how it is used I'll assume it is timestamp with timezone as that is what NOW() returns. Your query becomes:
select service_code
, service_cat_code
, mobile_no
, upper(applicant_name_eng) as name
, to_char(license_date,'dd/mm/yyyy')as license_from
, to_char(license_valid_upto,'dd/mm/yyyy')as license_to
, upper(license_no) as license_no
, district_code
, taluk_code
, concat(address_building,', '
,address_cityvillage,', '
,address_locality,', '
,address_landmark,', '
,address_street) as address
from mst_license
where and upper(license_no)='1SP146924BJP'
and ( cast(license_valid_upto as date) > cast( now() as date)
or (cast (icense_valid_upto as date) < cast( now() as date)
and cast (icense_valid_upto as date) = date '2021-06-30'
)
);
Also, learn for format your queries for readability and so you do not need to scroll right. You, and others looking at your queries later will appreciate it later.

Query returning different number of rows, results in crosstab error

I get this error with my query, but I can't figure out what is wrong with it:
Query-specified return tuple has 135 columns but crosstab returns 295.
And here's my query: (The part highlighted in bold returns same number of rows when run separately in pgAdmin.)
SELECT X.*, pi.productcode, pi.productitemdesc, pi.retailsalesprice, cat.productcategorydesc FROM (
SELECT * FROM crosstab (
'SELECT a.productitem AS productitemid, l.locationcode, (CASE WHEN SUM(a.netamount) IS NOT NULL THEN SUM(a.netamount) ELSE 0 END) || ''#'' || (CASE WHEN SUM(a.quantity) IS NOT NULL THEN SUM(a.quantity) ELSE 0 END) AS sales_qty FROM invoiceitem a INNER JOIN invoiceinfo b ON a.invoice = b.invoiceid INNER JOIN locationinfo l ON b.location = l.locationid WHERE b.status !=2 AND l.locationtype = 1 AND l.status = 1 AND TO_CHAR (b.invoicedate, ''YYYY-MM-DD'')>=''2018-03-01'' AND TO_CHAR (b.invoicedate, ''YYYY-MM-DD'')<=''2018-03-03'' GROUP BY a.productitem, l.locationcode ORDER BY a.productitem',
'SELECT l.locationcode FROM locationinfo l INNER JOIN invoiceinfo b ON b.location = l.locationid
WHERE b.status !=2 AND l.locationtype = 1 AND l.status = 1 AND TO_CHAR (b.invoicedate, ''YYYY-MM-DD'')>=''2018-03-01'' AND TO_CHAR (b.invoicedate, ''YYYY-MM-DD'')<=''2018-03-03'' GROUP BY l.locationcode order by l.locationcode')
AS (productitemid int, "0007" text,"BE101" text,"BE1013" text,"BE1014" text,"BE102" text,"BE103" text,"BE1034" text,"BE104" text,"BE1040" text,"BE1043" text,"BE1044" text,"BE1045" text,"BE1046" text,"BE105" text,"BE106" text,"BE107" text,"BE108" text,"BE109" text,"BE110" text,"BE111" text,"BE112" text,"BE123" text,"BE1265" text,"BE1266" text,"BE1271" text,"BE1272" text,"BE1273" text,"BE1274" text,"BE1279" text,"BE1280" text,"BE1281" text,"BE1282" text,"BE1351" text,"BE1400" text,"BE1401" text,"BE1404" text,"BE141" text,"BE142" text,"BE193" text,"BE194" text,"BE2125" text,"BE2126" text,"BE2127" text,"BE2128" text,"BE3001" text,"BE3002" text,"BE3005" text,"BE3006" text,"BE3009" text,"BE3010" text,"BE3031" text,"BE3032" text,"BE3121" text,"BE3122" text,"BE3123" text,"BE3124" text,"BE3127" text,"BE3128" text,"BE3131" text,"BE3132" text,"BE3203" text,"BE3204" text,"BE325" text,"BE3253" text,"BE3254" text,"BE326" text,"BE332" text,"BE3503" text,"BE3504" text,"BE355" text,"BE356" text,"BE365" text,"BE366" text,"BE381" text,"BE382" text,"BE383" text,"BE384" text,"BE400" text,"BE401" text,"BE402" text,"BE403" text,"BE405" text,"BE406" text,"BE408" text,"BE409" text,"BE411" text,"BE412" text,"BE4311" text,"BE4316" text,"BE4401" text,"BE4402" text,"BE4521" text,"BE4522" text,"BE4551" text,"BE4552" text,"BE470" text,"BE473" text,"BE475" text,"BE481" text,"BE482" text,"BE601" text,"BE604" text,"BE609" text,"BE610" text,"BE7040" text,"BE7043" text,"BE7045" text,"BE7046" text,"BE7048" text,"BE7049" text,"BE708" text,"BE7111" text,"BE7112" text,"BE7127" text,"BE7128" text,"BE7217" text,"BE7218" text,"BE7307" text,"BE7308" text,"BE7351" text,"BE7352" text,"BE801" text,"BE802" text,"BE803" text,"BE804" text,"BE831" text,"BE832" text,"BE860" text,"BE861" text,"BE862" text,"BE863" text,"BE865" text,"BE981" text,"BE982" text
)) X
LEFT JOIN productitem pi ON X.productitemid = pi.productitemid
LEFT JOIN productcategory cat ON pi.productcategory = cat.productcategoryid
The bold part of
productitemid int, "0007" text,"BE101" text,"BE1013" text,"BE1014" text,"BE102" text,"BE103" text,"BE1034" text,"BE104" text,"BE1040" text,"BE1043" text,"BE1044" text,"BE1045" text,"BE1046" text,"BE105" text,"BE106" text,"BE107" text,"BE108" text,"BE109" text,"BE110" text,"BE111" text,"BE112" text,"BE123" text,"BE1265" text,"BE1266" text,"BE1271" text,"BE1272" text,"BE1273" text,"BE1274" text,"BE1279" text,"BE1280" text,"BE1281" text,"BE1282" text,"BE1351" text,"BE1400" text,"BE1401" text,"BE1404" text,"BE141" text,"BE142" text,"BE193" text,"BE194" text,"BE2125" text,"BE2126" text,"BE2127" text,"BE2128" text,"BE3001" text,"BE3002" text,"BE3005" text,"BE3006" text,"BE3009" text,"BE3010" text,"BE3031" text,"BE3032" text,"BE3121" text,"BE3122" text,"BE3123" text,"BE3124" text,"BE3127" text,"BE3128" text,"BE3131" text,"BE3132" text,"BE3203" text,"BE3204" text,"BE325" text,"BE3253" text,"BE3254" text,"BE326" text,"BE332" text,"BE3503" text,"BE3504" text,"BE355" text,"BE356" text,"BE365" text,"BE366" text,"BE381" text,"BE382" text,"BE383" text,"BE384" text,"BE400" text,"BE401" text,"BE402" text,"BE403" text,"BE405" text,"BE406" text,"BE408" text,"BE409" text,"BE411" text,"BE412" text,"BE4311" text,"BE4316" text,"BE4401" text,"BE4402" text,"BE4521" text,"BE4522" text,"BE4551" text,"BE4552" text,"BE470" text,"BE473" text,"BE475" text,"BE481" text,"BE482" text,"BE601" text,"BE604" text,"BE609" text,"BE610" text,"BE7040" text,"BE7043" text,"BE7045" text,"BE7046" text,"BE7048" text,"BE7049" text,"BE708" text,"BE7111" text,"BE7112" text,"BE7127" text,"BE7128" text,"BE7217" text,"BE7218" text,"BE7307" text,"BE7308" text,"BE7351" text,"BE7352" text,"BE801" text,"BE802" text,"BE803" text,"BE804" text,"BE831" text,"BE832" text,"BE860" text,"BE861" text,"BE862" text,"BE863" text,"BE865" text,"BE981" text,"BE982" text
AND
SELECT l.locationcode FROM locationinfo l INNER JOIN invoiceinfo b ON b.location = l.locationid
WHERE b.status !=2 AND l.locationtype = 1 AND l.status = 1 AND TO_CHAR (b.invoicedate, ''YYYY-MM-DD'')>=''2018-03-01'' AND TO_CHAR (b.invoicedate, ''YYYY-MM-DD'')<=''2018-03-03'' GROUP BY l.locationcode order by l.locationcode
When run seperately, I get 295 results which is correct. However putting it together in the whole query gets the error.
The manual:
The remaining output columns must have the type of the last column of the source_sql query's result, and there must be exactly as many of them as there are rows in the category_sql query's result.
crosstab(text, text) with a dynamic (!) SELECT query as 2nd parameter is very error prone, since the output column list is static. You should also generate the output column list dynamically. Example:
Dynamic alternative to pivot with CASE and GROUP BY
That aside, the different number of rows returned from the same query, may very well be due to a different timezone setting in the two different sessions.
You have the expression TO_CHAR(b.invoicedate, 'YYYY-MM-DD') in your predicates. If b.invoicedate is type timestamptz the result depends on the timezone setting. Consider:
SET timezone = '+10';
SELECT TO_CHAR(timestamptz '2018-04-07 23:30+0', 'YYYY-MM-DD');
to_char
----------
2018-04-08
SET timezone = '-10';
SELECT TO_CHAR(timestamptz '2018-04-07 23:30+0', 'YYYY-MM-DD');
to_char
----------
2018-04-07
Solution
To remove the dependency on the timezone setting, use absolute values.
to_char() is expensive complication in this place to begin with. Drop that and adjust the predicate.
Provide timestamptz values to match your column. This also allows indexes to be used (if applicable).
Return Table Type from A function in PostgreSQL
Use time zone names (not offsets or abbreviations) to avoid more misunderstandings.
Ignoring timezones altogether in Rails and PostgreSQL
Use dollar-quoting to clean up the quote mess.
Insert text with single quotes in PostgreSQL
I suggest this as 2nd parameter:
$$
SELECT l.locationcode
FROM locationinfo l
JOIN invoiceinfo b ON b.location = l.locationid
WHERE b.status <> 2 AND l.locationtype = 1 AND l.status = 1
AND b.invoicedate >= timestamptz '2018-03-01 Europe/Vienna'
AND b.invoicedate < timestamptz '2018-03-04 Europe/Vienna'
GROUP BY 1
ORDER BY 1
$$
Replace Europe/Vienna in my example with the time zone name defining your dates.
Note 2018-03-04 to include all of 2018-03-03 like you intended.

Stored Procedure Date appears long date

ALTER PROCEDURE [dbo].[SP_My_Procedured]
AS
BEGIN
SELECT Mission_Time
FROM Mission_Table
WITH (NOLOCK)
WHERE
cast(getdate() as Date)=Mission_Time
END
When i run SP_My_Procedured,
I see Mission_Time as
"2014-01-04 08:35:05.510"
"2014-01-03 10:49:00.697"
But ı want to see like below,
"2014-01-04"
"2014-01-03"
So how can i do this in stored procedure by select ?
Any help will be appreciated.
Cast the value in the SELECT list to Date:
ALTER PROCEDURE [dbo].[SP_My_Procedured]
AS
BEGIN
SELECT
Mission_Time = cast(Mission_Time as Date)
FROM
Mission_Table (NOLOCK)
WHERE
cast(Mission_Time as Date) = cast(getdate() as Date)
END
[BTW: there are dangers to using NO LOCK]
[Also, casting the column to date may not be necessary if it is already of type Date (you don't specify its type). Doing so may result in an appropriate index not being used]
Try
SELECT CONVERT(VARCHAR(10), 'date', 120) AS MissionTime
For more date formats check this

column "statement_date" is of type date but expression is of type integer

Consider the following query:
INSERT INTO statement_line_items
SELECT count(*)::integer as clicks, sum(amount_cents)::integer as amount_cents, imps.user_id, imps.created_at::date as statement_date
FROM impression_events imps
INNER JOIN transactions t ON t.event_id = imps.id
AND t.event_type = 'ImpressionEvent'
AND amount_cents >= 0
WHERE imps.created_at >= (now() - interval '8 days')::date
AND imps.created_at < (now() - interval '7 day')::date
AND imps.clicked = true
GROUP BY imps.user_id, imps.created_at::date;
This is returning:
ERROR: column "statement_date" is of type date but expression is of type integer
LINE 2: ...icks, sum(amount_cents)::integer as amount_cents, imps.user_...
^
HINT: You will need to rewrite or cast the expression.
********** Error **********
ERROR: column "statement_date" is of type date but expression is of type integer
SQL state: 42804
Hint: You will need to rewrite or cast the expression.
Character: 117
My table structure for statement_line_items is:
"id"; "integer"
"user_id"; "integer"
"statement_date"; "date"
"description"; "character varying(255)"
"clicks"; "integer"
"amount_cents"; "integer"
"created_at"; "timestamp without time zone"
"updated_at"; "timestamp without time zone"
You are putting the imps.userid in your statement_date column. That has to fail.
count(*)::integer as clicks goes into id
sum(amount_cents)::integer as amount_cents goes into userid
imps.user_id goes into statement_date
To specify the order you are inserting you can do this:
INSERT INTO statement_line_items (col1, col2, ...)
values (select data_for_col1, data_for_col2, ...)
If you have this kind of problem check if your date is in the apostrophe.