Does anyone have any idea how to convert a date formatted "mmm-yy" (varchar) to "dd-mm-yyyy" or only from varchar data type to date data type.
It actually depends on what language or tool you are using, but there is almost certainly a builtin function that will help you do this.
Checkout
MySQL: DATE_FORMAT()
Oracle: SELECT TO_CHAR(SYSDATE, 'DD-MM-YYYY') FROM dual;
SQL Server: SELECT CONVERT(VARCHAR(10), GETDATE(), 120);
Or if you are doing this using a programing language i.e., Python, JavaScript etc. Just use the built in string replace methods to change the date into your desired format.
Related
I Have some difficulties with my data base.
i have uploaded data from multiple excel file, each file has a spécific date Format. some time DD/MM/YYYY and some time YYYY/MM/DD the column is character varring.
i want to make them YYYY/MM/DD.
A simple solution:
select regexp_replace('05/01/2019', '(\d\d)/(\d\d)/(\d\d\d\d)', '\3/\2/\1')
regexp_replace
----------------
2019/01/05
(1 row)
You could update the table with
update my_table
set date_column = regexp_replace(date_column, '(\d\d)/(\d\d)/(\d\d\d\d)', '\3/\2/\1')
However, you should basically store dates in a column of type date. Use the function to convert differently formatted texts to dates:
create or replace function iso_date(text)
returns date language sql immutable as $$
select case
when $1 like '__/__/____' then to_date($1, 'DD/MM/YYYY')
when $1 like '____/__/__' then to_date($1, 'YYYY/MM/DD')
end
$$
The above is an example, you can modify the function if you have more different formats. Now you can alter the column type in this way:
alter table my_table alter date_column type date using iso_date(date_column);
Read more about Data Type Formatting Functions and POSIX Regular Expressions in the documentation.
I getting an XML date/time from an XML file and need to load it in a datatype of just Date, so I have to truncate the time.
I would like to try something like this in order to let Oracle do the truncation:
TO_DATE('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD')
I verify this failed by running this:
SELECT TO_DATE('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD') FROM DUAL
It throws error: ORA-01830: data format picture ends before converting entire string.
I'm trying to minimize change to my C# program that is building the SQL statements. If I need to, I can change my C# code to generate this:
TO_DATE('2015-11-04','YYYY-MM-DD')
Can Oracle handle the truncation or must I do it in my program?
If you're storing your information in a DATE column in Oracle (which accepts date-and-time), then the following should help you see what you need to do:
select to_timestamp_tz('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD hh24:mi:ss.ff3tzh:tzm') tz,
to_date(to_char(to_timestamp_tz('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD hh24:mi:ss.ff3tzh:tzm'), 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss') tz_char_date,
cast(to_timestamp_tz('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD hh24:mi:ss.ff3tzh:tzm') as date) tz_cast_date
from dual;
TZ TZ_CHAR_DATE TZ_CAST_DATE
---------------------------------------- ------------------- ---------------------
04/11/2015 13:45:19.387000000 -05:00 04/11/2015 13:45:19 2015-11-04 13:45:19
An alternative is to use substr to shorten the string to get the portion you're interested in:
select to_date(substr('2015-11-04 13:45:19.387-05:00', 1, 19), 'yyyy-mm-dd hh24:mi:ss') substr_date
from dual;
SUBSTR_DATE
---------------------
2015-11-04 13:45:19
You can use the to_timestamp_tz() function to convert the string from XML into a timestamp with timezone value:
SELECT TO_TIMESTAMP_TZ('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD HH24:MI:SS.FFTZH:TZM')
FROM DUAL;
TO_TIMESTAMP_TZ('2015-11-0413:45:19.387-05:00','YYYY-MM-DDHH24:MI:SS.FFTZH:TZM')
--------------------------------------------------------------------------------
04-NOV-15 13.45.19.387000000 -05:00
You can then use the trunc() function to truncate the time portion to midnight; this also converts it implicitly from a timestamp to a date:
SELECT TRUNC(TO_TIMESTAMP_TZ('2015-11-04 13:45:19.387-05:00','YYYY-MM-DD HH24:MI:SS.FFTZH:TZM'))
FROM DUAL;
TRUNC(TO_TIMESTAMP_TZ('2015-11-0413:45:19.387-05:00','YYYY-MM-DDHH24:MI:SS.FFTZH
--------------------------------------------------------------------------------
04-NOV-15
This ignores the actual time zone - essentially assuming the values are in your system timezone (i.e. you are in the same -05:00 region).
You could also use a substring to strip the time and timezone part from the raw string before converting:
SELECT TO_DATE(SUBSTR('2015-11-04 13:45:19.387-05:00', 1, 10), 'YYYY-MM-DD')
FROM DUAL;
TO_DATE(SUBSTR('2015-11-0413:45:19.387-05:00',1,10),'YYYY-MM-DD')
-----------------------------------------------------------------
04-NOV-15
... or performing the substring in C# if you prefer, assuming that is parsing the XML document.
(These are intentionally displaying in a format that is different from the ISO standard so they don't look like the original string value from your XML; they are actual date/timestamp with time zone types, my client is just using my NLS settings.)
You also have the option of using Oracle's built-in XML handling to extract relational data from your document, but that's rather off-topic and might not be appropriate.
I've read a couple of posts on converting from dd/mm/yyyy to yyyy-mm-dd but the code is not working (CONVERT AND CAST). Please see example below and kindly advise if you can help as the error I'm getting is 'Conversion failed when converting date and/or time from character string'. If I change my date_engaged to '2010-09-12' it will work but I need to convert it somehow because if i say select getdate(), it will spit out this date format (yyyy-mm-dd).
It's interesting because the code is not working for me on my work PC and I'm using Microsoft SQL server Management studio 2008, but at home, it works perfectly fine.
create table test(
name varchar (10),
date_engaged nvarchar (20),
timestamp2 nvarchar (20),
LOS nvarchar (20)
)
insert into test (name,date_engaged) values ('JJ','12/09/2010')
update test
set timestamp2=CAST(DATEADD(month,0,dateadd(month,datediff(month,0,getdate()),-1)) AS DATE)
update test
set LOS=DATEDIFF(day,date_engaged,timestamp2)/365.25
thx
You need to take a look at the MSDN docs for CONVERT where you will find that we need to provide the format also so that it can format the date as per our reruirement. The reason why it is working on your home system, is may be because your system is configured in the format in which you are giving the dates.
A fix to your problem
DECLARE #x VARCHAR(13) = '12/09/2010'
SELECT RIGHT(#x,4) + '-' + LEFT(RIGHT(#x,7),2) + '-' + LEFT(#x,2)
DEMO
On a side note:
It is not recommended to use NVARCHAR or VARCHAR or CHAR to store dates, so as to avoid these types of issues. You can better use DATE datatype to store dates.
Is there any other way to get datetime field from oracle database in 24hour format???like
-> "select getxsddate(col_name) from tab_name" will get you datetime format as "2012-04-04T12:31:00"...I wanted to know if there are any other ways as i`m not satisfied with this format.
select to_char(col_name, 'yyyy-mm-dd hh24:mi:ss') from tab_name
You can read more about to_char function here
Another very simple way is to set, in the database nls parameters, the parameter date_format='DD-MM-YYYY HH24:MI:SS'
I am using Perl's DBD::ODBC to connect to an Oracle database. However, an issue arises when I try to execute a select query using a date in the where clause. It seems this issue occurs because of the database's date format being DD-MON-RR (see DBD::ODBC::FAQ). Since I cannot change the database's settings, can anyone suggest a workaround?
The database's default date format only matters if you depend on it, which you should not in general. You can:
1) Specify the format of the date in your query:
select *
from news
where news_date = to_date ('01-DEC-2009','DD-MON-RRRR');
2) Use the ANSI standard for date literals:
select *
from news
where news_date = DATE '2009-12-01';
One option is to use the TO_DATE() function (or the ANSI 'DATE' keyword) to convert the format in every query:
WHERE date_field > TO_DATE('2009-11-01', 'YYYY-MM-DD');
-- or
WHERE date_field > DATE '2009-11-01'
If you have to do this a lot, a better option would be to set the format for the session:
$dbh->do("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'");
$dbh->do("ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SSxFF'");
$dbh->do("ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SSxFF'");
Then:
my $sth = $dbh->prepare(<<EOT);
SELECT date_field
FROM some_table
WHERE date_field > '2009-11-01'
EOT
Don't rely on implicit datatype conversion. You can always specify the date format in the where clause:
WHERE your_column = to_date(:your_parameter, 'yyyy/mm/dd')