How to use a date range in a Cognos SQL Prompt Query - macros

I'm using a prompt query in Cognos to allow users to filter based on the past X number of days from the current date. How do I format the query for Teradata?
I've tried with and without "#sq" in my code and with and without "integer"as the format. When I hard code a number it works as well (i.e day_date between (current_date - interval '6' day) and current_date).
day_date between current_date - interval (#sq(prompt('Select_number_of_days', 'integer'))# )
day and current_date
The error message I receive is:
Syntax error, expected something like a string or a Unicode character literal between the 'interval' keyword and '('.

You could try: [day_date] between current_date and _add_days(current_date, -1* ?DaysBack?). Be sure to check the native SQL to ensure the current_date is being passed down to the database. If it's not you can replace current_date with: #timeStampMask($current_timestamp,'YYYY')#

Related

Checking if today's date is in between the given start date and end date

The code is written in postgresql and run in pgadmin 4.
I am trying to get the id from the pricing table if the current date lies in between the given start date and end date.
I am trying to get the id from the pricing table if the current date lies in between the given start date and end date.
select id
from pricing
where current_Date between start_date>=date '2022-10-19' AND end_date<=date '2022-10-20';
The error raises
operator does not exist: date >= boolean LINE 3: where
current_Date between start_date>=date '2022-10-19'...
Your where condition is essentially someDate between someBoolean and someOtherBoolean, which confuses compiler. Use just this:
where current_Date between date '2022-10-19' AND date '2022-10-20'
between only expects the values:
select id
from pricing
where current_Date between '2022-10-19' AND '2022-10-20';

How to fix inconsistent datatypes:expected NUMBER got DATE in Oracle

I am receiving an error on a query that I am running. My error is 'inconsistent datatypes:expected NUMBER got DATE'
I am able to successfully do it in SQL Server so I looked up how to update the query for Oracle syntax and I am running into issues.
I have tried TO_DATE of a date field minus TO_DATE of the SYSDATE and then tried to divide that by 365 for the number of days in a year.
SELECT min.Contact,
min.MIN_DATE,
COUNT(giv.ID) AS COUNT,
SYSDATE,
TO_DATE(min.MIN_DATE,'YYYY-MM-DD')-TO_DATE(SYSDATE,'YYYY-MM-DD') AS DATEDIFF,
COUNT(giv.ID)/(TO_DATE(min.MIN_DATE,'YYYY-MM-DD')-TO_DATE(SYSDATE,'YYYY-MM-DD')/365)
FROM giv JOIN min
ON giv.Contact=min.Contact
GROUP BY min.Contact,min.MIN_DATE;
It's the parenthesis. Division takes precedence over subtraction. Try this:
COUNT(giv.ID)/((TO_DATE(min.MIN_DATE,'YYYY-MM-DD')-TO_DATE(SYSDATE,'YYYY-MM-DD'))/365)

Difference between two timestamps as timestamp across multiple days

I have two timestamps and I would like to have a result with the difference between them. I found a similar question asked here but I have noticed that:
select
to_char(column1::timestamp - column2::timestamp, 'HH:MS:SS')
from
table
Gives me an incorrect return if these timestamps cross multiple days. I know that I can use EPOCH to work out the number of hours/days/minutes/seconds etc but my use case requires the result as a timestamp (or a string...anything not an interval!).
In the case of multiple days I would like to continue counting the hours, even if it should go past 24. This would allow results like:
36:55:01
I'd use the built-in date_part function (as previously described in an older thread: How to convert an interval like "1 day 01:30:00" into "25:30:00"?) but finally cast the result to the type you desire:
SELECT
from_date,
to_date,
to_date - from_date as date_diff_interval,
(date_part('epoch', to_date - from_date) * INTERVAL '1 second')::text as date_diff_text
from (
(select
'2018-01-01 04:03:06'::timestamp as from_date,
'2018-01-02 16:58:07'::timestamp as to_date)
) as dates;
This results in the following:
I'm currently unaware of any way to convert this interval into a timestamp and also not sure whether there is a use for it. You're still dealing with an interval and you'd need a point of reference in time to transform that interval into an actual timestamp.

How to select 24 hours old records using Postgres?

I am using this query
select * from table_nm where table_nm_date > NOW() - INTERVAL '24 hour'
But giving today's records too. Please help me.
Output : "2016-03-20 19:31:11.896159",
"2016-03-21 08:24:58.223245",
"2016-03-21 09:13:59.768953",
"2016-03-21 09:51:25.161428",
"2016-03-21 11:35:07.378706"
I only want 2016-03-20 data.
If you want yesterday's data, filter for date only:
SELECT *
FROM table_nm
WHERE table_nm_date BETWEEN CURRENT_DATE - 1 AND CURRENT_DATE
(which is an index-friendly variant of:)
WHERE table_nm_date::date = CURRENT_DATE - 1
Assuming table_nm_date is a usual date-time like data type
then your query turns down to select "any entry from the last 24 hours"
If you want to exclude "todays" records you need to exclude those in an appropriate way, e.g by using table_nm_date between START_OF_WINDOW and END_OF WINDOW, setting both borders as it suits your needs.

SAP HANA- working with dates

How can I compare current date with stored date in SAP HANA?
When I am using dateFrom > CURRENT_DATE in where clause it gives following error:
CURRENT_DATE is reserved SQL keyword and cannot be used
dateFrom is of type UTCTimestamp.
First check the type of field, if is char, you need to convert this to DATE content, using TO_DATE(YOUR FIELD, MASK),
Then you can use NOW(), to get the current time and date from server.