Why does DateDiff function fail with "Invalid operation: Data value "0" has invalid format"in Redshift - amazon-redshift

I have a datediff() function that throws an exception.
I am trying to calculate the number of days between two dates. The rub is that one date is a converted integer value in YYYYMMDD format and the second date field is a timestamp. So, in the snippet below I am doing what I think are the correct conversions. Sometimes, it runs actually.
The message I get is: Amazon Invalid operation: Data value "0" has invalid format.
select site, datediff(days,to_date(cast(posting_dt_sk as varchar), 'YYYYMMDD'),trunc(ship_dt)) days_to_ship from sales_table
Later I added a Where-clause as to ignore empty values thinking I had bad data but that's not it. I still get the message.
where posting_dt_sk is not null and posting_dt_sk > 0
It all looks right to me, but it fails.

Related

Date CAST in SQL Server throwing conversion failed for a date

I am experiencing a very unique cast error I don't understand why it happens with some dates and only in one particular case.
First at all, I cannot change the current code, it's a dynamic query from a legacy application and it's the result of queries to different tables to assemble the query I am having troubles with.
The error is a classic 'conversion failed when converting date and/or time from character string'.
At the beginning I thought it was a classic file naming error, we obtain the date from the file name in the format YYYYMMDD, the file has prefix and suffix and it's always formatted like that. It was pretty common to get wrongly formatted dates but it doesn't happen anymore. The issue is interesting because it only happens in 1 case for some dates that do not look like errors, for example, 20201105 which is basically translated to 11/05/2020 (US Format with month first).
This is the query:
SELECT TOP 1 CAST(LEFT(REPLACE(FileName, 'XXYYY_,''),8) AS DATE) AS MyDate FROM Mytable
The file name in this case is XXYYY_20201105.txt
Why the top 1? Well, it is a very bad design, there are many rows with the same value and it has to take only one to determine the date.
The most interesting part of it, when it fails I can "fix" the error just adding one more column:
SELECT TOP 1 CAST(LEFT(REPLACE(FileName, 'XXYYY_,''),8) AS DATE) AS MyDate, AnotherColumn
FROM Mytable
This query, just adding a column, doesn't fail. That's the weirdest part. I am trying wrap my head around what is the difference between obtaining ONE column and TWO columns. When I add any other column it seems to make the issue disappear.
Thanks a lot.

postgreSQL increment number in output

I am extracting three values (server, region, max(date)) from my postgresql> But I want to extract an additional 4th field which should be the numerical addition of 1 to 3rd field. I am unable to use date add function as in the database date field is defined as an integer.
date type in DB
date|integer|not null
tried using cast and date add function
MAX(s.date)::date + cast('1 day' as interval)
Error Received
ERROR: cannot cast type integer to date
Required output
select server, region, max(alarm_date), next date from table .....
testserver, europe, 20190901, 20190902
testserver2, europe, 20191001, 20191002
next date value should be the addition to alarm_date
To convert an integer like 20190901 to a date, use something like
to_date(CAST(s.date AS text), 'YYYYMMDD')
It is a bad idea to store dates as integers like that. Using the date data type will prevent corrupted data from entering the database, and it will make all operations natural.
First solution that came to my mind:
select (20190901::varchar)::date + 1
Which output 2019-09-02 as type date.
Other solutions can be found here.

Redshift - Weird behaviour from TO_TIMESTAMP function

I'm trying to create a timestamp from separated date and time fields. The date field is of 'date' type while the time field is 'varchar'. The table has just over 60k records. Date is in YYYY-MM-DD format, ie 2017-07-25. Time is in HH:MIpm format, ie 4:25pm.
I run the following query to try and create the timestamp:
select *,
to_timestamp(TO_CHAR("date", 'YYYY-MM-DD')||' '||(case when "time"='' then '11:59pm' else "time" end), 'YYYY-MM-DD HH:MIpm') as REQUIRED_TIMESTAMP
from TABLE;
However, it only returns 1023 records. Here's what makes things even weirder:
i. The records that are returned aren't always the same on subsequent runs. However, there is always 1023 records returned. On one run the first few IDs would be 43, 55, 63, 69, etc. and on the next run it'd be different. Sometimes I'd get the following error:
Query 1 ERROR: ERROR: invalid value for "HH" in source string
ii. I used CAST(as timestamp) instead, it'd show the timestamp in a different format but again it'd only show 1023 records.
iii. It's not a specific time value that is omitted. A record with time 7:30pm may be omitted, but another record with 7:30pm shows up just fine.
Is there something wrong with my query? Could it be a problem with the data or is this an issue with Redshift?

PostgreSQL Timestampz BIRT Params

I am having an issue with adding a timestamp with timezone, aka timestampz, as a parameter in BIRT for querying postgreSQL.
With no param in BIRT the following works fine:
WHERE start_date = '2016-02-02 03:26:30.000Z'
Now with a param in BIRT, I can't get any combination of a DateTime to work:
WHERE start_date = ?
Next I make the param which fails as DateTime data type and as string data type:
In the param editor, I've also tried the following in default value expression:
BirtDateTime.now()
and
new Date().toISOString().replace("T"," ")
I always get an error saying:
ERROR: operator does not exist: timestamp with time zone = character varying
Hint: No operator matches the given name and argument type(s).
You might need to add explicit type casts.
I even tried casting like so:
WHERE start_date = ?::timestampz
BIRT just tells me the syntax is not supported, which means it is just not supported on a parameter.
On another note, how can you debug BIRT queries? I would like to see what the startDate param is right before it is added to the query.
In the mean time I'm going to have to take the "hacky" route. :(
WHERE extract(year from start_date) = ?
AND extract(month from start_date) = ?
AND extract(day from start_date) = ?
----- EDIT -----
Dang! 8 months later and no response. Still haven't figured this out. Just been using the hacky route till this day. I guess just not enough people use BIRT and Postgres :(
With BIRT 4.6.0, PostgreSQL 9.1.7 and PostgreSQL JDBC driver 9.2.1002, I had success with the parameter having "Data Type" equal to "String" in the "Edit Parameter" dialog for the Data Set, and an SQL query clause like this:
WHERE some_field >= TO_DATE(?, 'YYYYMMDD')
The string parameter has to be specified in the provided format.
This seems to still be a bit hacky, but not as ugly.
Report Parameters
Although the question didn't ask about this type of parameter, I noticed that somehow the above works with them, although I don't understand why.
When I made a Report Parameter of type "Date" and set the data source for a chart to that data set and passed through the Report Parameter as params["some_param"].value, it somehow ended up working. I don't understand why, as params["some_param"] is apparently of type java.sql.Date (according to an error message I received when playing with this) and it doesn't seem to stringify in "YYYYMMDD" format.
It also worked when I changed to TO_DATE(?, 'YYYY-MM-DD').

SSRS expression fails for IIF date with #error

A stored procedure is included in a report builder file of type rdl. the stored procedure has many fields. One of the fields returns a date (not datetime). The desired outcome in the report is to show the date only and empty field if no date is returned.
With just the field value the result shows empty field when the date is null otherwise shows the date with a datetime value.
Using IIF to check the value for 'nothing' as shown below.
=IIF(Fields!myDate.Value Is Nothing,"",Fields!myDate.Value)
The output is the same. A datetime value is shown when date is available.
Attempting to use the shortDateString() function yields the correct result in when a date is present but #Error when a date is NOT present. This is the statement:
=IIF(Fields!rlsPromoDate.Value Is Nothing, "",
Fields!rlsPromoDate.Value.ToShortDateString())
The version below was attempted. No errors resulted however the date was not returned but this was "Microsoft.ReportingServices.ReportProcessing.OnDemandReportObjectModel.FieldImpl".
=IIF(Fields!myDate.Value Is Nothing,"", String.Format("{0:MM/dd/yyyy}",
Fields!myDate))
Please advise if there is a solution.
Try the following:
=IIF(Fields!rlsPromoDate.Value Is Nothing, "", Format(Fields!rlsPromoDate.Value, "dd/MM/yyyy"))
or whatever date format you'd actually like to use.