Writing a Date expression in PS Query
Forum » Forum / Help » Writing a Date expression in PS Query
Started by: Steve (guest)
On: 1234383464|%e %b %Y, %H:%M %Z|agohover
Number of posts: 7
rss icon RSS: New posts
Writing a Date expression in PS Query
Steve (guest) 1234383464|%e %b %Y, %H:%M %Z|agohover

Hi,

I'm trying to write an expression in PSQuery which will subtract two dates from each other and return the difference.

If I write the following in SQL it works fine, Select ROUND(A.EFFDT - A.LASTUPDDTTM) from PS_JOB a.

However if I try to insert this as an expression in PSQuery I get various error messages about Date formats beingincorrect.

I think the problem is the dynamic SQL that's created by PSQuery, i tried added a To_Date conversion into the expression but PS still wirting the SQL as follows:-

SELECT TO_DATE( TO_CHAR(A.EFFDT,'YYYY-MM-DD'),'YYYY-MM-DD') - (TO_DATE( TO_CHAR(CAST((A.LASTUPDDTTM) AS TIMESTAMP),'YYYY-MM-DD-HH24.MI.SS.FF'),'YYYY-MM-DD-HH24-MI-SS-FF'))

If I don't include the .SS.FF in the mask I receive an error about the date picture, ORA1830 error.

Does anybody have any experience of using the last update field on job in HRMS. Or can anybody suggest how I can write the expression in ps query.

Thanks
Steve

Reply  |  Options
Unfold Writing a Date expression in PS Query by Steve (guest), 1234383464|%e %b %Y, %H:%M %Z|agohover
Re: Writing a Date expression in PS Query
PrajPraj 1234410074|%e %b %Y, %H:%M %Z|agohover

Hi,

I have written something similar in PS Query to return the processing time for a process/job in the process request table. This query returns the process instance, process name, operator ID, run status, run control ID and processing time (which is process end date/time - process begin date/time expressed as seconds).

What you have explained is very close. My processing time expression looks like this:

processing-time-query-expression.png

You can ignore the case statement, the key to this is really the following:

to_char(round((ENDDTTM - BEGINDTTM) * 24 * 60 * 60))

This gives the difference in seconds. However if you just want the answer in days it would be just:

to_char(round(ENDDTTM - BEGINDTTM))

Which is pretty much exactly what you have. Note the to_char is to format the output in character format which PSQuery seems to handle a lot better than numeric format.

In terms of formatting instead of messing around with dates I've just used a character expression as I just want to display the number of seconds. I figure you just want the output as the number of days?

My Query SQL looks like this:

SELECT A.PRCSINSTANCE
 , A.PRCSNAME
 , TO_CHAR(A.RUNDTTM
 ,'YYYY-MM-DD-HH24.MI.SS."000000"')
 , A.OPRID
 , A.RUNSTATUS
 , A.RUNCNTLID
 , (CASE WHEN ENDDTTM IS NULL THEN 'Not Available' 
    ELSE to_char(round((ENDDTTM - BEGINDTTM) * 24 * 60 * 60)) END) 
   AS PROCESSING_TIME 
  FROM PSPRCSRQST A

If you try to get the output in date format you'll have all kinds of problems. For example what do you display as the year and month? What about the timestamp? I've found that I really just want the difference expressed as a year, month, day, hour, minute or second value and not in date format at all.

Hope that helps.

Reply  |  Options
Unfold Re: Writing a Date expression in PS Query by PrajPraj, 1234410074|%e %b %Y, %H:%M %Z|agohover
Re: Writing a Date expression in PS Query
Steve (guest) 1234427035|%e %b %Y, %H:%M %Z|agohover

Thanks for your reply it's appreciated :).

I've modified your expression above to use my field names, it looks as follows:-

to_char(round(a.effdt - A.LASTUPDDTTM))

I've set the expression type to character and the length to 15. When I run it I receive the following error:-

A SQL error occurred. Please consult your system log for details.
Error in running query because of SQL Error, Code=1722, Message=ORA-01722: invalid number (50,380)

When I look at the SQL that's been inserted by PSQUERY (View SQL Tab) i see the following for this expression:-

to_char(round( TO_CHAR(A.EFFDT,'YYYY-MM-DD') - TO_CHAR(CAST((A.LASTUPDDTTM) AS TIMESTAMP),'YYYY-MM-DD-HH24.MI.SS.FF'))),

Any more ideas gratefully received, it's driving me mad. Especially as when you do the same line in SQL it works fine.

Thanks
Steve

Unfold Re: Writing a Date expression in PS Query by Steve (guest), 1234427035|%e %b %Y, %H:%M %Z|agohover
Re: Writing a Date expression in PS Query
PrajPraj 1234500576|%e %b %Y, %H:%M %Z|agohover

Hi,

I can't seem to be able to replicate your scenario where the expression you're using as a field:

to_char(round(a.effdt - A.LASTUPDDTTM))

Suddenly turns to:

to_char(round( TO_CHAR(A.EFFDT,'YYYY-MM-DD') - TO_CHAR(CAST((A.LASTUPDDTTM) AS TIMESTAMP),'YYYY-MM-DD-HH24.MI.SS.FF')))

In the View SQL tab. Are you using any criteria on your expression field? Can you try removing all fields in your query, then going to the expressions tab, finding your expression and selecting use as a field again?

Sometimes PS Query caches the expression and you get the wrong version of it. It should put the expression exactly in line into your SQL.

Another thing you can try is:

  • Create a new private query
  • Add the record PSXLATITEM
  • Select the fields, FIELDNAME, FIELDVALUE, EFFDT, LASTUPDDTTM
  • Create a new expression: to_char(round(LASTUPDDTTM - EFFDT)) DAYS as a character with a length of 15 and choose select use as field to make it the fifth field.
  • Have a look at your view SQL and see if that looks right/runs? You might get a maximum rows exceeded error but that's ok, the field should show the number of days.
Unfold Re: Writing a Date expression in PS Query by PrajPraj, 1234500576|%e %b %Y, %H:%M %Z|agohover
Re: Writing a Date expression in PS Query
Steve (guest) 1234979589|%e %b %Y, %H:%M %Z|agohover

Hi,

Sorry it's taken me a few days to reply.

I couldn't get PSQuery to insert this expression without all the other associated code. I gave up in the end and ran it directly against the database via SQL. The end user only wanted a one report so it wasn't too much of a problem.

Thanks for you assistance.

Reply  |  Options
Unfold Re: Writing a Date expression in PS Query by Steve (guest), 1234979589|%e %b %Y, %H:%M %Z|agohover
Re: Writing a Date expression in PS Query
john (guest) 1250846101|%e %b %Y, %H:%M %Z|agohover

I am using PSquery to calculate the number of people who have send their resume to the company within a year period. How could I do that using date format in PSQuery?

also the number of resume received at the 31 of each month….

Thanks for your help

Reply  |  Options
Unfold Re: Writing a Date expression in PS Query by john (guest), 1250846101|%e %b %Y, %H:%M %Z|agohover
Re: Writing a Date expression in PS Query
PrajPraj 1251264004|%e %b %Y, %H:%M %Z|agohover

Here's an example that's similar to yours that may help.

Find all user profiles that were updated in the last year.

To do this in SQL:

select OPRID, LASTUPDDTTM
from PSOPRDEFN
where LASTUPDDTTM between (sysdate - 365) and sysdate
order by LASTUPDDTTM desc;

So find any user where the last update date/time LASTUPDDTTM is greater than or equal to 365 days (1 year) back from the current date, up until the current date.

Here's how you would create this expression in PS Query.

date-between-one-year-period.png

Note that I've replaced sysdate with meta-SQL - %CurrentDateTimeIn and %AddDate(%CurrentDateTimeIn, -365) to go back 365 days.

For the second part, you might want count all user profiles that were updated in the last year, grouped by each month. Here's the SQL you would write to do this:

select to_char(LASTUPDDTTM, 'Month') as MONTH, count(distinct OPRID)
from PSOPRDEFN
where LASTUPDDTTM >= (sysdate - 365) and LASTUPDDTTM <= sysdate
group by to_char(LASTUPDDTTM, 'Month');

The key here is the use of to_char to convert the month part of the LASTUPDDTTM field to a month. Here's how you would create the expression:

to-month-expression.png

Add this as a field, and perform a count on the OPRID field.

Last edited on 1251265242|%e %b %Y, %H:%M %Z|agohover By Praj + Show more
Reply  |  Options
Unfold Re: Writing a Date expression in PS Query by PrajPraj, 1251264004|%e %b %Y, %H:%M %Z|agohover
New Post
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License