How do I get day number from a date in abap? - abap

I need to convert a date in 'MM/DD/YYYY' format to a number that say which day in the year it is. I.E '01/01/YYYY'=1 and '12/31/YYYY'=365. Is there any built in function to do this in ABAP? I've tried googling but I couldn't find any functions which did this

Here you go in one line of code:
DATA(g_day) = p_date - CONV d( p_date(4) && '0101' ) + 1.

It is absolutely unnecessary to rely on any function module that may or may not be present in your system. Just use basic built-in language elements:
DATA: l_my_date TYPE d, " note that the data type D is YYYYMMDD
l_jan_01 TYPE d, " This will be jan 1 of the needed year
l_day TYPE i.
l_my_date = ...whatever...
l_jan_01 = l_my_date.
l_jan_01+4 = '0101'. " or any other means to get the first day of the year.
l_day = l_my_date - l_jan_01 + 1.

You can use this function module: HR_AUPBS_MONTH_DAY.
You have to pass an initial date and an end date, and it will return the number of days in between (this is what you want):
CALL FUNCTION 'HR_AUPBS_MONTH_DAY'
EXPORTING BEG_DA = P_BEGDA " Here you should put the first day of the year
END_DA = P_ENDDA " Here you put the date
IMPORTING NO_CAL_DAY = P_CAL_DAY. " This is what you want

Related

issue formatting into human time

SELECT
prefix_grade_items.itemname AS Course,
prefix_grade_items.grademax,
ROUND(prefix_grade_grades_history.finalgrade, 0)
AS finalgrade,
prefix_user.firstname,
prefix_user.lastname,
prefix_user.username,
prefix_grade_grades_history.timemodified
FROM
prefix_grade_grades_history
INNER JOIN prefix_user ON prefix_grade_grades_history.userid = prefix_user.id
INNER JOIN prefix_grade_items ON prefix_grade_grades_history.itemid =
prefix_grade_items.id
WHERE (prefix_grade_items.itemname IS NOT NULL)
AND (prefix_grade_items.itemtype = 'mod' OR prefix_grade_items.itemtype = 'manual')
AND (prefix_grade_items.itemmodule = 'quiz' OR prefix_grade_items.itemmodule IS NULL)
AND (prefix_grade_grades_history.timemodified IS NOT NULL)
AND (prefix_grade_grades_history.finalgrade > 0)
AND (prefix_user.deleted = 0)
ORDER BY course
Currently I am trying to polish this query. The problem I am having is using a UNIX Command to convert the time queried from timemodified into Human time. It comes out in epoch time. I have been attempting to use commands such as FROM_UNIXTIME(timestamp,'%a - %D %M %y %H:%i:%s') as timestamp. For reference this is a adhoc query to a moodle server contained in MariaDB. My desired result from the query is that nothing would change as far as the results we are getting, except that the time would be in a month/day/year format instead of the current format.
I have converted the timestamp into a custom date format using the below command in my select query.
DATE_FORMAT(FROM_UNIXTIME(`timestamp`), "%b-%d-%y")
As included in your question where you mention FROM_UNIXTIME(timestamp,'%a - %D %M %y %H:%i:%s'), it is indeed possible to include a second argument in order to specify the specific time/date format you wish to output converted from the UNIX timestamp.
That's the bit that looks like: '%a - %D %M %y %H:%i:%s' - this particular format string will give you an output that looks something like this: Fri - 24th January 20 14:17:09, which as you stated isn't quite what you were looking for, but we can fix that!
For example, the statement below will return the human-readable date (according to the value returned in the timestamp) in the form of month/day/year as you specified as the goal in your question, and would look similar to this: Jan/01/20
FROM_UNIXTIME(timestamp), '%b/%d/%y')
If you instead wish to use a 4 digit year you can substitute the lowercase %y for a capital %Y.
Additionally if a numeric month is instead preferred you can use %m in place of %b.
For a more comprehensive reference on the available specifiers that can be used to build up the format string, this page has a handy table
So putting it all together in the specific context of your original SQL query, using FROM_UNIXTIME to gain the human readable date (along with a suitable format string to specify the format of the output) may look something like this perhaps:
SELECT
prefix_grade_items.itemname AS Course,
prefix_grade_items.grademax,
ROUND(prefix_grade_grades_history.finalgrade, 0) AS finalgrade,
prefix_user.firstname,
prefix_user.lastname,
prefix_user.username,
FROM_UNIXTIME(prefix_grade_grades_history.timemodified, '%b/%d/%Y') AS grademodified
FROM
prefix_grade_grades_history
INNER JOIN prefix_user ON prefix_grade_grades_history.userid = prefix_user.id
INNER JOIN prefix_grade_items ON prefix_grade_grades_history.itemid = prefix_grade_items.id
WHERE (prefix_grade_items.itemname IS NOT NULL)
AND (prefix_grade_items.itemtype = 'mod' OR prefix_grade_items.itemtype = 'manual')
AND (prefix_grade_items.itemmodule = 'quiz' OR prefix_grade_items.itemmodule IS NULL)
AND (prefix_grade_grades_history.timemodified IS NOT NULL)
AND (prefix_grade_grades_history.finalgrade > 0)
AND (prefix_user.deleted = 0)
ORDER BY course
NOTE: I ended up specifying an alias for the timemodified column, calling it instead grademodified. This was done as without an alias the column name ends up getting a little busy :)
Hope that is helpful to you! :)

BAPI_ALM_ORDER_MAINTAIN Error IW:430 - Start date may not fall after end date

I'm calling the BAPI_ALM_ORDER_MAINTAIN function module like so to create a work order:
CALL FUNCTION 'BAPI_ALM_ORDER_MAINTAIN'
TABLES
it_methods = lt_methods
it_header = lt_header
it_header_up = lt_header_up
it_operation = lt_operation
it_operation_up = lt_operation_up
it_srule = lt_srule
it_srule_up = lt_srule_up
return = lt_return
extension_in = lt_extension_in.
In my lt_header table, I have the following information:
It's proven problematic for me to leave the finish_date initial. When I make the function module call, the error 430 from the IW message class is returned: Start date may not fall after end date. If I enter a valid finish date, the work order is created successfully.
I'm able to create a work order manually in IW31 without entering a finish_date.
Here is the configuration for this particular plant and order type (which I've removed from the photo):
Is there a way to submit this BAPI without specifying a finish date?
Ensure that you flag the start date with X in it_header_up, but leave the finish date unflagged - this way the missing finish date should not cause issues.

MDX Date Formatting

Can any one please tell me how to format date in MDX queries? We dont use SSRS to generate report ,we have our own customised reporting tool built on SSAS.Date filter sends date in yyyy/mm/dd format . As of now we dont have a date dimension. My date member looks like:
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00].
How can I format date in STRTOmemeber? I have tried doing this. My question is how will the value coming from user suit my member format as below. I know ssrs does it easily but we are not using SSRS. Below is my Code
my code
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS*
[CNB_DimSampleInfo].[LotNo].[LotNo].ALLMEMBERS*
[CNB_DimSampleInfo].[BatchNo].[BatchNo].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedBy].[COAReleasedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedON].[COAReleasedON].ALLMEMBERS*
[CNB_DimSampleInfo].[SampleReferenceNo].[SampleReferenceNo].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedBy].[AnalysedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedOn].[AnalysedOn].ALLMEMBERS
} ON ROWS
FROM
(
SELECT
StrToMember
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:STRTOMember([CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
) ON COLUMNS
FROM Cube001
);
There is also StrToSet which is better in your circumstance as you're using the : operator which returns a set:
...
...
FROM
(
SELECT
StrToSet
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:[CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
,CONSTRAINED
) ON COLUMNS
FROM Cube001
);
does the following definitely return the date formatted the same as your key values?
Format("2013-01-02","yyyy MM")
Do your key values for dates look like this?...
[CNB_DimSampleInfo].[COAReleasedON].[2013 01]
Your date member
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00]
looks like a bad candidate for a user date parameter, as it's precise down to the minute (perhaps the second). Unless the user exactly matches the time, they'll get nothing. But perhaps you're enforcing exact matches by using a LimitToList select list in the UI.
To get this member name, you can format the input string like this:
format([Your Input Parameter],'yyyy-MM-ddThh:mm:ss')
I have tried out using filter as below
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
filter([CNB_DimSampleInfo].[COAReleasedON].members,instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption,"2013-01-02")>0 or instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption, "2013-04-01")>0)
*[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS
} ON ROWS
FROM Cube002

Write Query to Compare time in google fusion table

I want to get all the entries from google fusion table which satisfies the condition in a given date
Time column > specific time
https://www.googleapis.com/fusiontables/v2/query?sql=SELECT * FROM 1WjowbI77j1WFcn3IEtbwBymhVZh8jfmP_dg1epd9 WHERE Date = '2015-02-23' AND Time > '10:25:04'&key=AIzaSyCALoSz00ZY3zTL1D_xUTD9GMb3T1ocBdU
But it gives me all the entries as result..
fusion table :
https://www.google.com/fusiontables/data?docid=1WjowbI77j1WFcn3IEtbwBymhVZh8jfmP_dg1epd9&key=AIzaSyCALoSz00ZY3zTL1D_xUTD9GMb3T1ocBdU#rows:id=1
It is assumed that the type of Time column is Date/Time and format is H:mm:ss AM/PM
In that case, it seems the filtering on Date/Time column is not supported.
According to Row and Query SQL Reference documentation:
Filtering on DATETIME
When filtering on a column of type DATETIME, the <value> should be
formatted as one of the following supported formats:
MMM dd, yy
MM/dd/yy
MM-dd-yy
MMM-dd-yy
yyyy.MM.dd
dd-MMM-yy
MMM/yy
MMM yy
dd/MMM/yy
yyyy
Having said that, you could consider to apply filtering to the returned results as demonstrates the following JavaScript example:
var key = 'AIzaSyCALoSz00ZY3zTL1D_xUTD9GMb3T1ocBdU'
var sql = "SELECT * FROM 1WjowbI77j1WFcn3IEtbwBymhVZh8jfmP_dg1epd9 WHERE Date = '2015-02-23'";
var requestUrl = "https://www.googleapis.com/fusiontables/v2/query?sql=" + sql + "&key=" + key;
var timeKey = '10:25:04';
$.getJSON(requestUrl, function(data) {
var filteredRows = data.rows.filter(function(row){
var dtCur = Date.parse(row[3] + ' ' + row[7]);
var dtKey = Date.parse(row[3] + ' ' + timeKey);
if (dtCur > dtKey) {
return row;
}
});
//print
var output = JSON.stringify(filteredRows, null, 2);
$("#output").text(output);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre style="background-color: #c0c0c0" id="output"></pre>
I can't tell you exactly what's going on there, the only issue I see that you've omited the AM(PM)
The documentation mentions the DateTime-Formats supported for filtering, but I think this part of the documentation is outdated(see: Date query with the current date between two date_time columns ).
For me it works as expected when I make a copy of the table(you may do the same):
https://www.googleapis.com/fusiontables/v2/query?sql=SELECT * FROM 12tl0d4jZRkhPD9ZdBzP2QzK4TyEu-U_Dnn8kimft WHERE Date = '2015-02-23' AND Time > '10:25:04 AM'&key=yourKey
Maybe they run a validation when a table will be copied and store DateTime-columns internally in another format.

How to format date as a DD, Month, Year?

I need to split ABAP date like
20091101 --> "01", "november", "2009"
The "01" and "2009" are trivial, but how do I get the month name (which should be localized)?
Is there a function to do that?
If there is no such function, perhaps a table with month names?
You can get the month's name in a given language using the module function 'MONTH_NAMES_GET', passing the language as a parameter. The day (Sunday for example) can also be obtained using 'RH_GET_DATE_DAYNAME'
Guillaume
I think the absolute simplest way would be to apply the conversion exit LDATE to your date field. Easiest is to call function module CONVERSION_EXIT_LDATE_OUTPUT.
This would for example convert
20090101
to
01. January 2009
(Unless you need to actually have the day, month text and year in separate strings, which you seem to indicate. Anyway, maybe it will help someone else).
This code will give you the date in the long text format like 'December 02, 2011'. You may change the code accordingly to print the date with long MONTH name.
DATA: LONG_DATE(20).
PERFORM GET_LONG_DATE USING LONG_DATE.
WRITE: LONG_DATE.
FORM GET_LONG_DATE USING DATE.
DATA: T_MONTH_NAMES LIKE TABLE OF T247 WITH HEADER LINE.
CALL FUNCTION 'MONTH_NAMES_GET'
EXPORTING
LANGUAGE = SY-LANGU
TABLES
MONTH_NAMES = T_MONTH_NAMES
.
DATA: YEAR(4) TYPE C,
MONTH(2) TYPE C,
DAY(2) TYPE C.
YEAR = SY-DATUM+(4).
MONTH = SY-DATUM+4(2).
DAY = SY-DATUM+6(2).
READ TABLE T_MONTH_NAMES INDEX ( MONTH ).
CONCATENATE T_MONTH_NAMES-LTX ' ' DAY INTO DATE SEPARATED BY SPACE.
CONCATENATE DATE ',' INTO DATE.
CONCATENATE DATE YEAR INTO DATE SEPARATED BY SPACE.
WRITE / DATE.
ENDFORM.
* to get full name of the day / month also can use
GET_MONTH_NAME ... for month and
GET_DATE_DAYNAME for the specific day name too
and other methods to get other format of the date are
Using the WRITE statement
data: get_date(10). "field to store output date
Converts SAP date from 20130901 to 01.09.2013
write sy-datum to get_date dd/mm/yyyy.
Converts SAP date from 20130901 to 01.09.13
write sy-datum to get_date dd/mm/yy.
Using data manipulation techniques
data: get_date(8). "field to store output date
Converts SAP date from 20130901 to 01092013
get_date(2) = sy-datum+6(2).
get_date+2(2) = sy-datum+4(2).
get_date+4(4) = sy-datum(4).
Using Function modules
data: get_date(8). "field to store output date
Converts date from 20130901 to 01SEP2013
get_date = sy-datum.
CALL FUNCTION 'CONVERSION_EXIT_IDATE_OUTPUT'
EXPORTING
input = get_date
IMPORTING
OUTPUT = get_date.
these all the formats you can use for the specific dates/months and year
Normally you can also export the date into the plant level country specific date format:
if w_country is initial.
select single LAND1
from T001W
into w_country
where WERKS eq w_the_plant.
endif.
SET COUNTRY w_country.
write w_the_date to w_export.
You may use a simple FM 'MONTH_NAMES_GET'
CALL FUNCTION 'MONTH_NAMES_GET'
EXPORTING
LANGUAGE = SY-LANGU
* IMPORTING
* RETURN_CODE =
TABLES
month_names = it_t247
EXCEPTIONS
MONTH_NAMES_NOT_FOUND = 1
OTHERS = 2
.
IF sy-subrc 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
PARAMETERS: P_1 TYPE SY-DATUM.
DATA : LV_DATE TYPE SY-DATUM,LV_TIME TYPE SY-UZEIT,lv_month type string.
LV_TIME = SY-UZEIT.
DATA: YEAR(4) TYPE C,
MONTH(2) TYPE C,
DAY(2) TYPE C.
YEAR = P_1+0(4).
MONTH = P_1+4(2).
DAY = P_1+6(2).
IF MONTH = '01'.
lv_month = 'JAN'.
ELSEIF Month = '02'.
lv_month = 'Feb'.
ELSEIF Month = '03'.
lv_month = 'Mar'.
ELSEIF Month = '04'.
lv_month = 'Apr'.
ELSEIF Month = '05'.
lv_month = 'May'.
ELSEIF Month = '06'.
lv_month = 'Jun'.
ELSEIF Month = '07'.
lv_month = 'Jul'.
ELSEIF Month = '08'.
lv_month = 'Aug'.
ELSEIF Month = '09'.
lv_month = 'Sep'.
ELSEIF Month = '10'.
lv_month = 'Oct'.
ELSEIF Month = '11'.
lv_month = 'Nov'.
ELSEIF Month = '12'.
lv_month = 'Dec'.
ENDIF.
WRITE: '|',day NO-GAP,'-',
lv_month NO-GAP,'-',year NO-GAP.
data : lv_timestamp TYPE string,
lv_str TYPE STRING.
concatenate sy-datum sy-uzeit into lv_timestamp.
concatenate 'C:\Users\Roopa Rani\desktop\header' '_' lv_timestamp '.txt' INTO FILEPATH.
I think it's useful.