Teradata Failing to Convert Date String to a Numeric value - teradata

I searched stackoverflow and on the internet but I could not find the exact answer. You can see my query below. I cannot figure out why I am getting the error message A character string failed conversion to a numeric value. I came to the conclusion it is that line of code because when I comment it out I don't get that error message but a different one. What I get instead is no more spool space which to me would make sense.
SELECT a.store_nbr AS club_number, a.visit_date, count(transaction_id)
FROM table_one
WHERE division = 1
AND a.visit_date BETWEEN '2020-01-01' AND '2020-02-01

Related

Bigquery String to date invalid, safe.parse returns null #standardSQL

I am trying to convert a date value ingested from SFMC as a string. Attempts and results below:
PARSE_DATE('%m/%d/%Y', '6/22/2017’) returns “invalid date”
2.safe.PARSE_DATE('%m-%d-%Y',Timesent) returns null results
Any insight would be greatly appreciated.
I wondered if it was because the day of month had no leading zero, because according to the docs, %d means the day of the month as a decimal number (01-31).
But your first example worked OK for me in a BigQuery SQL workspace anyway as per below. Your second example has hyphens rather than slashes in the format mask, and BigQuery returns a mismatch error for me.
Are you certain about the date value which is failing for you?

Why does read.csv2 work just fine, yet read.csv2.sql shows an error/warning?

I am trying to read a csv file in R using read.csv2.sql, since I would like to use a SELECT query from SQL to help me filter my data, but before I can even get to my SELECT query, I discovered that simply reading my csv file using read.csv2.sql already generates a warning message.
This is my code:
investment2 <- read.csv2.sql("investmentdata.csv")
This is the warning message:
Warning message:
In result_fetch(res#ptr, n = n) :
Column 'Capital.Investment': mixed type, first seen values of type real, coercing other values of type string
However, when I use the normal read.csv2 function, there is no error. In particular, the following code works fine with no warning messages:
investment <- read.csv2("investmentdata.csv")
Next, I tried to resolve this issue by casting the Capital.Investment column to be real as follows:
investment3 <- read.csv2.sql("investmentdata.csv", "SELECT *, CAST(Capital.Investment AS real) FROM file")
However, R now generates the following error:
Error: no such column: Capital.Investment
Thus, I have two questions. Firstly, why does using read.csv2.sql generate that warning message when read.csv2 works just fine? Secondly, why does R (or SQL) not recognise my Capital.Investment column when I try to cast it as real?
Perhaps it is also worth noting that I cannot simply ignore this warning that the read.csv2.sql function is showing, because I discovered that as a consequence of this warning, it has automatically casted some of the NA rows in my Capital.Investment column to 0, which I cannot allow - the NA rows must stay as NA. I do not seem to be having this problem with the other columns of my csv file though.
As I am quite new to R, any help and explanations will be greatly appreciated :)
Edit
The coded version of what my truncated csv file looks like is as follows. In particular, the name of the column-in-question is indeed Capital.Investment.
id;targetC;year;comp_id;homeC;Industry.Activity;Capital.Investment;Estimated;Jobs.Created;Estimated.1;Project.Type;geographic distance;SIC;listed;sales;assets;cap_structure;rnd;profit;rndintensity;polcon;homeC_gdp;targetC_gdp;homeC_gdppc;targetC_gdppc
1302;AUS;2008;FR338966385;FRA;Design, Development & Testing;33.1;Yes;36;Yes;New;15.26414042;3669;Unlisted;4333088.972;4037211.732;0;NA;-1339221.733;NA;0.489032525;2.92347E+12;1.05456E+12;45413.06571;49628.11513
1311;AUS;2008;US*190521496652;USA;Research & Development;8.4;Yes;30;No;New;15.24712914;NA;Unlisted;NA;NA;NA;NA;NA;NA;0.489032525;1.47E+13;1.05456E+12;48401.42734;49628.11513
1313;AUS;2008;GB05817296;GBR;Business Services;9.7;Yes;10;Yes;New;15.31094496;7389;Unlisted;NA;87.64187374;NA;NA;NA;NA;0.489032525;2.87546E+12;1.05456E+12;46523.26545;49628.11513
1318;AUS;2008;US129687150L;USA;Business Services;1.3;Yes;225;Yes;New;15.24712914;7373;Unlisted;NA;NA;NA;NA;NA;NA;0.489032525;1.47E+13;1.05456E+12;48401.42734;49628.11513
1351;AUS;2008;GB*P0060071;GBR;Electricity;516;No;51;Yes;New;15.31094496;NA;Unlisted;NA;NA;NA;NA;NA;NA;0.489032525;2.87546E+12;1.05456E+12;46523.26545;49628.11513
9925;AUS;2008;GB00034121;GBR;Business Services;34.8;Yes;37;Yes;New;15.31094496;4412;Unlisted;NA;2079288.611;0.355157008;NA;94320.15469;NA;0.489032525;2.87546E+12;1.05456E+12;46523.26545;49628.11513
9932;AUS;2008;CA30060NC;CAN;Sales, Marketing & Support;3.2;Yes;11;Yes;New;14.88812529;1094;Listed;NA;NA;NA;NA;NA;NA;0.489032525;1.54913E+12;1.05456E+12;46596.33599;49628.11513
9935;AUS;2008;US940890210;USA;Manufacturing;771;Yes;266;Yes;New;15.24712914;2911;Listed;NA;NA;NA;NA;NA;NA;0.489032525;1.47E+13;1.05456E+12;48401.42734;49628.11513
9938;AUS;2008;US770059951;USA;Technical Support Centre;9.1;Yes;104;Yes;Co-Locati;15.24712914;3661;Listed;34922000;53340000;0.120134983;4598000;7333000;0.086201723;0.489032525;1.47E+13;1.05456E+12;48401.42734;49628.11513
9946;AUS;2008;US010562944;USA;Extraction;535.8;Yes;198;Yes;New;15.24712914;2911;Listed;NA;NA;NA;NA;NA;NA;0.489032525;1.47E+13;1.05456E+12;48401.42734;49628.11513
9955;AUS;2008;DE5030147191;DEU;Logistics, Distribution & Transportation;21.2;Yes;134;Yes;New;14.6718338;4311;Listed;93495971.01;346629334.8;0.036629492;0;2044745.934;0;0.489032525;3.75237E+12;1.05456E+12;45699.19832;49628.11513
9958;AUS;2008;US126012192L;USA;Business Services;9.7;Yes;10;Yes;New;15.24712914;8111;Unlisted;NA;NA;NA;NA;NA;NA;0.489032525;1.47E+13;1.05456E+12;48401.42734;49628.11513
9969;AUS;2008;US135409005;USA;Extraction;NA;No;538;Yes;New;15.24712914;2911;Listed;NA;NA;NA;NA;NA;NA;0.489032525;1.47E+13;1.05456E+12;48401.42734;49628.11513
9977;AUS;2008;JP000000728JPN;JPN;ICT & Internet Infrastructure;128.6;Yes;77;Yes;New;7.0333688;3571;Listed;53255396.85;38181450.16;0.190244908;2584585.523;480589.4308;0.067692176;0.489032525;5.03791E+12;1.05456E+12;39339.29757;49628.11513
9984;AUS;2008;US841547578;USA;Sales, Marketing & Support;13.6;Yes;23;Yes;New;15.24712914;2095;Listed;NA;NA;NA;NA;NA;NA;0.489032525;1.47E+13;1.05456E+12;48401.42734;49628.11513
9993;AUS;2008;US258715604L;USA;Customer Contact Centre;1.8;No;40;No;New;15.24712914;NA;Unlisted;NA;NA;NA;NA;NA;NA;0.489032525;1.47E+13;1.05456E+12;48401.42734;49628.11513
This issue was resolved in chat, to be one of two issues:
see my original answer below, this was causing an Error; when that is fixed, we see that ...
there is a warning, informing about the fact that a column (happens to be the same column) looks numeric but has a non-numeric cell somewhere within the guts of the file.
The first is resolved below, the second is just a warning.
However, because the OP is asking to convert to numeric via SQL, the NA is converted to 0, which is not good. My recommendation is to either cast([Capital.Investment] as char) as [Capital.Investment] and use R's as.numeric to convert to numeric (preserving the NA-nature), or to just read.csv2(.) the file outright and use sqldf(.) to use its SQL querying on table-like data.
Up front: add brackets or quotes around your column name.
Rationale: Capital.Investment is seen as a dot-delimited table-column or schema-table or something similarly not what you intend. I believe in general in SQL that field names with embedded dots need this escaping. If your data has an embedded space, realize that R does not like spaces in its field names, so it is by-default using make.names when reading it in (which replaces spaces with dots).
Setup:
Save the following as "quux.csv". (I've named it csv, but since I'm changing it to be ;-delimited, it behaves the same.)
quux;Capital.Investment
1;100
2;200
(Or you can use Capital Investment, it's the same thing.)
sqldf::read.csv2.sql("quux.csv", sql='select quux, cast(Capital.Investment as real) from file')
# Error: no such column: Capital.Investment
sqldf::read.csv2.sql("quux.csv", sql='select quux, cast([Capital.Investment] as real) as CI from file')
# quux CI
# 1 1 100
# 2 2 200
sqldf::read.csv2.sql("quux.csv", sql='select quux, cast("Capital.Investment" as real) as CI from file')
# quux CI
# 1 1 100
# 2 2 200

using BDP in R for finding SETTLE_DTE (works in excel but not in R)

I need to get the settlement date of forward contracts in R.
I can do it in excel as below:
BDP("CAD1M CURNCY","SETTLE_DT","REFERENCE_DATE",TEXT(C2,"YYYYMMDD"))
(assume my specific date for which I am trying to find the settlement date on a 1 month forward contract is at cell C2)
this gives me the settlement date for the forward contract above.
So I want to do the same thing in R (I am connected to bloomberg and can get data)
I tried the followings (and many other versions):
bdp("CAD1M Curncy",c("SETTLE_DT","REFERENCE_DATE",as.Date("2018-11-15")))
but does not work.
Seems like SETTLE_DT is not recognized but I do not know how to make it recognized for bdp in R.
Here are the error messages I get
bdh("CAD1M Curncy","SETTLE_DT",REFERENCE_DATE = as.Date("20181115"),options=NULL)
Error in bdh("CAD1M Curncy", "SETTLE_DT", REFERENCE_DATE = as.Date("20181115"), : unused argument (REFERENCE_DATE = as.Date("20181115"))
bdh("CAD1M Curncy","SETTLE_DT","REFERENCE_DATE" = as.Date("20181115"),options=NULL)
Error in bdh("CAD1M Curncy", "SETTLE_DT", REFERENCE_DATE = as.Date("20181115"), : unused argument (REFERENCE_DATE = as.Date("20181115"))
bdh("CAD1M Curncy","SETTLE_DT","REFERENCE_DATE = as.Date("20181115")",options=NULL)
Error: unexpected numeric constant in "bdh("CAD1M Curncy","SETTLE_DT","REFERENCE_DATE = as.Date("20181115"
bdp("CAD1M Curncy","SETTLE_DT","REFERENCE_DATE = as.Date("20181115")",options=NULL)
Error: unexpected numeric constant in "bdp("CAD1M Curncy","SETTLE_DT","REFERENCE_DATE = as.Date("20181115"
bdp("CAD1M Curncy","SETTLE_DT",as.Date("20181115"),options=NULL)
Error in charToDate(x) : character string is not in a standard unambiguous format
bdp("CAD1M Curncy","SETTLE_DT",as.Date("2018-11-15"),options=NULL)
Error in bdp_Impl(con, securities, fields, options, overrides, verbose, : Request overrides must be named.
bdp("CAD1M Curncy","SETTLE_DT","REFERENCE_DATe",as.Date("2018-11-15"),options=NULL)
Error in bdp_Impl(con, securities, fields, options, overrides, verbose, : Request overrides must be named.
bdp("CAD1M Curncy","SETTLE_DT",c("REFERENCE_DATe"=as.Date("2018-11-15")),options=NULL) SETTLE_DT CAD1M Curncy 2018-12-31
also if I change the date above to "20181115" instead, it will give me
Error in charToDate(x) : character string is not in a standard unambiguous format
also with so the last one is the only one which gives me some answer but it is basically only giving me the settlement date of the last forward contract (for today) and does not take into account my reference date. In excel the following command works and gives me the right output: =BDP("CAD1M Curncy","SETTLE_DT","REFERENCE_DATE","20181115") output: 2018/12/17
Also tried:
bdp("CAD1M Curncy","SETTLE_DT",c("REFERENCE_DATe",as.Date("2018-11-15")),options=NULL)
Error in bdp_Impl(con, securities, fields, options, overrides, verbose, : Request overrides must be named.
Any ideas?
Thank you guys really appreciate it
bdp("CAD1M Curncy", "SETTLE_DT", overrides = c("REFERENCE_DATE"="20181115"))
solved the problem.
Thanks Assylias.
You are using different date format between Excel and R, which is a possible cause. I'd suggest to use ...as.Date("20181115") in R, as well. Also, it would be helpful to share the entire error message.

ReportBuilder embedded FormatDateTime errors in switch comparator

I have a datetime field that may be blank. when it is not blank I want to show only the date portion. I used:
=FormatDateTime(Fields!myDate.Value, DateFormat.ShortDate)
This works except for blank entries which are replaced by "1/1/0001".
I used a switch statement but it returns an error when the condition is met. The switch statement is:
=switch(Fields!myDate.Value<>"", FormatDateTime(Fields!myDate.Value, DateFormat.ShortDate))
This works for the blanks but yields an error for datetime entries.
The workaround is to change the stored procedure to return only the date for the datetime field. I would prefer to have the time value in case it is needed in the future.
Please let me know what I'm doing wrong or if there is a report builder solution that works.
=IIF(Fields!myDate.Value Is Nothing, "", Format(Fields!myDate.Value,
"dd/MM/yyyy"))
Please mark as duplicate. This is a duplicate answer of my similar question (with Answer by Jeffrey Van Laethem) "SSRS expression fails for IIF date with #error"

SSIS datetime trucation error

I'm using following expression in Derived Column Component to get hour and minute from datetime field. e.g. Get "12:13" from "2012-08-14 12:13:03.650"
LKPStartTime:
(DT_STR,5,1252)SUBSTRING((DT_STR,10,1252)(DT_DBTIME)StartTime,1,5)
However, it pops up error message of
"Derived Column" failed because truncation occurred, and the
truncation row disposition on output column "LKPStartTime" specifies
failure on truncation. "
Is there any problem of my expression? Thanks for any answers!
I think it should be 12,5 instead of 1,5

Resources