I am having a hard time trying to find something that would be equivalent to YEARFRAC (Excel) for Teradata. I messed around with the below, but want I want it to display the fraction of the year. So instead of 37 I would want to see 37.033. If possible would like it to account for leap years so wouldn't want to just divide it by 365. Any help would be greatly appreciated!
SELECT (CURRENT_DATE - CAST('1985-05-01' AS DATE)) YEAR
There is no direct function to get the desired output.
Excel YEARFRAC method uses different logic to calculate the output based on the optional parameter basis.
Syntax YEARFRAC(start_date, end_date, [basis])
Considering the basis parameter as 0 or omitted, you can achieve it in Teradata using below query.
SELECT
DATE'2022-05-13' AS Till_Date
,DATE'1985-05-01' AS From_Date
,(Till_Date - From_Date) YEAR TO MONTH AS Year_To_Month
,EXTRACT(YEAR FROM Year_To_Month)
+EXTRACT(MONTH FROM Year_To_Month)*30.0000/360
+( EXTRACT(DAY FROM Till_Date)-EXTRACT(DAY FROM From_Date))*1.0000/360 AS YEARFRAC
The basis parameter with 0 or omitted uses a 30/360 format to calculate the difference.
You can find more details about the YEARFRAC logic in below link.
https://support.microsoft.com/en-us/office/yearfrac-function-3844141e-c76d-4143-82b6-208454ddc6a8
Related
(This is all steps in containers within an Alteryx flow that is connecting to a Teradata source)
My SQL is incredibly rusty as it's been almost 8 years since I've needed to use it. I know this is a quite basic question. Basically I have several queries that need to be manually adjusted monthly to shift the month. in YYYY-MM format. They look like this:
Is the main one where I just adjust one backwards one month
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = '2023-01'
This is the secondary one where I adjust one backwards one month, and the others are same month or plus a month or more.
and A.B_MONTH in ('2022-12-01', '2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01','2023-05-01')
and B.Year_month = '2023-01'
How do I adjust the where clause to always be the needed relative references?
Any help is greatly appreciated
I tried using concat but it choked for some reason.
You can try this:
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = DATE_FORMAT( NOW() - INTERVAL 1 MONTH, '%Y/%m')
I don't understand your second need, but you can do it similar to my response.
Just play with the NOW() - INTERVAL X.
Pretty basic stuff. ADD_MONTHS to move your month around, TO_CHAR for your desired format.
To get the previous month:
select to_char(add_months(current_date,-1), 'YYYY-MM')
I was reading through a couple of older posts and tried to apply the same logic to my question, I need to extract 13 months of data broken down per month, I would also like to apply the data to relevant headers... any suggestions. Please see code below and error received.
SELECT ST.TXDATE, ST.CODE, ST.QUANTITY
FROM StocTran ST
WHERE ST.TXDATE >= DATEADD(MONTH, -13, CAST(GETDATE() AS DATE))
ORDER BY ST.TXDATE
ERROR: [Elevate Software][DBISAM] DBISAM Engine Error # 11949 SQL
parsing error - Expected end of statement but instead found ( in
SELECT SQL statement at line 3, column 27 Error Code: 11949
DATEADD is a function in MS's TransactSQL for Sql Server. I do not know that DBIsam supports it, and it is not listed in DBIsam's list of supported functions here:
https://www.elevatesoft.com/manual?action=viewtopic&id=dbisam4&product=delphi&version=7&topic=functions
Generally, date functions are not portable across different SQL engines, and from that list, one possibility might be to use the EXTRACT function instead:
The EXTRACT function returns a specific value from a date, time, or timestamp value. The syntax is as follows:
EXTRACT(extract_value
FROM column_reference or expression)
EXTRACT(extract_value,
column_reference or expression)
Use EXTRACT to return the year, month, week, day of week, day, hours, minutes, seconds, or milliseconds from a date, time, or timestamp column. EXTRACT returns the value for the specified element as an integer.
The extract_value parameter may contain any one of the specifiers:
YEAR
MONTH
WEEK
DAYOFWEEK
DAYOFYEAR
DAY
HOUR
MINUTE
SECOND
MSECOND
Even if you are in a hurry, I strngly recommend that you study that page carefully.
UPDATE: From googling dbisam dateadd it looks like Elevate don't have a good answer for an equivalent to DATEADD. One of the hits is this thread:
https://www.sqlservercentral.com/Forums/Topic173627-169-1.aspx
which suggested an alternative way to do it using Delphi's built-in date functions (like IncMonth which I suggested you use in an answer to another q. Basically, you would calculate the start- and end-dates of a range of dates, then convert them to strings to construct a WHERE clause with a column date (from your db) which is equal to or greater than the start date and less or equal to the end date.
I'm looking to calculate how many days have passed since a specific date, retrieved from a table in my database. Based on the info I've found on W3Schools (Here), I have attempted using DATEDIFF, but am coming up against a couple of different errors I can't seem to work around.
I have included my code below, and based on this, what I want to happen is this: Select the "DD" from the "Wave_Data" table, and, based on "sysdate", work out how many days have lapsed since then.
SELECT DATEDIFF(WEEKDAY,:P1_DD,SYSDATE)
FROM WAVE_DATA
WHERE WAVE_NUMBER = :P1_WAVE;
The final calculation would then be inputted into a text field within my ApEx database.
Thank you in advance for any help you may be able to provide,
Dominic
In Oracle you can just subtract one Date from another to get the difference (in days) between them:
SELECT SYSDATE - :p1_dd
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
If you want to know the difference between the dates without any time parts then you can do:
SELECT TRUNC( SYSDATE ) - TRUNC( :p1_dd )
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
or
SELECT FLOOR( SYSDATE - :p1_dd )
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
I have the dates of when employees have begun working at a zoo, however i need to figure out how long they have been working there for. I have done my research and know what i need to do, but i cant seem to figure out the syntax for incorporating the NOW() function within the DATEDIFF function. I have to display all active employees and the amount of years (with 2 decimal places) they have been working for.
I have two columns, Joined (Date) and Resigned (date, where may equal null if employee is active)
So lets just say that someone started working on 1996-09-18 (yyyy/mm/dd).
please help, thank you kindly.
(assuming you are using MySQL - from the NOW() function)
I think you need to use COALESCE() function so either Resigned or NOW() is used in the calculation by DATEDIFF():
DATEDIFF(COALESCE(Resigned, CURDATE()), Joined) AS days
So, you could have something like:
(DATEDIFF(COALESCE(Resigned, CURDATE()), Joined)) / 365 AS years
or:
(DATEDIFF(COALESCE(Resigned, CURDATE()), Joined)) / 365.25 AS years
If you want to be extremely accurate about extreme cases and leap years, a more complex calculation will be needed.
Years with 2 decimal places (Assuming TSQL)
SELECT ROUND(
CONVERT(FLOAT,
DATEDIFF(day,joined,ISNULL(resigned,GETDATE()) ))/365,2)
I am wondering if it's possible (without actually parsing the given string) to get the actual range (in terms of days, minutes or seconds) that is specified when you have an SQL statement like
[select 'x'
from dual
where date between to_date('20111113152049')
and to_date('20120113152049')]
I am working on a query where I'm given a string in the form of
"between to_date(A) and to_date(B)"
and would like to get that value in days to compare to a policy we let the user set so they don't enter a date range longer than say a week.
Assuming you're looking for a theoretical answer (that is: don't take this into production) this could work:
Prerequistes:
have three tables: days_seq(day_seq), month_seq(mth_seq) and year_seq(yr_seq)
days has the numbers 1...31, month 1..12, years 2011....?
Use te following query (I used access because I don't have proper RDBMS available here, keep in mind that MS-ACCESS/JET is forgiving in the use of the Dateserial function, that is, it doesn't break when you ask the dateserial for february, 30th, 2012)
SELECT Max(DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
, [days_seq]![day_seq]))
-
Min(DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
,[days_seq]![day_seq])) AS days
FROM days_seq, month_seq, year_seq
WHERE DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
,[days_seq]![day_seq])
BETWEEN #2012-02-1# AND #2012-02-28#
The query basically produces a carthesian product of three tables which generates all possible days in months, months in a year for as many years as you have in the years table.
Bonus:
You could off-course generate a permanent Calendar table as X-Zero suggests.
table calendar([date])
INSERT INTO calendar
SELECT DISTINCT DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
, [days_seq]![day_seq]))
FROM days_seq, month_seq, year_seq
You still have to pick your start year and your end year wisely. According to the Maya's an enddate of december 21st, 2012 will do.