Want to set July 01 as month and day but have a variable for the year. Always want to default to year of most recent past July 01.
Has to work within the parameters present in Cognos 10.2. Tried several If statements with date2strings and string2date functions. Also tried a series of columns designed to filter data recursively.
logic
If current_month <= 6 then year(current_date, -1 ) else year(current_date). Want to concatenate the year with xxxx-06-01 or preferably 06/01/xxxx. Keep getting run-time errors, not errors in Query Calculation.
Can't see the forest for the trees at this point. Continue to explore, will post solution if achieved.
You want to keep dates in date format and avoid conversion to string and concatenation if possible. Cognos gives you several native functions to manipulate dates without having to convert.
Here's your expression:
_add_years(
_add_months(
_first_of_month(
current_date
),
7 - extract(
month,
current_date
)
),
floor(
extract(
month,
current_date
)
/ 7
) - 1
)
It easier to understand if you examine it inside out.
The _first_of_month() function returns the first day of the current month. We then use some math to determine an offset to pass into the _add_months() function that will turn the first of the current month into the first of July in the current year for every possible month value. Lastly, we use math again to either add 0 or 1 years to the resulting date using the _add_years() function. For all months less than 7, we add -1 and for months 7 and greater we add 0.
This works. Still trying to figure out how _make_timestamp () magically knows the Current FY start date?
if ( month( current_date ) < 7 ) then ( _make_timestamp ( [Year of Today] - 1, 7, 1 ) ) else
if ( month( current_date ) > 6 ) then ( _make_timestamp ( [Year of Today] + 0, 7, 1 ) ) else NULL
Related
I have current_date in Teradata which 18 DEC 2019
I have to calculate the previous quarter start date and end date from the above current_date.
Input = '2019-12-18'
Output Start Date = '2019-07-01'
Output End Date = '2019-09-30'
You should be able to do this using the TRUNC function, something like:
SELECT
TRUNC(ADD_MONTHS(CURRENT_DATE, -3), 'Q') AS Start_Quarter, -- Previous quarter start
TRUNC(CURRENT_DATE, 'Q') - 1 AS End_Quarter -- Current quarter start date - 1 day
Give it a try and let me know. This assumes the mistake in the manual is still considered a "mistake".
Also, depending on what TD version you're using, you may be able to use built-in functions:
SELECT
TD_QUARTER_BEGIN(CURRENT_DATE) AS Start_Quarter,
TD_QUARTER_END(CURRENT_DATE) AS End_Quarter
Reference
TD Manual
Built-in functions
I'm having a problem, I have a code that I'm looking for the time difference
select (to_date(:P26_DATA_UNPLUG , 'dd.mm.yyyy hh24:ss')
- to_date(:P26_DATA_PLUG, 'dd.mm.yyyy hh24:ss')) * 24
from dual;
I will answer this query similar to "2.34636..." but I want them to be of this format:'dd.mm.yyyy hh24:ss'. Is there anything that can be done ??
And one more question, when I get the answer "2.34636", the number of these numbers is too many, but can one somehow make my answer match 5 characters ??
It generally doesn't make sense to try to represent the difference between two dates in a date format. If you have two dates that are a couple of days apart, does it really make sense to return a value that represents "January 3, 0001 at 8:15:00 am" (roughly 2.34 days after January 1, 001 at midnight)? And things get harder when the differences get larger because converting days to months and years gets very, very non-obvious. If you have two dates that are exactly 1 month apart but that month is 30 days long, do you return "January 31, 0001", 30 days after Jan 1, or "February 1, 0001", 1 month after Jan 1.
If you convert the two fields to timestamps rather than days and subtract them, you'll get an interval which is a much more appropriate way to represent the difference between two points in time. You can then do things like extract the various components of the interval or convert the interval to a string.
with x as (
select to_timestamp( '19.09.2019 11:26:00', 'dd.mm.yyyy hh24:mi:ss' ) -
to_timestamp( '01.01.2019 00:00:00', 'dd.mm.yyyy hh24:mi:ss' ) diff
from dual
)
select to_char( diff ),
extract( day from diff ) days,
extract( hour from diff ) hours,
extract( minute from diff ) minutes,
extract( second from diff ) seconds
from x;
Note that you can't use extract to get months or years because there isn't an unambiguous way to represent that. If you want to implement your own logic to figure out what "years" and "months" means to you, you're free to do so by manipulating the days (i.e. in finance, you often work off 360 day years with 12 30 day months so it's easy enough to divide by 30 without caring whether you're dealing with a 28 or a 31 day month in reality).
I'd like to create a function to take the current month number (1 to 12) and produce a fiscal quarter i.e. 1, 2, 3 or 4.
A lot of the examples I have found use case statements but this seems over engineered for what I feel should be achievable using math.
I have tried a few different methods but have fallen a little short such as:
function getQuarter(m) { return round(m/4) }
Edit: building on #neil answer the function can be further shortened to:
function getQuarter(m) { return Math.ceil(m/3) }
function getQuarter(m) { return Math.floor((m - 1)/3) + 1 }
You subtract 1, because January isn't 1 month into the new year, it's the first month. Likewise 12 isn't a new quarter, it's the last month of the last quarter.
Divide by 3 instead of 4 (3 months to a quarter) and floor it. Then add one and the returned value is a number from 1 to 4 indicating which quarter you're in.
Good luck!
I'd like to get this to work in Teradata:
Updated SQL for better example
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + interval Offset hour) GMT
However, I get an error of Expected something like a string or a Unicode character blah blah. It seems that you have to hardcode the interval like this:
select current_timestamp + interval '4' day
Yes, I know I hardcoded it in my first example, but that was only to demonstrate a calculated result.
If you must know, I am having to convert all dates and times in a few tables to GMT, but I have to account for daylight savings time. I am in Eastern, so I need to add 4 hours if the date is within the DST timeframe and add 5 hours otherwise.
I know I can just create separate update statements for each period and just change the value from a 4 to a 5 accordingly, but I want my query to be dynamic and smart.
Here's the solution:
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + cast(Offset as interval hour)) GMT
You have to actually cast the case statement's return value as an interval. I didn't even know interval types existed in Teradata. Thanks to this page for helping me along:
http://www.teradataforum.com/l081007a.htm
If I understand correctly, you want to multiply the interval by some number. Believe it or not, that's literally all you need to do:
select current_timestamp as right_now
, right_now + (interval '1' day) as same_time_tomorrow
, right_now + (2 * (interval '1' day)) as same_time_next_day
Intervals have always challenged me for some reason; I don't use them very often. But I've had this little example in my Teradata "cheat sheet" for quite a while.
Two remarks:
You could return an INTERVAL instead of an INT
The recommended way to write a date literal in Teradata is DATE 'YYYY-MM-DD' instead of CAST/FORMAT
select
case
when current_date between DATE '2013-03-10' and DATE '2013-11-03'
then interval '4' hour
else interval '5'hour
end AS Offset,
current_timestamp + Offset AS GMT
I'm trying to obtain the current week for date comparison in SQLite.
I have no problem for last month, last year, today, yesterday... but don't find the solution to have the current week.
I tried lot of things like:
SELECT tastings.* FROM tastings
WHERE (DATE(tastings.date) > DATE('now','weekday 1','+ 7 days'))
Can you help me ? Thanks.
This code gives you the week number where the first day of week is monday. It also works well for last and first weeks of the year.
strftime('%W', 'now', 'localtime', 'weekday 0', '-6 days')
I guess you want compare 2 date, Assume you have a table named _testTbl and have 3 column _id INTEGER, _name TEXT, _recordDate TEXT
you want name that record this week
you can use below code:
SELECT * FROM _testTbl
WHERE _recordDate > datetime('now', 'start of day', 'weekday 6', '-7 day')
note that this week start by saturday (sunday 0, monday 1, ..., saturday 7)
this t-sql means:
datetime is a sqlite date and time function.
first parameter is given time: 'now' means the current time.
second parameter take the time to start of day.
third parameter take time to the next weekday number (in this case, saturday).
fourth parameter take time to start of week
What is stored inside the tastings.date column? Note that SQLite does not have “timestamp” type affinity, so probably you store Text (some representation of the date) or integer (julian day, epoch…)
All time and date functions expect a valid time string and convert that time string to another string format. If tastings.date contains a week number then use:
AND cast(tastings.date AS TEXT) = strftime('%W','now')
This helps me to compare the 2 dates using the week of the year.
AND ( strftime('%W', tastings.date) = strftime('%W', 'now') )
Thanks you.