how to convert a string to date format using dataweave - mule

I am performing a CSV to CSV transformation using DataWeave.
One of the Input fields is a string 13/01/2015. My requirement is to convert this string to a date format as 13-Jan-2015.
I have tried using as :string{"dd/MMM/yyyy} and as :date{format:dd/M/yyyy} functions but did not succeed in changing the format.
Here is what I tried:
payload map {
"Order Number":$[0],
"Order Date": ($[1] as :date{format:"d/M/yyyy"}),
}
This conversion gave the output as
Order Number,Order Date
14710655,2015-08-17
Then I tried the following:
payload map {
"Order Number":$[0],
"Order Date": ($[1] as :date{format:"d/M/yyyy"}) as :string{format:"d/MMM/yyyy"})
}
This conversion gave the output as
Order Number,Order Date
14710655,17/8/2015

When I tried :string { format: "dd-MMM-YYYY"}, it gave me 13-Jan-2015.

Related

I am getting "CREATE MODEL OPTIONS() format parameter is missing" error in BigQuery while creating a model?

I am getting this error "CREATE MODEL OPTIONS() format parameter is missing" while trying to create an ARIMA model and it seems to be telling me that I need to define a certain format parameter, but I don't understand which one exactly it is asking me to add.
I am using the following script:
CREATE MODEL forecast
OPTIONS (model_type = 'ARIMA_PLUS',
time_series_timestamp_col='day',
time_series_data_col='cost',
auto_arima = TRUE,
data_frequency = 'AUTO_FREQUENCY',
decompose_time_series = TRUE) AS
SELECT
FORMAT_DATE('%Y-%m-%d', date) as day,
sum(net_cost) as cost
FROM ads_mif.logs_actual_footprint_cost_daily_raw
GROUP BY 1
'time_series_timestamp_col' must have a type of Timestamp, Date or DateTime, but instead has STRING type in query statement.
Remove the formatting of the date column.

Prisma queryRaw returning date as string

Issue: When I use prisma.$queryRaw, it returns my date as a string, even though I specify the query's return type. If I use prisma.find then it returns it correctly. But, I have to use queryRaw because of the complexity of the query.
schema.prisma has the date defined like such:
effectiveDate DateTime? #map("effective_date") #db.Date
So, the model object has the field defined like effectiveDate: Date | null
The query looks something like this:
const catalogCourses: CatalogCourse[] = await prisma.$queryRaw<CatalogCourse[]>`
SELECT
id,
campus,
effective_date as "effectiveDate",
...rest of the query ommitted here because it's not important
If I then do something like
console.log(`typeof date: ${typeof catalogCourses[0].effectiveDate}, value ${catalogCourses[0].effectiveDate}`)
The result shows typeof date: string, value 2000-12-31. Why isn't it a date? I need to be able to work with it as a Date, but if I do effectiveDate.getTime() for example, it errors during runtime, saying 'getTime is not a function', which it is doc. If I try and do new Date(effectiveDate), that doesn't work either because typescript sees the field as a Date object already. EDIT: I was incorrect about why the previous statement wasn't working; doing new Date(effectiveDate) does work.
I do see in the prisma docs that it says:
Type caveats when using raw SQL When you type the results of
$queryRaw, the raw data does not always match the suggested TypeScript
type.
Is there a way for queryRaw to return my date as a Date object?

Query items of a collection before a certain date on Fauna Db?

I looked on google and tried different thing but I cannot figure out how to query all the item of a collection that have the key dueDate before the a certain Date.
On Mysql I would do something like :
select * from table_name where dueDate < "2001-01-01 00:00:00"
That query on mysql would return the items with the Date Inferior to 2001-01-01
I tried to do use that query on Fauna :
q.Map(
q.Paginate(
q.Match(q.Index(indexesQuery1)),
{ before: Date('2021-01-15T17:34:00+08:00')
} ),
q.Lambda("X", q.Get(q.Var("X"))) ) )
indexQuery1 is : getNewWordDemoIso(dueDate: Date!)
But It returns an empty Array,
Also I saved all my Date all Fauna the date format called : iso 8601
Also Im using javascript
Any ideas !?
thanks !!
When you perform an "Create", you need parse your ISOString Date for an FaunaDB Date format.
You migth need something like this:
const newTransaction = await fauna.query<TransactionObject>(
q.Create(q.Collection("transactions"), {
data: {
...transaction,
firstDueDate: q.Date(transaction.firstDueDate),
lastDueDate: q.Date(transaction.lastDueDate),
createdAt: q.ToDate(q.Now())
}
})
);
This will work for you as you said you are using JS/TS.
ps: (import {query as q} from 'faunadb')

To remove double quotes from date string in SQL

I am using JSON_EXTRACT() to retrieve the date from json.
I want to get the date without double quotes.
Here is the example of what I am doing :
JSON_EXTRACT(JSON_EXTRACT(events, "$.my_member"), "$.my_Number") as xyz
my_number holds date string as "2016-01-01 11:31:25", I want this without the double quotes.
I tried using timestamp as :
timestamp(JSON_EXTRACT(JSON_EXTRACT(events, "$.my_member"), "$.my_Number"))
but it is returning a null value to xyz.
Thanks.
Try
JSON_EXTRACT_SCALAR(JSON_EXTRACT(events, "$.my_member"), "$.my_Number")
Also, you should be able to further "optimize" your expression by building proper JSON Path and using JSON function only ones. See "hint" below
SELECT
JSON_EXTRACT_SCALAR(
'{"my_member":{"my_Number":"2016-01-01 11:31:25"}}',
"$.my_member.my_Number"
)
See more details and also difference between JSON_EXTRACT_SCALAR and JSON_EXTRACT at JSON functions
Run REPLACE
REPLACE(JSON_EXTRACT(JSON_EXTRACT(events, "$.my_member"), "$.my_Number"),"\"","") as xyz
I tried JSON_EXTRACT_SCALAR in MySQL Workbench but I got Error Code: 1305. FUNCTION manifest.JSON_EXTRACT_SCALAR does not exist
Instead I used JSON_UNQUOTE and that did the trick.
I have a column called 'buffer_time' which contains:
'{"after": {"time": "00:01:00", "is_enabled": true}, "before": {"time": "00:04:00", "is_enabled": true}}'
JSON_UNQUOTE(JSON_EXTRACT(buffer_time, '$.after.time'))
gave me: `
00:01:00
Hope that helps.

ToDate function provides unexpected output

I used the ToDate(userinput, format) function to covert my chararray field. I used the ToDate(userinput, 'MM/dd/yyyy') to covert the field from chararray to date but looks like i am not seeing the output as i had expected.
Here is the code:
l_dat = load 'textfile' using PigStorage('|') as (first:chararray,last:chararray,dob:chararray);
c_dat = foreach l_dat generate ToDate(dob,'MM/dd/yyyy') as mydate;
describe c_dat;
dump c_dat;
data looks like this:
(firstname1,lastname1,02/02/1967)
(John,deloy,05/26/1967)
(frank,fun,05/18/1967)
Output looks like this:
c_dat: {mydate: datetime}
(1967-05-26T00:00:00.000-04:00)
(1967-05-18T00:00:00.000-04:00)
(1967-02-02T00:00:00.000-05:00)
The output i was expecting was dateObjects with data as shown below:
(05/26/1967)
(05/18/1967)
(02/02/1967)
Please advise if i am doing anything wrong?
Ref : http://pig.apache.org/docs/r0.12.0/func.html#to-date, the return type of ToDate function is DateTime object. You can observe that in the schema description shared in output
c_dat: {mydate: datetime}
If you are having the date in the required format, you need not do any conversion.
c_dat = foreach l_dat generate dob as mydate;
If you are interested in converting the chararray date to any other format then you have to use ToString() function after getting the DateTime object.
Step 1: Convert date chararray to Date Time Ojbect using ToDate(datesstring, inutformat)
Step 2 : Use ToString(DateTime object, required format) to get the string date in the required format.
This can be achieved in a single step as below.
ToString(ToDate(date,inputformat),requiredformat);
Ref : http://pig.apache.org/docs/r0.12.0/func.html#to-string for details.