Normally I'm a huge fan of SQL, but I think the pure RPG solutions are
easier.
BTW the embedded SQL example won't work, because you cannot convert a string
with a character date in the format DDMMYYYY into a real date, you need to
add the valid date separators, i.e. DD.MM.YYYY or MM/DD/YYYY or YYYY-MM-DD.
...
Exec SQL
Set :NewDate = Date(CASE WHEN extract(day from :OldDate) >=16
THEN '16'
ELSE '01' END
concat '.' concat
Digits(Dec(Extract(month from :OldDate), 2, 0))
concat '.' concat
Digits(Dec(Extract(year from :OldDate), 4, 0)))
The date representation can also be a 14 character string in the format
YYYYMMDDHHMMSS:
Exec SQL
Set :NewDate = Date(Digits(Dec(Year(:OldDate), 4, 0)) concat
Digits(Dec(Month(:OldDate), 2, 0)) concat
Digits(Dec(Case When Day(:OldDate) < 16
Then 1
else 16 End, 2, 0)) concat
'000000');
If you are on release 6.1 or higher you also can use the scalar function
Trunc_timestamp:
Exec SQL
Set :NewDate = Date(Trunc_Timestamp(Timestamp_Iso(:OldDate), 'MM')
+ case when Day(:OldDate) >= 16
Then 15
Else 0 End Days);
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
"Shoot for the moon, even if you miss, you'll land among the stars." (Les
Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not training them
and keeping them!"
-----Ursprüngliche Nachricht-----
Von: rpg400-l-bounces@xxxxxxxxxxxx [mailto:rpg400-l-bounces@xxxxxxxxxxxx] Im
Auftrag von Hiebert, Chris
Gesendet: Saturday, 16.6 2012 00:35
An: rpg400-l@xxxxxxxxxxxx
Betreff: RE: Change the day of a date
If the program was and SQLRPGLE would this be more efficient?
Exec sql
SET :NEWDATE = Date(
(CASE WHEN extract(day from :OLDDATE ) >=16
THEN '16' ELSE '01' END)
|| digits( DEC( extract(month from :OLDDATE ) ,2,0))
|| digits( DEC( extract(year from :OLDDATE ) ,4,0)));
Chris Hiebert
-----Original Message-----
From: <mailto:rpg400-l-bounces@xxxxxxxxxxxx> rpg400-l-bounces@xxxxxxxxxxxx
<mailto:[mailto:rpg400-l-bounces@xxxxxxxxxxxx]>
[mailto:rpg400-l-bounces@xxxxxxxxxxxx] On Behalf Of Barbara Morris
Sent: Tuesday, June 12, 2012 4:15 PM
To: <mailto:rpg400-l@xxxxxxxxxxxx> rpg400-l@xxxxxxxxxxxx
Subject: Re: Change the day of a date
On 6/12/2012 1:52 PM, Dennis wrote:
Oops. . . in my haste to get to work -- because, don't we all love
ourjobs that much? -- I stopped the changes too soon. I will leave
completion as an exercise for no extra points.
This is the only way I can think of to do it without work variables.
if %subdt(MyNewDateField: *Days)>= 16 ; // set day to 16
MyNewDateField -= %days( %subdt(MyNewDateField: *Days) - 16 ) ;
else ; //<= 15 ; set day to 01
MyNewDateField -= %days( %subdt(MyNewDateField: *Days) - 01 ) ;
endif ;
But, gagorama, I should be awarded negative points for even typing this.
Not only does it do the costly %subdt twice, it has the same complex
expression three times in the code, maximizing the potential for maintenance
errors. Hoping there's a better solution to Dennis'
challenge.
Here's a way of using the work variable twice, to further simplify the code
by having only one statement where the %days is subtracted.
Days = %subdt(MyNewDateField: *Days);
if Days >= 16 ; // set day to 16, eg 2012/06/27
Days -= 16; // by subtracting this many days, eg 11
else; // set day to 1, eg 2012/06/09
Days -= 1; // by subtracting this many days, eg 8
endif;
MyNewDateField -= %days( Days );
I'm a fan of using work variables and extra statements to simplify code.
I'm a bit uneasy about using the vague "Days" for two purposes, though.
Maybe two work variables, "DayNbr" and "DaysToSubtract", would be better.
--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing list
To post a message email: <mailto:RPG400-L@xxxxxxxxxxxx>
RPG400-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: <
http://lists.midrange.com/mailman/listinfo/rpg400-l>
http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: <mailto:RPG400-L-request@xxxxxxxxxxxx>
RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at
<
http://archive.midrange.com/rpg400-l>
http://archive.midrange.com/rpg400-l.
--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing list
To post a message email: <mailto:RPG400-L@xxxxxxxxxxxx>
RPG400-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: <
http://lists.midrange.com/mailman/listinfo/rpg400-l>
http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: <mailto:RPG400-L-request@xxxxxxxxxxxx>
RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at
<
http://archive.midrange.com/rpg400-l>
http://archive.midrange.com/rpg400-l.
As an Amazon Associate we earn from qualifying purchases.