how to use airflow macros with nodash to suffix to table name - macros

i would like to suffix macros date with nodash to my final table .
I am using the below macro
if sd = 2018-05-09 , {{macros.ds_add(ds, -4)}}
to get the current date - 4 date, getting out put like 2018-05-05. Expected output would be 20180505.
tried
{{{{macros.ds_add(ds, -4)}}_nodash}}
I'm getting the
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'
Assist me to resolve this issue.

You can use airflow.macros.ds_format to format the dates as you want. For example:
airflow.macros.ds_format(airflow.macros.ds_add('2018-05-09',-4),'%Y-%m-%d','%Y%m%d')
More details: http://airflow.incubator.apache.org/code.html?highlight=macro#airflow.macros.ds_format

Related

pySpark output file with timestamp name

I need to name my outputfile with the timestamp but getting an error. Not sure what I'm doing wrong
timestamp = spark.sql("select string(date_format(current_timestamp,'yyyy/MM/dd_HH:mm:ss'))").collect()[0][0]
print(timestamp)
Error: ADLException: Error getting info for file
/06/05_13:14:01
no error if I use current date instead of timestamp. But I need timestamp
some caracters are not allowed in file naming:
#L1234_ABC123_2020/06/05_13:14:01 is not vaide. Try something like #L1234_ABC123_20200605_131401 for example, or with underscore _. colons : are not allowed basically.

What's the best way to parse an interval from a string?

What's the most direct way get an interval from a string (varchar) value in postgres?
I have a column that contains JSON data. One of the JSON properties is a "runtime" value that is formatted like: "runtime":"03s.684". Using the JSON functions I'll able to get to just the runtime value with no problem, but then I've got the quoted string value.
I was looking for something similar to to_timestamp() so I could parse the string doing something like:
select to_interval('"03s.684"', '"SS\s.MS"'); --would like to do / this function doesn't exist
or something like this would be nice too:
select to_timestamp('"03s.684"', '"SS\s.MS"') :: interval; --also not valid
One approach I can see that should work is to translate the string into a format that can be cast to interval using regexp_replace() but that doesn't seem like the best approach. What's the recommended way of getting an interval from a custom-formatted time string?
You can use to_number()
SELECT to_number('3s.684', '999D9099') * '1 second'::interval;

Error on converting String to Date

My date data looks like this: 20151112,20151116 .I want to convert it into Date "MM/DD/YYYY", then i used the Code:
=CDATE(MID(Fields!Date.Value,5,2)&"/"&(RIGHT(Fields!Date.Value,2)&"/"&LEFT(Fields!Date.Value,4)))
But there is an Error: Conversion from String 11/24/2015 to type Date is invalid. Can you please help me how to fix this Problem?
You are making your life harder than it needs to be by converting your string date into a potentially ambiguous localised date format (MM/dd/yyyy) and then trying to actually convert it to a date datatype. If you go about this the other way around - converting your data to a date data type and then formatting it - you will avoid a lot of headaches.
To do this, you need to first add in some forward slashes / to your string dates, retaining the yyyy/MM/dd:
=CDate(left("20160131",4) & "/" & mid("20160131",5,2) & "/" & right("20160131",2))
Now that you have a date data type that will always be properly handled by SSRS, you can format it however you require for being displayed in your application:
=format(CDate(left("20160131",4) & "/" & mid("20160131",5,2) & "/" & right("20160131",2)),"MM/dd/yyyy")

Date display Appery.io

I use a date picker that saves the date in a yyyy-mm-dd format in a database. It then automatically adds time that always appears as 00:00:00. So for example it displays the date like this : 2014-12-14 00:00:00. I want it to display just the date in a mm-dd-yyyy format.
I use the code below but something seems to be wrong with it because it simply doesn't change the way it is displayed. I want to split up each of the values, then display only the date in a different format.
var parts = value.split("/");
return parts[1] + "-" +parts[2] + "-" +parts[0];
What can I do to make it work? Javascript please.
Thanks in advance.
It's a wide variety of solutions. Depends on you server-side settings too, but I will assume, that you use common for most websites MySQL DB and PHP.
You can change you column type to DATE. As said in docs:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
You can change your column type to VARCHAR and store a date in any format you like when INSERT it
You can convert it to proper format using MySQL built-in DATE_FORMAT()
SELECT DATE_FORMAT('2014-12-14 00:00:00', '%c-%e-%Y');
will give you 12-14-2014. Here is sqlfiddle,
just change first param to your column name in query
On server you can use PHP's date() function like this
$yourdate = '2014-12-14 00:00:00';
echo date("m-d-Y", strtotime($yourdate));
Closer to your question, how to do it in JS? It also has special object for this: Date(). Put your date in constructor, then use methods:
var yourdate = new Date('2014-12-14 00:00:00');
alert(yourdate.getMonth() + "-" + yourdate.getDate() + "-" + yourdate.getFullYear());
JSFiddle < - here
So, as far as almost every platform understands this datetime format - you can use any tool for converting it

Convertion parameter numeric month to title month

I have month parameter in JasperReports report which will be changed into title of month. I didn't use any query to change them. I try this code:
$P{MONTH}.intValue()==1?"JAN":
$P{MONTH}.intValue()==2?"FEB":
$P{MONTH}.intValue()==3?"MAR":
$P{MONTH}.intValue()==4?"APR":
$P{MONTH}.intValue()==5?"MAY":
$P{MONTH}.intValue()==6?"JUN":
$P{MONTH}.intValue()==7?"JUL":
$P{MONTH}.intValue()==8?"AUG":
$P{MONTH}.intValue()==9?"SEP":
$P{MONTH}.intValue()==10?"OCT":
$P{MONTH}.intValue()==11?"NOV":"DEC";
but it didn't work. Could anyone know the solution for me?
This is what I use
$F{MONTH}.intValue()==1?"JAN":
$F{MONTH}.intValue()==2?"FEB":
$F{MONTH}.intValue()==3?"MAR":
$F{MONTH}.intValue()==4?"APR":
$F{MONTH}.intValue()==5?"MAY":
$F{MONTH}.intValue()==6?"JUN":
$F{MONTH}.intValue()==7?"JUL":
$F{MONTH}.intValue()==8?"AUG":
$F{MONTH}.intValue()==9?"SEP":
$F{MONTH}.intValue()==10?"OCT":
$F{MONTH}.intValue()==11?"NOV":"DEC"
It may be the semi-colon at the end that's the problem
It may also be that you are using a Parameter - .intValue() doesn't seem valid for a Parameter