Change system date suggestions nedded - sql

We have a new software package that allows the company to write SQL code to be place on Query portals. We have several reports we wish to code using the previous day as one of the selections. If a report is ran on MONDAY we want to automatically select the previous FRIDAY as the selection date, We have ORACLE SQL DEVELOPER 4.1. The code we are trying to use is listed below:
SELECT ALERT_CD,ALERT_KEY,CHG_DTM,
CASE
WHEN TO_CHAR(SYSDATE,'fmday')='sunday'
THEN SYSDATE-2
WHEN TO_CHAR(SYSDATE,'fmday')='monday'
THEN SYSDATE-3
ELSE SYSDATE-1
END "change"
FROM SG00400T
WHERE ALERT_CD='AUTO'
and CHG_DTM >= to_date('SYSDATE', 'mm/dd/yyyy')
The error we are receiving:
ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
The CHG_DTM is a date/time field which could be part of the problem we do not full understand at this time.
Any help would be greatly appreciated.

The expression to_date('SYSDATE', 'mm/dd/yyyy') is are trying to convert the string constant 'SYSDATE' to a date - which can't work.
But you should never, ever call to_date() on a value that is already a date. That will first convert the date value to a varchar just to convert that varchar back to a date which it was to begin with.
So the to_date() function is wrong at that place to begin with. Most probably you want:
and CHG_DTM >= trunc(SYSDATE)

Related

format date in oracle SQL

I was trying to learn how to format date in oracle pl oracle, when I ran below query its returns error
SELECT TO_DATE('01-JAN-00', 'YYYY-DD-MM') FROM dual;
the error message is
ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
You are either not using the correct format specifier, or not passing the correct string. You want:
SELECT TO_DATE('2000-01-01', 'YYYY-DD-MM') FROM DUAL;
Or:
SELECT TO_DATE('01-JAN-00', 'DD-MON-YY') FROM DUAL;
Or you can simply declare a DATE litteral:
SELECT DATE'2000-01-01' FROM DUAL;
for my scenario I had to use to_char which perfectly solve the formatting issue.
SELECT TO_CHAR('01-JAN-00', 'yyyy-DD-MM') FROM dual;

sql teradata filtering on date - database version Teradata 15.10.06.02 and provider version Teradata.Net 15.11.0.0

my table has a date column. its data type is date. I confirmed it by going to table name>>columns and it says MTH_END_DT [DATE, Not NULL]
I want to filter my data for a particular date. If I put a condition where MTH_END_DT = '6/1/2018' I get an error select failed [3535] A character string failed conversion to a numeric value.
I followed this page. I used where MTH_END_DT = date '6/1/2018' and i get an error syntax error invalid date literal
I tried where cast(timestamp_column as date) = date '2013-10-22'; something like this and it throws error too
How should i filter my data?
There's only one reliable way to write a date, using a date literal, date 'yyyy-mm-dd'
where MTH_END_DT = DATE '2018-06-01'
For a Timestamp it's
TIMESTAMP '2018-06-01 15:34:56'
and for Time
TIME '15:34:56'
In SQL Assistant it's recommended to switch to Standard SQL format YYYY-MM-DD in Tools-Options-Data Format-Display dates in this format
I did have the similar problem when I was filtering a particular date for my query with Teradata. First method I tried was putting 'DATE' term as the following:
WHERE saledate = DATE'04/08/01' but this did not solve the problem.
I then used an approach I stumbled upon when surfing, finally it worked.
WHERE extract(year from saledate)=2004 AND extract(MONTH from saledate)=8 AND extract(DAY from saledate)= 1 source
I think this really should not be this long, but it worked.
It seems to me it’s most likely you have input the date format incorrectly? Maybe it includes a time by default.
For example
where MTH_END_DT = ‘2013-10-22-00:00:00:00’

Converting data with to_date function and combination of substrings

some of my teammates messed up date format which is now in YYYY-DD-MM format 2013-24-09 in my table on Oracle database. I want to convert it into date format and try following code:
TO_DATE((SUBSTR(BEGINNING_DATE, 1, 4)||'-'||substr(BEGINNING_DATE, 9,2)||'-'||substr(BEGINNING_DATE,6, 2)), 'YYYY-MM-DD')
Still an error appears:
ERROR [HY000] ERROR: to_timestamp(): bad value 31 for day of the month
When I hower select seprate substrings into columns everything looks like below and I believe it is ok.
YYYY MM DD
2013 12 31
I wonder where do I make mistake. Can you help me?
If the value is stored in the database as a string, and you just want a date out of it, TO_DATE(BEGINNING_DATE, 'YYYY-DD-MM') should suffice. You could then convert it back with TO_CHAR (with the correct format) if you're trying to fix the records in the database.
As for your particular error, if they are all stored as strings, you might want to check what value it is failing on, and make sure that one in particular is valid (e.g., it isn't '2013-02-31' or something).

oracle sql - a non-numeric character was found where a numeric was expected

I know there are loads of postings regarding fixing this error but, I'm just not understanding it!
val_strg1 value is 01.04.2016. I want to use this and not show lines where this date is older than current date, (i.e. and trunc(sysdate) < dv.val_strg1).
But, even though I have used a to_date format, I still get the a non-numeric character was found where a numeric was expected error?
I have tried several to_date formats;
to_date(val_strg1,'DDMMYYYY'), to_date(val_strg1,'DD-MM-YYYY')
The following gives me a 'not a valid month' error?
to_date(val_strg1,'DD-MON-YYYY')
My script...
select val_strg, val_strg1, to_date(val_strg1,'DDMMYYYY')
from sd_domainval_org
where name = 'HYPERCARE_CUNR'
order by sort_no
How can I use the val_strg1 as a date?
It seems you are using a text column (e.g. VARCHAR2) for the date. And you are saying that
to_date(val_strg1, 'DD.MM.YYYY')
results in an error. So you have a value in that column that does not match the pattern. Here is a query to find such invalid entries:
select *
from domainval
where name = 'HYPERCARE_CUNR'
and not regexp_like(val_strg1, '^[[:digit:]]{2}\.[[:digit:]]{2}\.[[:digit:]]{4}$');
You can then correct the wrong entries, but a better solution would of course be not to store dates in string columns at all. Use date columns instead, so as to not have such issues.
if your string date val_strg1 is in the form 'DD.MM.YYYY' (i.e. '01.04.2016'), then you have to use to_date(val_strg1,'DD.MM.YYYY');
for example: Select to_date('01.04.2016','DD.MM.YYYY') from dual;
If you have errors again, probably you have a string in the recordset that is not in a valid form for the to_date function (check the values in the val_strg1 column).
Bye,
Igor
I found an answer...
and trunc(sysdate) < to_date(regexp_substr(val_strg1, '^[[:digit:]]{2}\.[[:digit:]]{2}\.[[:digit:]]{4}$'),'DD.MM.YYYY')
...seems to work OK.
you have to tune formating string to exactly match your date structure
in this case
select val_strg, val_strg1, to_date(val_strg1,'DD.MM.YYYY')
from sd_domainval_org
where name = 'HYPERCARE_CUNR'
order by sort_no;

character_length Teradata SQL Assistant

I have to run column checks for data consistency and the only thing that is throwing off my code is checking for character lengths for dates between certain parameters.
SEL
sum(case when ( A.date is null or (character_length(A.date) >8)) then 1 else 0 end ) as Date
from
table A
;
The date format of the column is YYYY-MM-DD, and the type is DA. When I run the script in SQL Assistant, I get an error 3580 "Illegal use of CHARACTERS, MCHARACTERS, or OCTET_LENGTH functions."
Preliminary research suggests that SQL Assistant has issues with the character_length function, but I don't know how to adjust the code to make it run.
with chareter length are you trying to get the memory used? Becuase if so that is constant for a date field. If you are trying to get the length of the string representation i think LENGTH(A.date) will suffice. Unfortanatly since teradata will pad zeros on conversions to string, I think this might always return 10.
UPDATE :
Okay so if you want a date in a special 'form' when you output it you need to select it properly. In teradata as with most DBs Date are not store in strings, but rather as ints, counting days from a given 'epoch' date for the database (for example the epoch might be 01/01/0000). Each date type in teradata has a format parameter, which places in the record header instructions on how to format the output on select. By default a date format is set to this DATE FROMAT 'MM/DD/YYYY' I believe. You can change that by casting.
Try SELECT cast(cast(A.date as DATE FORMAT 'MM-DD-YYYY') as CHAR(10)) FROM A. and see what happens. There should be no need to validate the form of the dates past a small sample to see if the format is correct. The second cast forces the database to perform the conversion and use the format header specified. Other wise what you might see is the database will pass the date in a date form to SQL Assitant and sql assitant will perform the conversion on the application level, using the format specified in its own setting rather then the one set in the database.