This question already has answers here:
Regular Expression of a specific Date format
(2 answers)
Closed 8 years ago.
I am newbie in Oracle. I have to select invalid dates in column in table in oracle database. But I also can't write function because of having read only rights in database.
Can anybody help to write simple query to select invalid date in columns:
e.g. select dates, to_date(dates 'yyyy/mm/dd') from table
In the above query if the date is not in valid format it give error. But instead of error I have to output that date. Can we do it in simple query?
You can test the format using a regular expression.
It would be something like:
select dates
from tbl
where regexp_like(dates, '[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}')
This works okay. It checks that the format is in "4-digit number / 2 digit number / 2 digit number". You might want something stronger, such as:
select dates
from tbl
where regexp_like(dates, '[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}') or
(substr(dates, 1, 4) not between '1900' and '2014' or
substr(dates, 6, 2) not between '01' and '12'
substr(dates, 9, 2) not between '01' and '31'
)
This checks the format and for reasonable values in each column. Of course, it doesn't check for June 31st, but it will catch many errors.
You can try:
select *
from tbl
where not regexp_like (dtstring,'^[[:digit:]{4},/,[:digit:]{2},/,[:digit:]{2}]')
Fiddle: http://sqlfiddle.com/#!4/c7da3/11/0
Will show any values that are not 4 digits, followed by a slash, 2 more digits, followed by a slash, followed by 2 more digits.
It will not however check to see if a value matching that format is invalid, ie. 2012/23/99
Related
This question already has an answer here:
Query to compare between date with time and date without time - python using access db
(1 answer)
Closed 4 years ago.
I need to select records with DateTime between two dates in an Access query. The problem is that when I'm execute this query:
select * from logs
where date_added >= CDate("01/10/2018")
AND date_added <= CDate("04/10/2018")
I need both border values but the result does not include the last day. Maybe because "04/10/2018" is converted to "04/10/2018 00:00:00" and this value is less than all date_added values of that day.
Can I convert date_added to date only?
Do you can add a day to your date?
AND date_added < DateAdd('d',1,CDate("04/10/2018"))
An alternative expression:
SELECT * FROM logs
WHERE DateValue(date_added) BETWEEN #01/10/2018# AND #4/10/2018#
Useful date functions and syntax:
Date literals can be delimited with # in both VBA code and SQL statements, so you don't have to call CDate() on string values. Examples: #10/6/2018 4:16 PM#, #1/1/2018#
Simple mathematical notation can be used to add and subtract whole days from a date value. Example: #10/6/2018# + 1 == #10/7/2018#
DateValue( val ) takes arguments of various formats and returns a date/time value with only the date portion. This answers your question Can I convert date_added to date only? It essentially returns the same date value with the time portion as 00:00:00.
Example: DateValue(#10/6/2018 4:16 PM#) == #10/6/2018#
DateAdd ( interval, number, date ) as already noted by Milad Aghamohammadi.
Within SQL only (not VBA), one can use the BETWEEN operator. It works with various data types that have a natural sort order, which includes date values.
Example ... WHERE [DateField] BETWEEN #1/1/2018# AND #4/1/2018#
This question already has answers here:
Equals(=) vs. LIKE for date data type
(3 answers)
Closed 5 years ago.
Query 1 :
select count(*) from CI_TXN_HEADER where TXN_HEADER_DTTM = '25-JAN-13';
Result: 1
Query 2 :
select count(*) from CI_TXN_HEADER where TXN_HEADER_DTTM like '25-JAN-13';
Result: 19
In my DB I have 19 rows with TXN_HEADER_DTTM as 25-JAN-13.
Data Type of TXN_HEADER_DTTM is DATE.
Can someone please explain the difference in output?
An Oracle DATE column contains a date and a time. The LIKE condition is only for VARCHAR columns. If applied to other data types Oracle implicitly converts that to a varchar (using rules depending on the current client settings).
So you might have rows with e.g. 2013-01-25 17:42:01, however the string constant '25-JAN-13' is (most probably) converted to: 2013-01-25 00:00:00 and thus the = comparison doesn't work.
To find all rows for a specific day use trunc() and a proper date literal. Don't rely on the evil implicit data type conversion to specify date values.
Use trunc() to set the time part of a DATE value to 00:00:00:
I prefer ANSI SQL date literals:
select count(*)
from CI_TXN_HEADER
where trunc(TXN_HEADER_DTTM) = DATE '2013-01-25';
You can also use Oracle's to_date:
select count(*)
from CI_TXN_HEADER
where trunc(TXN_HEADER_DTTM) = to_date('2013-01-25', 'yyyy-mm-dd');
Note that Oracle can't use an index on TXN_HEADER_DTT, so if performance is critical use a range query:
select count(*)
from CI_TXN_HEADER
where TXN_HEADER_DTTM >= DATE '2013-01-25'
and TXN_HEADER_DTTM < DATE '2013-01-25' + 1;
The difference between like and equal is explained in this link very good
https://stackoverflow.com/a/2336940/4506285
I checked your problem on my table and I get the same results.
This link helps also to understand how to compare dates in sql
https://stackoverflow.com/a/18505739/4506285
Maybe your data consists of space, it is not exactly '25-JAN-13' but ' 25-JAN-13';
Please refer this two link:
Equals(=) vs. LIKE
What's the difference between "LIKE" and "=" in SQL?
I have different numbers looking like 40825 and I want to convert them to an actual date in Oracle SQL.
I know it should be SELECT TO_DATE(40825 ,'MM-DD-YYYY') in SQL Server, but this does not work with the same syntax in oracle SQL.
Any help?
If this number mean 4 day, 8 month, and year 2025 then, u must use to_date function with string (not nubmer) and string must looks like date mask.
SELECT TO_DATE(to_char(40825,'FM000000') ,'MMDDYY') FROM dual
I have to write a SQL request to pull some data from a server I have access to. The server uses postgresql.
There are columns in the data that look like 20150102. Problem is, these column are integers (not strings) and thus seems not to be recognized as a date.
However, I need to pull observations where the date that is stored in col1 is before the date that is stored in col2 (plus x days).
So I was thinking about:
WHERE
TO_DATE(col1, 'YYYYMMDD') < (TO_DATE(col2, 'YYYYMMDD') + 10 days)
How can I do that? What is the correct syntax here?
Many thanks!
You need to cast integer values to text 1st:
WITH t(id,col1,col2) AS ( VALUES
(1,20150102,20150103),
(2,20150102,20150103),
(3,20150102,20150102),
(4,20150102,20150110)
)
SELECT * FROM t WHERE
to_date(col1::TEXT,'YYYYMMDD') < to_date(col2::TEXT,'YYYYMMDD' + interval '10 day');
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert DB2 SQL Decimal to DATE
I have a db2 database and I want to convert a decimal to a date during the where clause in a select statement.
The decimal date could be 12 or 13 characters long depending on the month.
12 characters:
1,241,999.00 should become: 1999/1/24
13 Characters:
12,241,999.00 should become: 1999/12/24
The column name is DECIMALCOLUMN:
Select * from table1 WHERE
cast(replace(convert,DECIMALCOLUMN,date)) = #date
I see: You want some way of rearranging the digits of a number to become a date. It looks like the encoding is [m]mddyyyy. (Why isn't that a date datatype in the database?)
The number needs to be converted to a string and then substrings arranged and converted to a date. Given the complexities here, a conversion function should be written in lieu of the field being altered to be a proper datatype. Something like this should do it (untested, no access to db2):
create function decimaldate(decdate DECIMAL)
returns DATE
return
with tmp (dstr) as
(
select substr(digits (decdate),8)
from sysibm.sysdummy1
)
select
date (substr(dstr,4,4) || '-' ||
substr(dstr,1,2) || '-' ||
substr(dstr,3,2)
)
from tmp
This converts the number to a string, reformats the string as yyyy-mm-dd and converts to a date. (I used this as a basis.)
So the solution to the original question is simply to write:
SELECT *
FROM table1
WHERE decimaldate(DECIMALCOLUMN) = #date
With such a screwy way of encoding the date in the database, having the function always available should prove invaluable.