Maturity column is of date datatype.
select TO_char(maturity,'YYYY-MM') || '-15' from tablename
The above query returns me the column value with text datatype. But how can i return the column value as date datatype.
You can cast to date using ::date. ie:
select (TO_char(maturity,'YYYY-MM') || '-15')::date from myTable
You could also do this using date arithmetic:
select maturity + (15 - extract(day from maturity)) * interval '1 day'
In general, I prefer not to convert to strings to do things that can be handled by date/time functions.
Related
I have a table with date column in which date is updated in this format - 11/21/2022.
How can I get the results for the last 15 days using this date column in Teradata? Looks like need to change the date format in where clause.
I was using below query which does not work with this format
select *
from table_A
WHERE date BETWEEN (CURRENT_DATE - INTERVAL '30' DAY)
AND CURRENT_DATE
This does not give any results.
Assuming your date column be text, then you must first convert it to a bona fide date using the TO_DATE() function:
SELECT *
FROM table_A
WHERE TO_DATE("date", 'MM/DD/YYYY') BETWEEN (CURRENT_DATE - INTERVAL '30' DAY) AND CURRENT_DATE;
Note that ideally you should be storing your dates in actual date columns, not as text. This would avoid the need to call the clunky TO_DATE() function.
I have these varchar : 20211026231735.
So I would like a query to substract actual sysdate to that date and convert the substraction to DAY HOURS AND SECONDS.
select TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS') - start_time from TABLEA where job_name='jOB_AA_BB';
I get 4220.
Any help please? Thanks
When you do datetime arithmetic with the DATE datatype, you get back a NUMBER of days. To get an INTERVAL you can subtract two TIMESTAMPs. You don't say what the data type is for start_time, but you might get away with this:
select localtimestamp - start_time
from tablea where job_name='jOB_AA_BB';
LOCALTIMESTAMP gives you a TIMESTAMP value in the current session time zone. There's also CURRENT_TIMESTAMP, which give you the same thing in a TIMESTAMP WITH TIME ZONE and SYSTIMESTAMP that gives you the database time in TIMESTAMP WITH TIME ZONE. You may need to convert your start_time to avoid time zone differences, if any.
You can us the function numtodsinterval to convert the results of date arithmetic to an interval. If necessary then use extract to pull out the needed components.
with tablea(job_name, start_time) as
(select 'jOB_AA_BB','20211026231735' from dual)
select numtodsinterval((SYSDATE - to_date( start_time,'yyyymmddhh24miss')),'hour') date_diff
from tablea where job_name='jOB_AA_BB' ;
with tablea(job_name, start_time) as
(select 'jOB_AA_BB','20211026231735' from dual)
select extract (hour from date_diff) || ':' || extract (minute from date_diff)
from (
select numtodsinterval((sysdate - to_date( start_time,'yyyymmddhh24miss')),'day') date_diff
from tablea where job_name='jOB_AA_BB'
);
NOTE: I am not sure how you got any result, other than an error, as your query winds up as a string - a string. You should not convert sysdate to a string but your string to a date (better yet store it as the proper data type - date).
You can convert the value to a date (rather than converting SYSDATE to a string) and then subtract and explicitly return the value as an INTERVAL DAY TO SECOND type:
SELECT (SYSDATE - TO_DATE('20211026231735', 'YYYYMMDDHH24MISS')) DAY TO SECOND
FROM DUAL;
Or, for your table:
SELECT (SYSDATE - TO_DATE(start_time,'YYYYMMDDHH24MISS')) DAY(5) TO SECOND
FROM TABLEA
WHERE job_name='jOB_AA_BB';
db<>fiddle here
I need to convert an integer to a date format in AS400.
I have the field called ivdat8, which is integer in the formatted YYYYMMDD.
I need to use a WHERE clause to select data between two dates, today and 3 days ago.
I am using the below line to do this:
Where
ivdat8 Between (Select current date - 3 days from sysibm.sysdummy1) And (Select current date from sysibm.sysdummy1)
The current date from sysibm is a true date format, but ivdat is integer.
How can i cast ivdat8 to be a date format i can use within the WHERE clause ?
I have tried the below to convert the int to date:
cast(cast(A.ivdat8 as varchar(50)) as date)
&
TIMESTAMP_FORMAT(ivdat8,'YYYYMMDD')
Actually it's better for performance not to convert the ivdat8 column data, but do this with parameters like below.
select ivdat8
from table (values
int(to_char(current date - 2 days, 'YYYYMMDD'))
, int(to_char(current date - 5 days, 'YYYYMMDD'))
) t (ivdat8)
where ivdat8 between
int(to_char(current date - 3 days, 'YYYYMMDD'))
and int(to_char(current date, 'YYYYMMDD'));
Easiest way to do it without causing a complicated conversion in the query is to use this:
cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date)
The full where clause doesn't need to select from sysibm.dummy1 either.
where cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date) between current date - 3 days and current date
If you have indexes built on ivdat8 though, the fastest selection will be:
where A.ivdat8 between cast(Current date - 3 days as dec(8)) and cast(Current Date as dec(8))
Managed to convert the int to a date, first i had to cast it as a string, then use TO_DATE
To_Date(cast(A.ivdat8 as varchar(50)),"YYYYMMDD")
try this
Where cast(TIMESTAMP_FORMAT(cast(ivdat8 as varchar(8)), 'YYYYMMDD') as date)
Between (current date - 3 days) and current date
i have column with date type, and another with varchar type, i want to concatinate this column then convert it in timestamp
i try this but doesn't work
CAST (adjustmentDate || adjustmenttime AS TIMESTAMP (0) FORMAT 'YYYYMMDDHHMISS' )
Can you please try
select
cast(adjustmentDate as timestamp(0)) +
( CAST (adjustmenttime AS time(4) format 'HHMISS') - time '00:00:00' hour to second)
;
I works for me with some literals, so I'm hoping it can work for your table.
I have a column with DATE datatype in a table.
I am trying to retrieve the column values in YYYYMM format. My select query looks like below
select *
from tablename
where date column = to_char(to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
I am getting below exception.
ORA-01847: day of month must be between 1 and last day of month
Appreciate any input on this.
I think the simplest method is:
where to_char(datecolumn, 'YYYYMM') = '400012'
Or, if you prefer:
where to_char(datecolumn, 'YYYYMM') = to_char(to_date('12/31/4000', 'MM/DD/YYYY'), 'YYYYMM');
Syntax-wise, the right hand date (to the right of the equals) is OK. But you are doing a character comparison, not a date comparison.
This works for me in multiple databases:
select to_char (to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM')
from dual;
Even though your column is named DATE_COLUMN, you are comparing based on characters in the query.
So, try this instead - this compares based on dates (NOT a character comparison) and truncates off the hour, minute, ETC. so you are only comparing the DAY:
select * from DATE_TAB
where TRUNC(DATE1, 'DDD') = TRUNC(to_date('12/31/4000','MM/DD/YYYY'),'DDD');
NOTE: The DATE1 field above is a DATE field. If you're DATE_COLUMN is not a DATE field, you must
convert it to a DATE datatype first (using TO_DATE, ETC.)
Assuming that "date_column" is actually a date, and that you have an index on date_column, you can do something like this to return the data quickly (without truncating dates in all rows to do a comparison):
with dat as (
select level as id, sysdate - (level*10) as date_column
from dual
connect by level <= 100
)
select id, date_column
from dat
where date_column between to_date('11/1/2013', 'MM/DD/YYYY') and last_day(to_date('11/2013 23:59:59', 'MM/YYYY HH24:MI:SS'))
Here I just dummy up some data with dates going back a few years. This example picks all rows that have a date in the month of November 2013.
If your date_column's data-type is DATE, then use
select *
from tablename
where TO_CHAR(date_column,'YYYYMM') = to_char (to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
If your date_column's data-type is VARCHAR, then use:
select *
from tablename
where date_column = to_char (to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
I somehow feel your error is because you have a space between date and column as
"date column". If the field name in the table is "COLUMN", then just removing the word "DATE" from your original query would suffice, as:
select *
from tablename
where column = to_char(to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
If your column (YYYYMMDD) is in number format, the simplest way to get YYYYMM would be
select floor(DATE/100)
from tablename;