ERROR: function date_trunc(timestamp without time zone) does not exist - sql

I have this problem. I have an sql query am trying to make to my postgres db. These queries work fine in oracle but am in the process of converting it to a postgres query but it complains. This is the query:
select to_char(calldate,'Day') as Day, date_trunc(calldate) as transdate,
Onnet' as destination,ceil(sum(callduration::integer/60) )as total_minutes,round(sum(alltaxcost::integer) ,2)as revenue
from cdr_data
where callclass ='008' and callsubclass='001'
and callduration::integer >0
and regexp_like(identifiant,'^73')
and bundleunits = 'Money'
and inserviceresultindicator in (0,5)
and regexp_like(regexp_replace(callednumber,'^256','') ,'^73')
group by to_char(calldate,'Day') ,trunc(calldate),'Onnet' order by 2
And the error am getting is this:
Err] ERROR: function date_trunc(timestamp without time zone) does not exist
LINE 4: select to_char(calldate,'Day') as Day, date_trunc(calldate)...
What am I doing wrong, or what is the solution to this error?

Try:
... date_trunc('day',calldate) ...
For PostgreSQL date_trunc() function you must always specify precision as the first argument.
Details here.

Related

What causes error "Strings cannot be added or subtracted in dialect 3"

I have the query:
WITH STAN_IND
AS (
SELECT ro.kod_stanow, ro.ind_wyrob||' - '||ro.LP_OPER INDEKS_OPERACJA, count(*) ILE_POWT
FROM M_REJ_OPERACJI ro
JOIN M_TABST st ON st.SYMBOL = ro.kod_stanow
WHERE (st.KOD_GRST starting with 'F' or (st.KOD_GRST starting with 'T') ) AND ro.DATA_WYKON>'NOW'-100
GROUP BY 1,2)
SELECT S.kod_stanow, count(*) ILE_INDEKS, SUM(ILE_POWT-1) POWTORZEN
from STAN_IND S
GROUP BY S.kod_stanow
ORDER BY ILE_INDEKS
That should be working, but I get an error:
SQL Error [335544606] [42000]: Dynamic SQL Error; expression evaluation not supported; Strings cannot be added or subtracted in dialect 3 [SQLState:42000, ISC error code:335544606]
I tried to cast it into bigger varchar but still no success. What is wrong here? Database is a Firebird 2.1
Your problem is 'NOW'-100. The literal 'NOW' is not a date/timestamp by itself, but a CHAR(3) literal. Only when compared to (or assigned to) a date or timestamp column will it be converted, and here the subtraction happens before that point. And the subtraction fails, because subtraction from a string literal is not defined.
Use CAST('NOW' as TIMESTAMP) - 100 or CURRENT_TIMESTAMP - 100 (or cast to DATE or use CURRENT_DATE if the column DATA_WYKON is a DATE).

Log Parser not working with named column in WHERE statement

I am running into an issue with Log Parser where I cannot used named columns in the WHERE statement. The named column works fine in the Order By statement. Removing the named column from the WHERE statement makes the query work fine.
This is the error I get:
Error parsing query: WHERE clause: Semantic Error: WHERE clause contains aggregate functions [SQL query syntax invalid or unsupported.]
This is my query:
SELECT cs-uri-stem, count(cs-uri-stem) as hits INTO '" + $Destination + "' FROM $filePaths WHERE (cs-uri-stem LIKE '/testing%' OR cs-uri-stem LIKE '%/test%') AND (date > '2020-08-01' AND date < '2020-09-31') AND (hits > 1) Group By cs-uri-stem order by hits desc
Does Log Parser not support this?
No SQL dialect supports this - the results of aggregate functions cannot be referenced in the WHERE clause. You may use the HAVING clause instead for filtering aggregations:
...
Group By cs-uri-stem
HAVING hits > 1
order by hits desc

Google BigQuery Date(String format) failed to convert/cast as Date

I am trying to convert/cast the date in the big query into date format. My query is like this:
SELECT CAST(t.date AS date)
FROM `table` t;
But I got an Error code of Invalid date:'20151108'. it gave me different error date when I run the query.
Any thoughts?
try
SELECT PARSE_DATE('%Y%m%d', t.date) FROM table t

query to subtract date from systimestamp in oracle 11g

I want to perform a subtraction operation on the date returned from another query and the system time in oracle SQL. So far I have been able to use the result of another query but when I try to subtract from systimestamp it gives me the following error
ORA-01722: invalid number
'01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
Below is my query
select round(to_number(systimestamp - e.last_time) * 24) as lag
from (
select ATTR_VALUE as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
) e;
I have also tried this
select to_char(sys_extract_utc(systimestamp)-e.last_time,'YYYY-MM-DD HH24:MI:SS') as lag
from (
select ATTR_VALUE as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
) e;
I want the difference between the time intervals to be in hours.
Thank you for any help in advance.
P.S. The datatype of ATTR_VALUE is VARCHAR2(150). A sample result of e.last_time is 2016-09-05 22:43:81796
"its VARCHAR2(150). That means I need to convert that to date"
ATTR_VALUE is a string so yes you need to convert it to the correct type before attempting to compare it with another datatype. Given your sample data the correct type would be timestamp, in which case your subquery should be:
(
select to_timestamp(ATTR_VALUE, 'yyyy-mm-dd hh24:mi:ss.ff5') as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
)
The assumption is that your sample is representative of all the values in your CONFIG table for the given keys. If you have values in different formats your query will break on some other way: that's the danger of using this approach.
So finally after lots of trial and errors I got this one
1. Turns out initially the error was because the data_type of e.last_time was VARCHAR(150).
To find out the datatype of a given column in the table I used
desc <table_name>
which in my case was desc CONFIG
2. To convert VARCHAR to system time I have two options to_timestamp and to_date. If I use to_timestamp like
select round((systimestamp - to_timestamp(e.last_time,'YYYY-MM-DD HH24:MI:SSSSS')) * 24, 2) as lag
from (
select ATTR_VALUE as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
) e;
I get an error that round expects NUMBER and got INTERVAL DAY TO SECONDS since the difference in date comes out to be like +41 13:55:20.663990. To convert that into hour would require a complex logic.
An alternative is to use to_data which I preferred and used it as
select round((sysdate - to_date(e.last_time,'YYYY-MM-DD HH24:MI:SSSSS')) * 24, 2) as lag
from (
select ATTR_VALUE as last_time
from CONFIG
where ATTR_NAME='last_time'
and PROCESS_TYPE='new'
) e;
This returns me the desired result i.e. the difference in hours rounded off to 2 floating digits

SQL Server gives syntax error

I have following SQL for Microsoft SQL Server
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN DATE'2015-09-12' AND DATE'2015-09-15'
But this throws a syntax error:
Error: Incorrect syntax near '2015-09-12'. SQLState: S0001 ErrorCode:
102
What is wrong? I want to use ANSI literal code.
This is not how you cast in MS SQL Server. Instead, you should try the cast syntax:
SELECT *
FROM tblA
WHERE creation_date BETWEEN CAST('2015-09-12' AS DATE) AND
CAST('2015-09-15' AS DATE)
You can simply use this query, without adding date:
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN '2015-09-12' AND '2015-09-15'
Date literals are tricky... besides the problem with conversion (often depending on your system's culture) you must think of the day's end if you use between!
Here you find a system independent way to literally write dates: https://msdn.microsoft.com/en-us/library/ms187819.aspx?f=255&MSPPError=-2147217396 (look at "ODBC)
So you could write
BETWEEN {d'2015-09-12'} AND {ts'2015-09-12 23:59:59'}
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN CAST('2015-09-12' AS DATE)
AND CAST('2015-09-15' AS DATE)