Error when grabbing rows within current month - sql

I'm trying to grab all rows that fall into the current month. For now, I'm just hard coding the month since I can't even get that to work properly.
I did the following to convert my dates to the format "DD-MON-YYYY'
SELECT "MOLECULE_NAME", to_date("FLASK_START_DATE", 'MM/DD/YYYY')
FROM EXCEL_SCHEDULE_IMPORT
Which gives me the correct date output I want. But now, when I try to grab all the dates that fall into this current month with the following code, it's a no go. Getting the error "invalid number".
SELECT "MOLECULE_NAME", to_date("FLASK_START_DATE", 'MM/DD/YYYY')
FROM EXCEL_SCHEDULE_IMPORT
WHERE to_char("FLASK_START_DATE",'Mon-YYYY')='JUN-2017';
Any help would be appreciated. I want to end up grabbing all values from a current month based off the system clock. So if you can do that... That would be cool.
EDIT: Turns out some of the dates in the date column turn out to have incorrect data, such as "Molecule" or "Sequence". So words instead of a date. Is there any way to ignore those values?

If you want data from the current month, why not just do:
WHERE "FLASK_START_DATE" like TO_CHAR(sysdate, 'MM') || '/%/' + TO_CHAR(sydate, 'YYYY')
Or:
WHERE "FLASK_START_DATE" like '06/%/2017'
Why go back and forth on string and date conversions? Also, you should be storing date in the proper data type -- called DATE.

Related

Extract year from timestamp in hive

I am writing the query to show the data entries for a specific year. Date is stored in dd/mm/yyyy hh:mm:ss.(Date TIMESTAMP - e.g. 12/2/2014 0:00:00).
I am trying to display the two columns(name, orderdate) filtered by a specific year(year from orderdate). The requirement is to enter the specific year(2010 or 2020 etc) not the entire date. I tried using date_format() and regexp_replace() with WHERE but nothing helped.
Can someone help me?
If your are storing the date -- incorrectly -- as a string, then you can use string functions to do what you want:
where orderdate like '__/__/2010%'
However, you should really put your effort into storing the date using a correct format -- YYYY-MM-DD for strings at least.

Oracle query BETWEEN (date) AND (date)

My table "Message" contain a column name : message_date (datatype : TIMESTAMP) which stores date and time. But in this case, I would like to only show the date of the data, so I use the method to_char(case(message_date as date),'DD-MM-YYYY')
SELECT msg_id, msg_details, to_char(cast(message_date as date) ,'DD-MM-YYYY')as "DATE"
FROM message
WHERE message LIKE '%hi%'
AND to_char(cast(message_date as date), 'DD-MM-YYYY')
BETWEEN '15-01-2018'
AND '30-01-2018'
I would like to show only the row between the date 15-01-2018 and 30-01-2018. But in the end, the query result came out with the data which from date 15 - 30..where row with other month (exp: 20-03-2018 also shown in the result. I not sure why it only check for the date and not together with month and year...
Seek for help ..thanks
Use date literals with the dates in an ISO compliant format:
WHERE
message LIKE '%hi%' AND
message_date BETWEEN date '2018-01-15' AND date '2018-01-30'
Notr that you don't need to cast message_date to text, because it is already a timestamp and can be directly compared to dates.
The only issue with using BETWEEN when comparing dates is if one or more of the dates you're comparing has a time portion. For example, the date 30-JAN-18 01.32.32 PM certainly isn't between 15-JAN-18 and 30-JAN-18 - it's greater (er, later) than the latter date. Plus while using BETWEEN, which is inclusive, there is always the chance of including an edge case you didn't intend. My recommendation would be to do something like this:
SELECT msg_id, msg_details, TO_CHAR(message_date, 'DD-MM-YYYY') AS "DATE"
FROM message
WHERE message LIKE '%hi%'
AND message_date >= DATE'2018-01-15'
AND message_date < DATE'2018-01-30' + 1;
Notice in the first line I got rid of your CAST() - there is no reason to cast a date value to the DATE datatype. In the last line I'm using a bit of Oracle date arithmetic; this will give me all dates up to and including 30-JAN-18 11:59.59.9999.....
As an aside you might want to apply the LOWER() function to message in your WHERE clause:
WHERE LOWER(message) LIKE '%hi%'
otherwise you will miss messages containing Hi, HI, hI, etc.

Remove Time from date field in Teradata

Im trying to remove the time portion of a date field I am pulling in. I am using Teradata. I tried
select cast(inv_dt as date) as invoice
from tablex
to no avail it still is showing 09/01/2015 12:00:00 AM
I dont want to cast it as a char as I need to use the field in a calculation for dates. Thank you.
Your cast(inv_dt as date) does exactly what you want, you can use it in your calculation for dates...
If it's still showing a time portion it's due to your client (maybe it assumes Teradata's DATE is similar to Oracle's including time).
Finally figured it out
select
cast(cast(inv.INV_DT- EXTRACT(DAY FROM inv.INV_DT) + 1 as date format 'mm/dd/yyyy') as char (10)) as inv_dt
from invoice inv

1st Day of Current Year in Date Range Criteria in PS Query

I know how to select the first day of the first month of the current year in a number of different formats. The following works fine: '01-JAN-' || TO_CHAR(TO_DATE(SYSDATE),'YYYY').
However, I need to use January 1, of the current year in a date range criteria in a YTD PSoft Query:
WHERE A.effdt BETWEEN (January 1, Current_Year) AND SYSDATE.
When I use the expression '01-JAN-' || TO_CHAR(TO_DATE(SYSDATE),'YYYY') in the criteria, I get the following error:
A SQL error occurred. Please consult your system log for details.
Error in running query because of SQL Error, Code=1858, Message=ORA-01858:
a non-numeric character was found where a numeric was expected (50,380)`
You should NEVER compare LITERAL with DATE. Since, Oracle will do an IMPLICIT conversion. And, sooner or later, it would become a performance issue.
Explicitly convert the literal to date using TO_DATE.
For example,
Depending on the date value input method,
1. If you are passing the literal via some program
BETWEEN TO_DATE('01-01-2014','DD-MM-YYYY') and SYSDATE
2. If you already have the date value in table, then use TRUNC
BETWEEN TRUNC(SYSDATE, 'YYYY') and SYSDATE

Convert a BC date to an AD

I have dates, which are prior to year 0. I need to convert them to an AD date and add 1 year more. For example I have BC 16.2.2011 and want AD 16.2.2012.
We have a 3rd side Java application, in which sometimes an error occurs; it saves the date incorrectly. In example above it should save 16.2.2012 but instead, in the DB, is -16.2.2011 (it is one "year smaller" because Oracle has no year zero - this is important).
Converting these dates is easy, but the problem is that when a saved date should be 29.2.2012 because there is no -29.2.2011. Instead of this date it saves -1.3.2011, which I can't convert easily.
Is there any way to convert the date correctly? Like get seconds from start year right from dump data in the database?
Tell us if it is something wrong with:
select to_date(to_char(your_bc_date_here,'dd.mm.YYYY'), 'dd.mm.YYYY') + 1 from dual;
This is a wrong cast because it does not considers year may be negative(correct would be syyyy)
I've tested with: your_bc_date = to_date('BC 28.2.2011', 'BC dd.mm.yyyy')