Get data that is newer than given date - sql

I am using Oracle
I want to see the result my data, where the date is after Jun 25th 2017. But I am seeing results from data that is older than the 25th Jun 2017. Some of it go all the way back to 1998 etc.
Here is my code:
select DATE_OF_LAST_CHANGE_OF_IMMU
from recipient_treatment
where DATE_OF_LAST_CHANGE_OF_IMMU > TO_DATE('25-JUN-2017', 'DD-MON-YYYY')
;
Is there something in my code that isn't right for what I wish to see?
The column: DATE_OF_LAST_CHANGE_OF_IMMU is of type date in the table

Please use the trunc command while filtering the date field as specified below
select DATE_OF_LAST_CHANGE_OF_IMMU
from recipient_treatment
where TRUNC (DATE_OF_LAST_CHANGE_OF_IMMU) > TO_DATE('25-JUN-2017', 'DD-MON-YYYY')
;

Related

How to convert date format yyyyMMdd in Hive SQL and automate the query inside the where clause

I would like to get some help in regards of automating my query.
The goal is to get the latest data, everyday the data refreshes but the start date should remain the same.
For example: Start date is Jan 1 2023, but the todays end date should be Jan 13 2023, and next day it will be Jan 14 2023, but the start date stays the same (Jan 1st 2023).
I have tried the following query but it keeps running and do not provide any output. the query I am using is hive SQL and hoping if anyone can help this.
SELECT *
FROM table_a
WHERE cast(entered_date as date) >= '20230101'
and cast(entered_date as date) < date_add(current_date(),0)
You can use date_format(DATE|TIMESTAMP|STRING ts, STRING fmt) to convert value to string in the date format. You can find the description of the function from here. Therefore if you want the output between your desired date and not beyond the current date you can query like this
SELECT *
FROM table
WHERE DATE_FORMAT(entered_date, 'yyyyMMdd') >= '20230101'
AND DATE_FORMAT(entered_date, 'yyyyMMdd') <= DATE_FORMAT(CURRENT_DATE(), 'yyyyMMdd');

Date_Trunc and To_Date Questions SQL

Can we use date_trunc for a date (not date-time) that we are trying to "truncate" (not sure if the term can be applied here) to e.g. the start of the week? So if I have date_trunc(week, 28/10/2020) and I want to get the start of the week that 28th of October lies in (so 26th of October)? I tried this in my SQL command line but I get error messages.
If I am doing: SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); How can I ensure the resulting format is in a date format I specify (rather than the default date format)? For example if I want it in format DD-MM-YYYY?
select to_char(date '2017-06-02', 'MM') < in this example, why do we need "date" for this to work? The general format for to_char should be TO_CHAR (timestamp_expression, 'format'). I don't see in this general format that we need "day".
if I have a WHERE filter like to_char(order_date, '20-10-2020'), and there are indeed some rows with this order date, will these rows still show in my results (after running query) if these rows are in DATE format (so 20 Oct is in date format) as opposed to string (which is what I am filtering by as I am doing to_char). I know there would be no need to use to_char in this case but just asking..
yes, you can use date in text form but you have to cast it to a correct type
these queries will work
select date_trunc('week', '2020-10-28'::date);
select date_trunc('week', '10/28/2020'::date);
-- as well as
select date_trunc('week', '2020-10-28'::datetime);
and return timestamp 2020-10-26 00:00:00.000000
note, next query
select date_trunc('week', '28/10/2020'::date);
will fail with error date/time field value out of range: "28/10/2020";
You can use to_char, it returns text, if you need a date format you have to case it again
select to_char( to_date ('02 Oct 2001', 'DD Mon YYYY'), 'DD-MM-YYYY')::date;
select to_char('02 Oct 2001'::date, 'DD-MM-YYYY')::date;
'2017-06-02' is a text and it can't be automatically converted to timestamp. Actually I don't know a text format which can.
No, you need to explicitly cast into date type to use it as a filter
where order_date = date_stored_as_a_text::date
I am answering the questions in a different order as there are some wrong assumptions:
Question 3
There is a general difference between '2017-06-02' and date '2017-06-02' - the first one is just a varchar, a string, NOT handled as a date by Redshift, the 2nd one tells Redshift to handle the string as date and therefore works.
Question 2
A date data type column has no format - you may an sql client that can display date columns in different formats, however, this is not a functionality of redshift. SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); tells redshift to convert the string '02 Oct 2001' to date.
Question 1
DATE_TRUNC('datepart', timestamp) also supports week as datepart - see Date parts for date or timestamp function (Also shown in the example of AWS). You should also be able to provide a date instead of a timestamp.
Question 4
to_char(order_date, '20-10-2020')is not a filter and you are using it wrong.
AWS TO_CHAR
TO_CHAR converts a timestamp or numeric expression to a character-string data format.
I guess you are rather looking for:
where to_char(order_date, 'YYYY-MM-DD') = '20-10-2020'

Oracle SQL - Why does select to_char(to_date('31-DEC-18'),'YYYY-IW') from dual; return 2018-01?

When using Oracle SQL, why does the select statement
select to_char(to_date('31-DEC-18'), 'YYYY-IW')
from dual;
return '2018-01'?
I understand that 'IW' uses the ISO standard, which is what I want to use, but as far as I can tell from my research, December 31, 2018 occurs during week 1 of 2019, according to the ISO standard. So I understand why the select statement returns '01' for the 'IW', but why does it return '2018' for the 'YYYY'? Shouldn't the statement return '2019-01'?
For the data I am using, it is preferable to use IW instead of WW.
What you may need is the ISO year format IYYY, according to Oracle documentation:
SQL> select to_char( date '2018-12-31', 'IYYY-IW')
2 from dual;
TO_CHAR
-------
2019-01
As an aside, by writing to_date('31-DEC-18') you are relying on some assumptions, which may not always be true; a safer way to write dates is the ANSI format: date '2019-12-31'
ISO year 2019 started on Dec 31 2018. All weeks start on a Monday and by the rule used, that is the first week of 2019. Oracle is correctly doing the calculation. There are handy references on-line for ISO dates.
You may want to review the rules for ISO weeks and years, say in Wikipedia.

casting to the date datatype

I have a column named thedate that is varchar. An example of a row is:
Feb 29
I want to convert this value to a date because I want to compare it to today. I have tried to_date but I'm getting a strange value:
0001-01-29 BC
I want it to be 2012-02-29. Any idea why it is not working? I thought it would automatically put in the current year for me and for some reason the month is one behind. It is also listing BC which is not needed.
select to_date(thedate, 'YYYY-MM-DD') from market;
If the data in the field is really "Feb 29", you're going to have to fill in the year yourself. That's why it's giving you 1 BC. Try this or replace '2012' with a function to get the current year.
select to_date('2012 '||thedate, 'YYYY MON DD') from market;
Try this:
select ('feb 29 ' || extract(year from CURRENT_DATE)) ::date

Oracle to_date function with quarter-format

I need to find some records created in a range of quarters. For example, I'm looking for all records created between the 4th quarter of 2008 and the 1st quarter of 2010. I have this in my WHERE-clause:
...and r.record_create_date between to_date('2008 4','YYYY Q')
and to_date('2010 1','YYYY Q')
but Oracle says: ORA-01820: format code cannot appear in date input format. The Q is a valid date format symbol, so I'm not sure what's happened. Is this even a valid way to find values in between calender quarters, or is there a better way?
Also interesting, and possibly related, if I execute this:
select to_date('2009','YYYY') from dual;
The value displayed in my IDE is 2009-08-01. I would have expected 2009-08-04, since today is 2010-08-04.
This:
select to_date('2009 1','YYYY Q') from dual;
of course, fails.
(Oracle 10g)
Oracle says: ORA-01820: format code cannot appear in date input format. The Q is a valid date format symbol, so I'm not sure what's happened.
See the second column of table 2.15 at http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34948. Not all format elements are allowed when converting to dates, timestamps, etc.
I recommend against using between for date range checks. People often will miss values within the ending day that the expect to be included. So I would translate:
and r.record_create_date between to_date('2008 4','YYYY Q')
and to_date('2010 1','YYYY Q')
To
and to_date('2008-10-01', 'YYYY-MM-DD') <= r.record_create_date
and record_create_date < to_date('2010-04-01', 'YYYY-MM-DD') -- < beginning of 2Q2010.
Someone asked the same question on OTN: http://forums.oracle.com/forums/thread.jspa?threadID=1081398&tstart=255
The crux of the issue is that you can not specify "Q" in the TO_DATE function.
Given that you're already specifying a portion of the date, why not provide the entire date? Mind too that to_date('2010 1','YYYY Q') would give you Jan 1st, 2010 when you really want March 31st, 2010... at a second to midnight.
Since the relationship between quarters to months is one-to-many, it doesn't make sense to do TO_DATE('2008 1', 'yyyy q'); what date should be returned? The first of the quarter, the end of the quarter, ...? (On the other hand, converting a date to a quarter - like TO_CHAR(SYSDATE, 'yyyy q') makes sense because a specific date only exists in one quarter.)
So, if you do want a query that looks for a date that falls between two quarters, you will have to "rolll your own" (explicitly stating the dates of the start/end of a quarter.)
As a side note, in case anyone is considering not using TO_DATE please do not use things like: WHERE date_value BETWEEN 'date string1' and 'date string2' without the TO_DATE function. It assumes a default date format and under certain situations can avoid potentially useful indexes altogether.
Below is one example where the same query can have a different result.
select sysdate from dual where sysdate between '1-Jan-10' and '31-Dec-10';
SYSDATE
---------
04-AUG-10
SQL> alter session set nls_date_format = 'YYYY-MM-DD';
Session altered.
SQL> select * from dual where sysdate between '1-Jan-10' and '31-Dec-10';
no rows selected
(Notice that in the second instance no error is returned. It just assumes Jan 10, 0001 and Dec. 10th, 0031.)
I think the best way is to just input the quarter start date and quarter end dates without even bothering with to_date. I think if you use
between '1-Jan-10' and '31-Dec-10'
for example, then you don't (in Oracle I believe) need to_date and it isn't much more difficult than typing in the quarter number
To calculate in Oracle the first day of a quarter and the last day of a quarter from the year and quarter:
I Use the fact
start_month= -2 + 3 * quarter
last_month = 3 * quarter
variable v_year number
variable v_quarter number
exec :v_year :=2017
exec :v_quarter:=4
select :v_year as year,
:v_quarter as quarter,
to_date(:v_year||to_char(-2+3*:v_quarter,'fm00'),'yyyymm') as quarter_start,
last_day(to_date(:v_year||to_char(3*:v_quarter,'fm00')||'01 23:59:59','yyyymmdd hh24:mi:ss')) as quarter_end
from dual a;
YEAR|QUARTER|QUARTER_START |QUARTER_END
2017| 4|2017-10-01 00:00:00|2017-12-31 23:59:59