I have a timestamp field which has this definition:
Time interval: the beginning of the time interval expressed as the
number of millisecond elapsed from the Unix Epoch on January 1st, 1970
at UTC. The end of the time interval can be obtained by adding 600000
milliseconds (10 minutes) to this value. TYPE: numeric
I would like to split this field into Year, Month, Day of Month, Day of Week, Week Number.
It appears that I would need to use a Derive field with a Formula. But as a user new to the SPSS world, it isn't clear to me how I would use the derive field to do this.
The equivalent in pandas is:
df['Datetime'] = pd.to_datetime(df['Time interval'].astype(int))
df['Year'] = df['Datetime'].dt.year
df['Month'] = df['Datetime'].dt.month
df['Day'] = df['Datetime'].dt.day
df['DayOfWeek'] = df['Datetime'].dt.dayofweek
Do you want to create the 5 variables in separate, right?
For create:
**1) Year - Use a derive node and call the new variable as 'Year' with the syntax: "datetime_year(field)" -> will extract the year in numbers (2012)
2) Month- Use a derive node and call the new variable as 'Month' with the syntax: "datetime_month(field)" -> will extract the month in numbers (1 to 12)
3) Day of Month- Use a derive node and call the new variable as 'DayMonth' with the syntax: "datetime_day(field)" -> will extract the date of the month in numbers (1 to 31)
4) Day of Week - Use a derive node and call the new variable as 'DayWeek' with the syntax: "datetime_weekday(field)" -> will extract the weekday in numbers (1 to 7)
5) Week Number - Use a derive node and call the new variable as 'WeekNumb' with the syntax: "date_iso_week(field)" -> ISO 8601 (it's the only function that I never used in your list).**
Also, you can check others expressions inside the derive node tab, just select all functions and make some tests.
IBM Ref
I hope to have been helpful.
Related
Trying to nail down the syntax for a pretty straightforward problem.
I have a table called Events and a full-feature DATES table with a relationship between the Dates[Date] field.
Using the event name as a slicer, I trying to create a [First Monday] measure that will return the date of the first Monday of the month.
So for example, if my event date was 2/14/19, [First Monday] would return 2/4/19.
Your Date table needs to contain 2 columns:
Year-Month: for example, "2018-01"
Weekday Number: for example, 1 (for Monday); or Weekday Name (i.e, "Monday")
Then:
First Monday =
CALCULATE( MIN('Date'[Date]),
ALL('Date'),
VALUES('Date'[Year-Month]),
'Date'[Weekday Name] = "Monday")
How it works:
First, we need to access all dates in the Date table, so we use ALL()
Second, we need to see only dates for the current context year and month, for which we can use VALUES()
Third, for each month we only want Mondays, hence Date[Weekday] = "Monday"
All this unfiltering/filtering generates a set of Mondays for the Year-Month visible in the current filter context. All we need to do now is to find the earliest of the Mondays using Min(Date).
Given this domain:
class Burger{
String description
Date dateCreated
}
Currently, I have this namedQuery
queryOnDateCreated {Date dateArgument ->
eq 'dateCreated', dateArgument
}
I need a query that allows me find all the objects in the domain Burger with a specific dateCreated only taking into accountYear, Month and day (of month), while ignoring hours, minutes, seconds, miliseconds.
After some additional research, I found a solution which I'm going to share in case it helps someone else:
The named query needs to be as follows:
queryOnDateCreated {Date dateArgument ->
def dateArgumentIntervalEnd = DateUtils.addMilliseconds(dateArgument + 1, - 1)
between 'dateCreated', dateArgument, dateArgumentIntervalEnd
}
Explanation:
The "between" criteria returns every object in the domain whose date is between the interval given.
Since dateArgument is a Date created only with Year, Month and Day, it's time should be 00:00:00:000 (the first moment of the day).
Furthermore, "dateArgument + 1" holds the value of the next day (at the same time), which is why the substraction of 1 millisecond is required, that way "dateArgumentIntervalEnd" will hold the value of the same Year, Month and Day of "dateArgument" but the time will be 23:59:59:999 holding an interval of the whole day.
I have to variables: year and month, both of type integer.
E.g:
year = 2016
month = 1
I want to, in my select statement, return a timestamp given those two variables.
I've had a look at the documentation, specifically to_timestamp and to_date, but all the examples I've come across show a string being converted into a timestamp.
I do not really want to convert my year and month into a string, such as:
to_timestamp(to_char(int,year) + ' ' + to_char(int, month),YYYY/MM/DD HH24:MI:SS)
So, how can I (if it is possible) convert my year and month into a timestamp?
If you are using Postgres 9.4 or later, you could try using make_timestamp():
make_timestamp(2016, 1, 1, 0, 0, 0.0)
This would create a timestamp for January 1, 2016 at midnight. We need to specify values for the other components, even if they end up not being relevant to your query/calculation (e.g. you only need the date).
I creating an application, for that I need to find data by month using JPA and java.time.LocalDate. So, is it possible to retrieve data by month from mysql?
Thanks in advance for help.
First find start and end date of month and use between method of JPA to find data of current month.
LocalDate start = LocalDate.ofEpochDay(System.currentTimeMillis() / (24 * 60 * 60 * 1000) ).withDayOfMonth(1);
LocalDate end = LocalDate.ofEpochDay(System.currentTimeMillis() / (24 * 60 * 60 * 1000) ).plusMonths(1).withDayOfMonth(1).minusDays(1);
In Repository
List<Object> findByCreatedateGreaterThanAndCreatedateLessThan(LocalDate start,LocalDate end);
Its better to use the between keyword, it makes things allot shorter.
List<Object> findByCreatedateBetween(LocalDate start,LocalDate end);
Also if you want to use the LocalDate or LocalDateTime objects with Spring Data you should use the converter class Jsr310JpaConverters or else the documents will be stored as Blobs instead of Dates (which is bad for portability of the database). Please see this tutorial on how to implement the Converter.
https://www.mkyong.com/spring-boot/spring-boot-spring-data-jpa-java-8-date-and-time-jsr310/
tl;dr
YearMonth.now( ZoneId.of( "Pacific/Auckland" ) ) // Get current month for particular time zone.
.atDayOfMonth( 1 ) // Get the first date of that month.
.plusMonths( 1 ) // Get first of next month for Half-Open query.
Details
Assuming your column in MySQL is of DATE type…
LocalDate
The LocalDate class represents a date-only value without time-of-day and without time zone.
Time zone
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
YearMonth
The YearMonth class represents an entire month. Getting the current month requires a time zone as discussed above. Around the beginning/ending of the month, the current moment could be “next” month in Auckland New Zealand while still “previous” month in Kolkata India.
YearMonth currentMonth = YearMonth.now( z ) ;
Get the first date of the month.
LocalDate start = currentMonth.atDayOfMonth( 1 ) ;
Half-Open
Generally best to use the Half-Open [) approach to defining a span of time, where the beginning is inclusive while the ending is exclusive. So defining a month means starting with the first date of the month and running up to, but not including, the first date of the following month.
LocalDate stop = start.plusMonths( 1 ) ;
Query
Do not use the BETWEEN command in SQL as it is fully closed [], both beginning and ending being inclusive. Half-Open uses >= & < logic.
SELECT when FROM tbl
WHERE when >= start
AND when < stop
;
it's also useful
#Query("from PogWorkTime p where p.codePto = :codePto and month(p.dateApply) = :month and year(p.dateApply) = :year")
Iterable<PtoExceptWorkTime> findByCodePtoAndDateApply_MonthAndDateApply_Year(#Param("codePto") String codePto,#Param("month") int month, #Param("year") int year);
I am trying to use the so called DateDiff function to subtract the End Date from the Start Date and obtain the numbers of days apart.For example:
10/11/1995 - 7/11/1995 = 3 (extract the 'dd' from DD/MM/YYYY format)
As date values are double with the integer part counting for a day, you can use this simple expression:
[Due Date]-[Start Date]
or, for integer days only:
Fix([Due Date]-[Start Date])
That said, you should a query for tasks like this.