I am trying to run the following code to reformat variables n(date1, date2) to dates from strings but am getting an error:
' invalid name
r(198);
I am using stata 15.0 (upgrade not possible)
This is the code:
foreach var of varlist date1 date2 {
gen double_d'var' = clock('var', "YMD hms")
format d_'var' %tc
drop 'var'
rename d_'var' 'var'
}
Would be grateful for some help.
Remove the underscore after double and use `’ quotes (the grave accent and the standard single quote, or apostrophe):
clear
set obs 1
gen date1 = "2020/12/01 00:00:00"
gen date2 = "2021/12/01 00:00:00"
foreach var of varlist date1 date2 {
gen double d_`var' = clock(`var', "YMD hms")
format d_`var' %tc
drop `var'
rename d_`var' `var'
}
Related
I have a list of 12 month names, let's say ['AAA', 'BBB', ...]. In reality, they are kind of historical, so not included in any typical locale.
Now I want them to be included into my DateFormat('yMMMMd') instead of the normal names. I am not able to build the date string manually like
var myDateStr = myMonthName + ' ' + myDay + ', ' + myYear; // US
var myDateStr = myDay + '. ' + myMonthName + ' ' + myYear; // DE
because I want to keep the format of the chosen locale nonetheless. The day/month/year order changes with the chosen locale, which I want to keep. I just want to replace the month name. A RegExp replace or similar is not easily possible, I guess, because of the different date formats and language specific month names.
Expected result:
For chosen US locale: AAA 1st, 2022
For chosen DE locale: 1. AAA 2022
of course, every other locale format must be supported...
Do you have any ideas?
Is it possible to just insert custom month names into DateFormat object?
I recently had an idea, which seems to work:
var dateStr = DateFormat('yMMMMd', myLocale).format(myDate);
var monthName = DateFormat('MMMM', myLocale).format(myDate);
return dateStr.replaceFirst(monthName, 'AAA');
// or
return dateStr.replaceFirst(monthName, myHistoricalNames[myDate.month - 1]);
First create the normal date string to receive the locale's date format.
Calculate another date string which only includes the locale's month name. Now I know the string which I need to replace in dateStr no matter which language I choose.
Replace the month name with my custom month name.
I am using dlookup (date = date) its fine when the date is of "May" but when I put date of any other month like 1/6/19 the dlookup remains silent.
in Dlookup if date is of MAY its fine but not working when date id of JUN the code I am using is as below
Me
Total = DLookup("Total", "Dailycash", "Cdate =#" & sdate & "#")
Apply a proper format to the date value expression:
Total = DLookup("Total", "Dailycash", "Cdate = #" & Format(sdate, "yyyy\/mm\/dd") & "#")
I'm trying to use Date functions built into pig.
The input file contains date in yyyy-mm-dd hh:mi:ss format.
I'm trying to use the following code:
row = foreach log generate FLATTEN(REGEX_EXTRACT_ALL (pattern) AS (date_time : datetime, other columns);
final_data = foreach row {
yyyy = (chararray)GetYear(date_time);
mm = (chararray)GetMonth(date_time);
dd = (chararray)GetDay(date_time);
hh = (chararray)GetHour(date_time);
mi = (chararray)GetMinute(date_time);
ss = (chararray)GetSecond(date_time);
generate CONCAT(CONCAT(CONCAT(yyyy, '-'), CONCAT(mm, '-')),dd) as myDate;
}
but I get an error:
ERROR 1066: Unable to open iterator for alias final_data. Backend error : java.lang.String cannot be cast to org.joda.time.DateTime
I'm trying to use workaround from: Formatting Date in Generate Statement
What format is expected?
REGEX_EXTRACT_ALL takes a string and returns a tuple with the extracted strings, i.e you can't extract into to DateTime fields.
You may use the ToDate UDF on the date string you loaded:
E.g:
cat data.txt
2014-03-11 13:44:11
2014-02-22 10:44:11
A = load 'data.txt' as (in:chararray);
B = foreach A generate ToDate(in,'yyyy-MM-dd HH:mm:ss') as (dt:DateTime);
C = foreach B {
year = (chararray)GetYear(dt);
month = (chararray)GetMonth(dt);
day = (chararray)GetDay(dt);
generate CONCAT(CONCAT(CONCAT(year, ''), CONCAT(month, '-')),day) as myDate;
};
dump M;
(2014-3-11)
(2014-2-22)
I have an Access 2013 form which has two unbound date fields, FromDate and ToDate. I insert these into a table (TblGuestBooking) which has an autonumber key field, so this doesn't feature in the SQL statement to follow.
If the FromDate and ToDate are in the same month, the dates are entered as dd/mm/yy, the format of the form field. If, however, the From date is in one month and the to date is in the next month or later month, the format changes to mm/dd/yy for the subsequent months.
for example 26/2/14 to 3/3/14 results in the following table entries:
26/02/14,
27/02/14,
28/02/14,
03/01/14,
03/02/14,
03/03/14
This is the code snippet I am using to put the dates into the table (BookingID is obtained from the form.)
Dim BookingDate As Date
Dim SQLString As String
....
BookingDate = FromDate
Do
SQLString = "INSERT INTO TblGuestBooking ([BookingDate], [BookingID]) VALUES (#" & BookingDate & "#" & "," & Me.GuestSuiteBookingID & ")"
DoCmd.SetWarnings False
DoCmd.RunSQL SQLString
DoCmd.SetWarnings True
BookingDate = BookingDate + 1
Loop Until BookingDate = ToDate + 1
If you've read this far, thank you for your time. If you can help me out, many, many thanks.
When processing date literals (text values enclosed by "hash marks" #) Access SQL will always interpret ambiguous xx-yy-zzzz dates as mm-dd-yyyy, regardless of the regional setting in place on the computer. So, if your machine is configured to display short dates as dd-mm-yyyy and you create an Access query that uses #04-02-2014# it will always be interpreted as April 2, not February 4.
The solution is to always format date literals as unambiguous yyyy-mm-dd values. In your case, instead of
... VALUES (#" & BookingDate & "#" ...
you would use something like
... VALUES (#" & Format(BookingDate, "yyyy-mm-dd") & "#" ...
In my case I've used following code for an ASP page that create and update a Date field in Access database.
strDay = Request("Day")
strMonth = Request("Month")
strYear = Request("Year")
InputDate = strYear & "-" & strMonth & "-" & strDay
if IsDate(InputDate) Then
InsertDate = CDate(InputDate)
else
'if date typed wrong, use Todays Date
InsertDate = Date()
end if
SQLUPD = "UPDATE MyDataBase SET DateField = '"& FormatDateTime(InsertDate, vbShortDate) &"'"
SQLINS = "INSERT INTO MyDataBase (DateField, Alist, Atype, Acomment) VALUES ('"& FormatDateTime(InsertDate, vbShortDate) &"', .....
Then all sorting and output opration with dates are going well.
I tried parsing a string in a namedQuery, but it seems doesnt work. I have this code in my domain class:
searchBirthdaten{ q ->
def dates = Date.parse("yyyyy:MM:dd HH:mm:ss", "2011-9-21 00:00:00")
eq 'birthDate' , dates)
}
But I always got this error:
Unparseable date: "2011-9-21 00:00:00"
I really dont understand why this is happening. Any idea?
Your date input string has to be in the format you defined: yyyy:MM:dd HH:mm:ss (corrected)
So your 3 issues were:
You are using the "-" character to delimit you date for parsing but your format string is using ":"
You have 5 ys in your format string i.e. yyyyy:MM.... Which won't be valid for another 8 thousandish years ;)
You define your month format as MM but you are passing only '9', this will need to be '09' to match your fomat string.