How to change date format based on variable initial value? - openedge

I use below program to change the date format based on the value setup in variable(cDataFormat). But the concern is this can be changed by the user and the program should act accordingly
DEFINE VARIABLE cDate AS DATE NO-UNDO.
DEFINE VARIABLE clogindate AS CHARACTER NO-UNDO.
DEFINE VARIABLE cDateformat AS CHARACTER NO-UNDO INIT "YYYY/MM/DD". /*this can be changed by
user*/
cDate = DATE(11/27/2020).
IF cDateformat = "YYYY/MM/DD" THEN clogindate = string(year(cDate),"9999") +
string(month(cDate),"99") + string(day(cDate),"99").
ELSE IF cDateformat = "YY/MM/DD" THEN clogindate = string(year(cDate),"99") +
string(month(cDate),"99") + string(day(cDate),"99").
ELSE IF cDateformat = "MM/DD/YY" THEN clogindate = string(month(cDate),"99") +
string(day(cDate),"99") + string(year(cDate),"9999").
/* AND SO ON...... as you know writing so much lines not the smartest way..Please give any idea*/
DISP clogindate.

Instead of using IF THEN ELSE IF ELSE IF, use the CASE statement. Readability is better.
When you only have patterns MM, DD, YY and YYYY, you could use the REPLACE statement to have less lines of code.
clogindate = cDateformat.
clogindate = REPLACE(clogindate, "YYYY", STRING(YEAR(cDate), "9999")).
clogindate = REPLACE(clogindate, "YY", STRING(YEAR(cDate), "99")).
clogindate = REPLACE(clogindate, "MM", STRING(MONTH(cDate), "99")).
clogindate = REPLACE(clogindate, "DD", STRING(DAY(cDate), "99")).

Related

Saving to a date / datetime field using SQLSAVE

I'm using the following R code to save into database. However, the date field is not properly saved.
storedata$update = as.character(excelSerialNumToDate(tape$date),"%Y-%m-%d")
sqlSave(channel = dbhandle,dat = storedata,tablename = 'dbo.storeinfo',append = T,rownames = F,colnames = F,verbose = T,safer = F,fast = F, nastring = NULL)
What is the best way to save into a date field using SqlSave in R?
Wrote my own SqlSave to eliminate these issues.

Formatting NgbDate

I have a datepicker using NgbDate. I would like the format to spell out the month, hyphen, year. For example, August-2020. How can I format the following date this way?
effectiveDate = new NgbDate(date.year, date.month, date.day);
effectiveDate = effectiveDate.month.toString() + '-' +
effectiveDate.year.toString();
// the `effectiveDate` is of type `NgbDate`
effectiveDate = new NgbDate(date.year, date.month, date.day);
// you try to assign `string` to `NgbDate` which could work and it's very confusing
effectiveDate = effectiveDate.month.toString() + '-' +
effectiveDate.year.toString();
// instead it's better to create a variable for string value + string interpolation will make it easy to read
const formattedDate = `${effectiveDate.month}-${effectiveDate.year}`;
// or you can convert `NgbDate` into `Date` in the controller
this.jsDate = new Date(effectiveDate.year, effectiveDate.month - 1, effectiveDate.day);
// and use the `date` pipe in the component's HTML
jsDate | date: 'MM-yyyy'

How do you store a certain part of a string into a variable?

How do you store a certain part of a string into a variable?
For example:
x = myString // - But store the 9th character into a variable
Try this,
x = string_char_at(myString , 9);
This gets a single character from a string:
var x = string_char_at("This is my string", 4); //X == "s"
And you can use the string_copy function to copy parts of a string;
var x = string_copy("This is my string", 8, 2); //X == "my"

Saving variable get_pdf() into fields.binary

I want to save a variable automatically into a fields.binary (email_attachment_file) from get_pdf function.
My codes below here:
class example_example(models.Model):
email_attachment_file = fields.Binary('Data (.txt,.pdf)')
email_filename = fields.Char('Filename')
def generate(self,etc..):
report_name = "report_name_template"
datas = {
'ids':[],
'model' : etc,
'form' : etc
'context': context
}
moddelReport = self.pool.get('report')
alpha = modelReport.get_pdf(cr, uid,[],report_name,None,datas,context=context)
#alpha = base64.decodestring(alpha)
#alpha = alpha.decode('unicode_escape').encode('utf-8')
# --------- how to save alpha variable into fields.binary
And, is there something wrong modelReport.get_pdf function ?
Use encodestring() instead of decodestring().
report_obj = self.pool.get('report')
data = modelReport.get_pdf(cr, uid,[],report_name,None,datas,context=context)
self.email_attachment_file = base64.encodestring(data)

How to filter data from database based on YYYY-MM using Selection and SelectionArgs[] parameter in Android SQLite Query

I am storing YYYY-MM-DD and HH:MM:SS values in two separate columns in all my SQLite tables.
I have been using the following code to filter data by supplier id and date from my SQLite database.
public double addPurchaseTotal(String supplierID, String date) {
SQLiteDatabase db = helper.getReadableDatabase();
double result = 0;
String selection = VivzHelper.COLUMN_ADD_PURCHASE_SUPPLIER_ID + " =? "
+ " AND " + VivzHelper.COLUMN_ADD_PURCHASE_DATE + " =? ";
String[] selectionArgs = {supplierID, date};
Cursor c = db.query(VivzHelper.ADD_PURCHASE_TABLE,
new String[]{"sum(" + VivzHelper.COLUMN_ADD_PURCHASE_ITEM_COST_PRICE + ")"},
selection,
selectionArgs,
null,
null,
null);
if (c.moveToFirst()) {
result = c.getDouble(0);
}
c.close();
return result;
}
The value for date parameter is obtained from a date picker. As mentioned earlier, date value under the VivzHelper.COLUMN_ADD_PURCHASE_DATE is stored in YYYY-MM-DD format. I would like to filter my data based on YYYY-MM (year and month alone). How can this be done?
Instead of comparing the entire string with =, check for the prefix with LIKE or GLOB:
... WHERE Supplier = 'xxx' AND Date LIKE '2015-04-%'
... WHERE Supplier = 'xxx' AND Date GLOB '2015-04-*'
(GLOB works better together with indexing.)
In Java:
String selection = ...
+ " AND " + VivzHelper.COLUMN_ADD_PURCHASE_DATE + " GLOB ?";
String[] selectionArgs = { ..., date.substring(0, 8) + "*" };
I guess you are looking for this: SQLite Date and Time Functions

Resources