YYYYMMDD to YYYYMM in oracle - sql

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;

Related

Date Format syntax in where clause in Teradata

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.

Query that will select a timeframe from specific date to today

I'm having issues in my WHERE clause selecting data from a specific day to today's date. The day/time format in my date column is '7/2/2020 3:12:08 PM'.
I've tested a couple options but keep getting this error - 'literal does not match format string'.
Any idea's of how I can select all data from March 1, 2020 to current date?
Thanks!
In Oracle date columns are not strings, they are exactly in date datatype, so you don't need to convert/cast it. Just use simple date literals:
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-8F4B3F82-8821-4071-84D6-FBBA21C05AC1
select * from table where your_date_columg >= date'2015-12-31'
or with to_date function for your string:
select * from table
where
your_date_columg >= to_date('2019-11-25 13:57:52',
'yyyy-mm-dd hh24:mi:ss')

oracle compare a date with the current date

I have to convert a column to a date and then compare the month of the column with the current month.
The column looks like this :
Date
0117
0217
0317
..
I know how to convert it but cant compare it.
select date,to_date(date, 'mmyy')
from table
where ????
any ideas?
You can convert the value to a date using to_date():
select to_date(mmyy, 'MMYY')
from t;
Note that I renamed the column mmyy to clarify what it contains.
This returns the first day of the month.
The result of to_date() can then be compared to the current date. For instance, to match the first of date of the month:
where to_date(mmyy, 'MMYY') = trunc(sysdate)
If you want to match everything in the month, just use an appropriate comparison:
where to_char(to_date(mmyy, 'MMYY'), 'YYYY-MM') = to_char(sysdate, 'YYYY-MM')
or, more simply:
where mmyy = to_char(sysdate, 'MMYY')
You can convert the current date to a string and use string comparisons (which would allow you to use an index on your column):
SELECT *
FROM your_table
WHERE your_date_column = TO_CHAR( SYSDATE, 'MMYY' )
or you can convert the column to a date and compare it (which would not use an index on your column but could use a function-based index, if you created one):
SELECT *
FROM your_table
WHERE TO_DATE( your_date_column, 'MMYY' ) = TRUNC( SYSDATE, 'MM' )

How to select from SQL table WHERE a TIMESTAMP is a specific Date

Im trying to do a query where a TIMESTAMP field is = to a specific date but my query is not working:
The field is type TIMESTAMP(6) which I have only ever worked with DATE / DATETIME fields before. Here is example of a value stored here: 04-OCT-13 12.29.53.000000000 PM
Here is my SELECT statement:
SELECT * FROM SomeTable WHERE timestampField = TO_DATE('2013-10-04','yyyy-mm-dd')
I am retrieving no results and I am assuming it has to do with the fact that its not matching the TIME portion of the timestamp
If you want every record that occurs on a given day then this should work:
SELECT * FROM SomeTable
WHERE timestampField >= TO_TIMESTAMP( '2013-03-04', 'yyyy-mm-dd' )
AND timestampField < TO_TIMESTAMP( '2013-03-05', 'yyyy-mm-dd')
That will be likely to take advantage of an index on timestampField if it exists. Another way would be:
SELECT * FROM SomeTable
WHERE TRUNC(timestampField) = TO_DATE( '2013-03-04', 'yyyy-mm-dd' )
in which case you may want a function-based index on TRUNC(timestampField).
(Note that TRUNC applied to a TIMESTAMP returns a DATE.)

Select from table by knowing only date without time (ORACLE)

I'm trying to retrieve records from table by knowing the date in column contains date and time.
Suppose I have table called t1 which contains only two column name and date respectively.
The data stored in column date like this 8/3/2010 12:34:20 PM.
I want to retrieve this record by this query for example (note I don't put the time):
Select * From t1 Where date="8/3/2010"
This query give me nothing !
How can I retrieve date by knowing only date without the time?
DATE is a reserved keyword in Oracle, so I'm using column-name your_date instead.
If you have an index on your_date, I would use
WHERE your_date >= TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND your_date < TO_DATE('2010-08-04', 'YYYY-MM-DD')
or BETWEEN:
WHERE your_date BETWEEN TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND TO_DATE('2010-08-03 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
If there is no index or if there are not too many records
WHERE TRUNC(your_date) = TO_DATE('2010-08-03', 'YYYY-MM-DD')
should be sufficient. TRUNC without parameter removes hours, minutes and seconds from a DATE.
If performance really matters, consider putting a Function Based Index on that column:
CREATE INDEX trunc_date_idx ON t1(TRUNC(your_date));
Personally, I usually go with:
select *
from t1
where date between trunc( :somedate ) -- 00:00:00
and trunc( :somedate ) + .99999 -- 23:59:59
Convert your date column to the correct format and compare:
SELECT * From my_table WHERE to_char(my_table.my_date_col,'MM/dd/yyyy') = '8/3/2010'
This part
to_char(my_table.my_date_col,'MM/dd/yyyy')
Will result in string '8/3/2010'
You could use the between function to get all records between 2010-08-03 00:00:00:000 AND 2010-08-03 23:59:59:000
trunc(my_date,'DD') will give you just the date and not the time in Oracle.
Simply use this one:
select * from t1 where to_date(date_column)='8/3/2010'
Try the following way.
Select * from t1 where date(col_name)="8/3/2010"