How to solve 'ERROR: invalid value for "YYYY" in source string' when trying to get previous weeks data? - amazon-redshift

I'm trying to get the last week of data in a pipeline script but when I do this I get the above error.
The code I am using is:
where to_date(start_datetime, 'YYYYMMDDHH24MISS') >= date_trunc('week', current_date) - 7
and to_date(start_datetime, 'YYYYMMDDHH24MISS') < date_trunc('week', current_date)
I think the issue could be that the table doesn't ingest data for about 4 days after the fact (i.e if date is 12/12/22 then the most recent date is 08/12/22)
But I've tried it with getting the week that started 2 weeks ago but still get the same error.
Anyone know how to solve this?
Thanks

The problem is in the function to_date and your wrong input format.
If you have something like this in the start_datetime column 12/12/22, you need to specify another format
to_date('12/12/22', 'DD/MM/YY')
More about formats: https://docs.aws.amazon.com/redshift/latest/dg/r_FORMAT_strings.html

Related

Report Today minus 1 day

need help with this query DB2 IBM
SELECT
ABALPH AS Kunde,
SDLITM AS Artikel,
SDDSC1 AS Beschreibung,
SDSOQS AS Menge, date(digits(decimal(SDIVD+1900000,7,0))) AS Invoice,
decimal(SDUPRC/10000,15,2) AS Einzelpreis,
decimal(SDAEXP/100,15,2) AS Gesamtpreis,
SDDOCO AS Dokument,
AIAC01 AS Region
Now my question is , how can I get the today date minus 1 day ?
Thank you so much
have test it with ADD_DAYS doesn´t work.
I do not see the point in your query where a date is referenced but your query seems incomlete anyways because join contions are missing.
In general you get todays date with current date and you get yesterday with current date - 1 day
Check out this query:
SELECT current date, current date - 1 day
FROM sysibm.sysdummy1
Per the DB2 documentation, ADD_DAYS is valid with negative numbers for previous days. See example 4 on that page. So usage like this should have worked:
ADD_DAYS(DATE(...), -1)
or
ADD_DAYS(CURRENT_DATE, -1)
You should post your specific attempted usage of it and the error that is reported when you try it.

Cognos Where Clause on Date column?

I am a SQL DBA trying to get some information in Cognos.
The column name is [Packingdate] column.
In order to retrieve last 7 days of packing data information I use the below.
where [Packingdate] >_add_days( current_date, -7 ) -This is working fine.
However when i tried to get November month data I tried the below
[Packingdate] between '2021-11-01' and '2021-11-30'
It is giving me the below error
RQP-DEF-0177 An error occurred while performing operation 'sqlPrepareWithOptions' status='-126'.
Ami missing something?
Remove the quotes:
[Packingdate] between 2021-11-01 and 2021-11-30
When dealing with date or datetime data, YYYY-MM-DD (without quotes) is the format Cognos understands.

Convert julian date in Oracle SQL null

I want to convert Julian dates to normal dates (from JDE database). I run this query:
select to_date(to_char((DATE+1900000)),'YYYYDDD') from table ;
And I get the following error:
ORA-01848: day of year must be between 1 and 365 (366 for leap year).
I know it's because some dates are null or have no values.
Can anyone help me out on this please?
I have replicated your query and I tried with different values. Only if the date value is "0", you will get this error:
ORA-01848: "day of year must be between 1 and 365 (366 for leap year)"
So I thought that maybe you were interested in avoid the "0" values, and you can avoid it with a simple where, like this one:
SELECT to_date(to_char((DATE+1900000)),'YYYYDDD')
FROM table
WHERE DATE != 0;

Date Parameter to Cache SQL

I am very new to Cache. I am trying to develop a report with date parameters. When I issue the SQL command:
SELECT TOP 2 ad.admission_date from system.admission ad WHERE convert(sql_date,ad.admission_date) >= convert(sql_date,'08-01-2014' )
I get what I expect two records.
One of which is 10/1/2010 12:00:00 AM.
Then if I issue the command
SELECT TOP 2 ad.admission_date from system.admission ad WHERE convert(sql_date,ad.admission_date) <= convert(sql_date,'08-01-2014' )
I get no values returned?
When I issue the command
SELECT TOP 2 {fn convert('10-03-2010', sql_date) } FROM system.admission_data
I get two NULL values. Clearly I am confused about how Cache works.
I have found that if you use the standard ODBC format (yyyy-MM-dd) for the date you don't need to use the convert and it is much more efficient:
WHERE ad.admission_date <= '2014-08-01'
I formated date incorrectly. I have my code working now. Should look something like select top 2 convert(DATE, '10/03/2010 12:00:00 AM') .... and then I can actually do comparisons.

how to obtain interval from date on postgres in days

I have two columns, say "2010-10-26" and "2010-08-23" and I would like to obtain the interval in days, what is the best way to do this on a select query taking into account that there are months with 30,31,29,28 days? Is there any function that already does this?
Thank you.
If they are already in the date format, it's very easy:
SELECT '10/26/2010'::date - '08/23/2010'::date;
OR:
SELECT date '2001-10-01' - date '2001-09-28'; // outputs integer 3