How to get a datetime variable with a value of 00-00 and month ago with a given timezone? - tsql

I am looking for help to accomplish following task in T-SQL:
I have a timezone, and I need to calculate an UTC datetime which is current date minus one month, but has 0 hours and 0 minutes in a given timezone.

Is this what you need?
Select Cast(Floor(Cast(DateAdd(month, -1, getutcdate()) as float)) as datetime)

SQL Server 2008 and later provides the datetimeoffset type that includes a timezone. You can change the timezone using the SWITCHOFFSET function to change timezone and the DATEADD function to add -1 month to the resulting value. Then you can convert to the DATE type to eliminate the time part.
select cast(DATEADD(month,-1,todatetimeoffset(GETUTCDATE(),'+02:00')) as DATE)

Related

CAST datetime to date misalignment in RedShift

I need to covert a DATETIME field to DATE in order to join two datasets. No biggy, CAST(DATETIME) as DATE. However, the following is happening:
Datetime
Date
2022-12-02T22:00:00
2022-12-03
2022-12-02T08:00:00
2022-12-02
Clearly something is going on with the interpretation of the times. The DATETIME is on GMT while my system is on EST. My guess is that CAST is reading the datetime as the system's time zone and converting it to GMT on more time. Does anyone have any thoughts on what is happening and how to work around this?
I have tried CAST, CONVERT, TRUNC, AT TIME ZONE... All of them still gives the same result. The result I am searching for is the following:
Datetime
Date
2022-12-02T22:00:00
2022-12-02
2022-12-02T08:00:00
2022-12-02
Thanks!
This is due to your default timezone.
You can change for the current session like so:
SET timezone TO 'UTC';
or equivalently:
SET timezone TO 'GMT';
To change session back to default timezone:
set timezone to default;
https://docs.aws.amazon.com/redshift/latest/dg/r_timezone_config.html

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

How do we extract the hour value in 24 hours format from a DateTime value with time stored as 12 hours format with AM and PM?

I have a column with where dateTime stamp is present. Want to extract the value of the hour in 24 hours format while the time value stored in the field is in 12 Hours format, in AM and PM.
Maybe, You use the function to_char and set the parameter yyyy-mm-dd hh24:mi:ss.
Check this query:
SELECT TO_CHAR(column_name, 'yyyy-mm-dd hh24:mi:ss') FROM table_name;

Firebird check if date is before 12 pm of the current day

Date calculations are not my strong point and I need a little help.
I'm trying to check if a date (which is a timestamp) from a selected field is before 12pm of the current day. Thanks in advance.
Scenario: if an order is placed before 12pm that day, it will qualify for x otherwise it gets y. So my create date (including time) of that order is what I get in my select statement.
The DATE type doesn't carry time information, so it is up to you to define at what point in time the date is. You should use TIMESTAMP type if the time information is also important.
Anyway, lets say that the field stores the date at 12pm time, then you use
WHERE date_field <= CURRENT_DATE;
CURRENT_DATE is so called context variable which returns, obviously, current date. CURRENT_TIMESTAMP and CURRENT_TIME are also available. You can use DATEADD and DATEDIFF builtin functions to do some date calculations.
So if the field is actually timestamp, you could do it like
WHERE date_field < DateAdd(12 HOUR to cast(CURRENT_DATE as timestamp));

PostgreSQL Extract() DOW : why not Date datatype in addition to timestamp?

According to the PostgreSQL version 8.1 date-time function docs:
dow
The day of the week (0 - 6; Sunday is 0) (for timestamp values only)
SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 5
Why is not a Date datatype also a valid argument for this function? If the sequence of the days of the week does not change by locale:
0 - 6; Sunday is 0
why would the time-component of a combined date-type value be needed to determine the ordinal of the day in the week? Wouldn't the date-chunk alone be sufficient?
The original question referenced version 8.1, the 9.5 documentation states:
EXTRACT(field FROM source)
The extract function retrieves subfields such as year or hour from date/time values. source must be a value expression of type timestamp, time, or interval. (Expressions of type date are cast to timestamp and can therefore be used as well.) field is an identifier or string that selects what field to extract from the source value. The extract function returns values of type double precision. The following are valid field names:
And then later, specifically under dow:
dow
The day of the week as Sunday (0) to Saturday (6)
SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 5
Note that extract's day of the week numbering differs from that of the to_char(..., 'D') function.
This is just a documentation bug.