This question already has answers here:
convert string to required timestamp format in oracle
(2 answers)
Closed 5 months ago.
I have the following under mycolumn in an Oracle DB
1996-02-14-02.28.37.404000
1996-02-14-02.28.37.404001
1996-02-14-02.28.37.418000
1996-02-16-02.53.00.248000
1996-02-16-02.53.00.265000
How can I select some values using WHERE in Oracle SQL?
I tried the following
SELECT TO_DATE(mycolumn, '%Y-%m-%d-%H.%i.%s.%f')
FROM mytable
WHERE TO_DATE(mycolumn, '%Y-%m-%d-%H.%i.%s.%f') > timestamp '2013-02-14 02:28:37.404'
AND TO_DATE(mycolumn, '%Y-%m-%d-%H.%i.%s.%f') < timestamp '2013-03-14 02:28:37.404';
If mycolumn column's datatype is timestamp (should be; I hope you aren't storing those values into a varchar2 column), then you'd e.g.
select *
from mytable
where mycolumn > to_timestamp('2013-02-14 02:28:37.404', 'yyyy-mm-dd hh24:mi:ss.ff3');
In other words, don't touch mycolumn and provide valid format model for value you're comparing it with.
If it really is a string (in a varchar2 column), then do the same for both values, e.g.
where to_timestamp(mycolumn, 'yyyy-mm-dd-hh24.mi.ss.ff6') >
to_timestamp('2013-02-14 02:28:37.404', 'yyyy-mm-dd hh24:mi:ss.ff3')
Related
This question already has answers here:
Comparing Dates in Oracle SQL
(5 answers)
Closed 3 years ago.
I have trouble in the SQL query to display the data fields whose records are above 29-12-2018 on Oracle ,
SELECT * FROM
data
WHERE date BETWEEN('29-12-2018' AND '01-01-2019')
ORDER BY datetime DESC
You need to use to_date() function to convert your string to a date.
SELECT * FROM
data
WHERE "date" > to_date('29-12-2018','DD-MM-YYYY')
ORDER BY datetime DESC
You can either use
SELECT *
FROM data
WHERE "date" > to_date('2018-12-29','yyyy-mm-dd') --> needs formatting as the second argument
ORDER BY datetime DESC
or
SELECT *
FROM data
WHERE "date" > date'2018-12-29' --> literal according to "ISO 8601" standard
ORDER BY datetime DESC
where date is converted to "date" since, it's a reserved keyword, and not possible to create a table with this column name.
This question already has answers here:
Need help converting date in format 20120130 to Date data type oracle sql
(4 answers)
Closed 6 years ago.
Need to covert VARCHAR2 string in format of 20150101, 20150102, 20150125.....etc into date in format of 01/01/2015, 01/02/2015.... etc.
AND update TABLE DATA
ORACLE11G MY SQL DEVELOPER
ALL HELP APPRECIATED!!! LIFE SAVER
COMPLETE SQL PL/SQL NOOB!
Just use to_date() that is what it is there for:
select to_date(col, 'YYYYMMDD')
If you then want to format this, you can use to_char():
select to_char(to_date(col, 'YYYYMMDD'), 'DD/MM/YYYY')
This question already has answers here:
How to get Time from DateTime format in SQL?
(19 answers)
Closed 7 years ago.
I have one column in my Table with DateTime as its datatype.
Now I would like to remove the date part from the datetime column, and update the same column with only time part contained in it. How should I proceed?
Below is the column value :
1900-01-01 01:43:00.000
Expected value after update would be :
01:43:00.000
If you use SQL Server from version 2008 onwards you can use
SELECT CAST('1900-01-01 01:43:00.000' as TIME)
For earlier versions, use
SELECT CONVERT(VARCHAR(20),'1900-01-01 01:43:00.000',108)
You can use this query for getting time from datetime format.
select cast(yourdatetimecol as time) [time]
from yourtable
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
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.