How to handle DD/MM date time string in Vertica TO_TIMESTAMPTZ? - date

I have some date time strings with day first. When I try to convert to TIMESTAMPTZ the month seems to be ignored.
Example
TO_TIMESTAMPTZ('01/07/2020 04:00', 'DD/MM/YYYY HH:MM')
Returns
2020-01-01T04:00:00.000+00:00
I'm looking for 1st July but get 1st January (not even 7th January!)

The pattern for minutes is MI, not MM (see documentation):
dbadmin=> select TO_TIMESTAMP_TZ('01/07/2020 04:00', 'DD/MM/YYYY HH:MI');
TO_TIMESTAMP_TZ
------------------------
2020-07-01 04:00:00+02

Related

Set the time default to 00:00:00 in DB2

I have a DateTime column (activation_dt) in DB2 table and I want to add 1 day to the date part and my output should be "date 00:00:00".
For example:
How it is - 5/9/2001 03:00:00
how it should be - 5/10/2001 00:00:00
I tried using Concat function but is not working. Date part I am doing as "date(activation_dt +1 day) as new_dt"
Please help how should I achieve this is DB2.
You could cast it to a DATE, then TIMESTAMP. Here is the invers, try out the individual parts.
values date(timestamp(current date))
BTW: What is CURRENT TIMEZONE?
Try this
VALUES date_trunc('DAY', CURRENT TIMESTAMP + 1 DAY)
it will always return the next day 00:00:00 - use activation_dt instead of current timestamp

Nifi - How to add or subtract months from date?

I want to get two fields: Begin date and End date of last month. For example, 14-04-2020 should give me the Begin date as 01-03-2020 and End_date as 31-03-2020. I have read the Nifi Expression language docs but all it can do with a date format is add or subtract in milliseconds. This is not helpful for my use case as the number of days in a month is not fixed and conversion to milliseconds won't help.
Is there a way to achieve my use case somehow using Nifi Expression language?
#AdarshKumar
NiFI Expression Language for this Use Case would be very clunky and unreliable for different timezones, months with <> 30 days, and leap years.
Please reference this post below which goes into detail for how to get "last month":
How to insert previous month of data into database Nifi?
In the Case of NiFi you kind of have to play with the dates to get the outcome
if you have the dates you easily convert a date and then just hard set a day to get the 1st day of the month with a hard coded day
${now():toNumber():format('yyyy-MM-01')}
to get the last day of the month you can either use the script or play with the calc using epoch time.
so to get the last day of the previous month you can just use the date and convert the day into epoch time and subtract it from the epoch date to get last day of previous month
example
${now():toNumber():format('yyyy-MM-dd'):toDate('yyyy-MM-dd', 'GMT'):toNumber():minus(${now():toNumber():format('dd'):toNumber():multiply(86400):multiply(1000)}):format('yyyy-MM-dd')}
in this example above we convert the date to epoch format it to convert again, conversion happens to remove default timestamp and then we use the same formula to get just the day as a number to multiply it with 86400 seconds in a day and multiply that by 1000 for the epoch number to subtract from the date which is then formatted back into a date.
Raw Date: Thursday, August 26, 2021 11:20:31 AM
formatted: Thursday, August 26, 2021 12:00:00 AM
epoch of formatted date: 1629936000000
Subtract Epoch: 2246400000 (86400 seconds * 26 days * 1000)
result: 2021-07-31
alteratively you could first add a month and the work back to get the current day of the given month
this example is just to give you an idea of ways you can use built in date functions with epoch time to calculate the correct date, removing the issues with months that end on specific numbers.
I try UpdateAttribute to minus month
test${now():toNumber():format('yyyyMM'):minus(1)}01

TSQL ISO Month Week Number for ISO Year Week Number

Using TSQL I need to get the ISO Week Number in a Month for a give ISO Year Week Number.
For example: The following code will give me Week #1 for 12/31/2001, which is correct. It is the first Monday in 2002 and the first day of the ISO Year 2002.
select DATEPART(ISO_WEEK, '12-31-2001'); --Week 1 January 2002
My question is how do I...
(1) Take the ISO Week Number Example: ISO Year Week Number: 14 for April 4, 2016 (April Week #1).
(2) Now Take ISO Year Week Number 14 and return April Month Week Number = 1 for the example above.
There seems to be nothing in SQL Server to get the ISO Month Week# from the ISO Year Week Number. I have a function I wrote but it is has some hacks to get it to work, but not 100%.
I think you want something like this... but am not sure why you need the ISO_WEEK. Just replace getdate() with your column.
select datepart(wk, getdate()) - datepart(wk,dateadd(m, DATEDIFF(M, 0, getdate()), 0)) + 1
After attempting to handle getting ISO Month Week Number in functions I decided an easier solution was to create an ISO_Calendar table in SQL Sever.
We know in TSQL you can get the ISO Year Week Number and some a bit of work the ISO Year Number. The ISO Month Week Number is another story.
I build the table shown below with data from 2000 to 2040 by populating all the columns other than the ISO Month number. Then on a second pass I looped through the table and set the ISO Month number based on the Month# in the Monday Date.
Now if I want to get the ISO Month number for a date I check #Date between Monday and Sunday. In my case I am only concerned with dates between Monday and Friday since this is for a stock analysis site.
select #ISOMonthWeekNo = c.ISOMonthWeekNo
from ISO_Calendar c
where #TransDate between c.Monday and c.Sunday;
The advantage is the table is a one time build and easier to verify the accuracy of the data.

Postgresql ISOYEAR first date

How to get the start date of ISOYEAR in postgresql?
For example, i have a date 2012-01-01, isoyear is 2011 and it isoyear starts at 2011-01-03 and ends at 2012-01-01.
There is different ways to get isoyear, but i have no idea how to get date that iso year begins.
select extract(isoyear from '01.01.2012'::date)
select to_char('01.01.2012'::date,'IYYY')
Ask it to convert the first "ISO day" of the "ISO year":
=> SELECT to_date('2011-0001', 'IYYY-IDDD');
to_date
------------
2011-01-03

Date formatting with IYYY in PostgreSQL gives strange result

Date formatting with IYYY vs YYYY:
SELECT to_char( '2012-12-31'::DATE, 'IYYY-MM-DD' ) AS weird,
to_char( '2012-12-31'::DATE, 'YYYY-MM-DD' ) AS expected;
Result:
weird | expected
------------+------------
2013-12-31 | 2012-12-31
It would not be this surprised if the "weird" result was "2013-01-01".
What's wrong here ? (PostgreSQL 9.1.7)
IYYY refers to the year of the week of the specified date.
The first week of 2013 starts on Monday, 31 December 2012, so the weird result in the question is correct.
There's a warning directly related to this in the manual:
Attempting to construct a date using a mixture of ISO week and
Gregorian date fields is nonsensical, and will cause an error. In the
context of an ISO year, the concept of a "month" or "day of month" has
no meaning. In the context of a Gregorian year, the ISO week has no
meaning. Users should avoid mixing Gregorian and ISO date
specifications.
See also wikipedia iso week date article for lots of details.