I have a INSERT + SELECT statement. One column is a VARCHAR but must be filled with a date with 'yyyyMMdd' format in 3 different dialect.
With SQL server and Oracle there's no problem, but in Db2 I didn't find anything to do this stuff.
In SQLServer is CONVERT(VARCHAR,S.DT_NASCITA,112)
In Oracle is TO_CHAR(S.DT_NASCITA,'YYYYMMDD')
Any1 can help me pls ?
ty in advice !
Fix in a very bad way. This way work in all distro, seems
SELECT SUBSTR(CHAR(CURRENT DATE, ISO),1,4) || SUBSTR(CHAR(CURRENT DATE, ISO),6,2) || SUBSTR(CHAR(CURRENT DATE, ISO),9,2) FROM SYSIBM.SYSDUMMY1
Related
Which date function use to convert date format
YYYYMMDD TO DDMMYYYY
I am using db2 database please help me
assuming you column_with_date is a valid date column and if you have DB2 9.7, you could use
date(to_date(column_with_date,'DDMMYYYY'))
Assuming that your column's data type is VARCHAR, convert it to date and then format it to DDMMYYYY:
varchar_format((to_date(col, 'YYYYMMDD'), 'DDMMYYYY')
replace col with your column's name.
Or use substr() and concatenation:
substr(col, 7, 2) || substr(col, 5, 2) || substr(col, 1, 4)
You can use:
to_char(datecol,'DDMMYYYY')
just to display as DDMMYYYY
a date column doesn't have a format( not stored with a format within a DB table)
2 ways to do it that I can think of, already mentioned above but giving a real command so you can try it.
db2 "select to_char(current_date,'DDMMYYYY') from sysibm.sysdummy1"
or
db2 "select varchar_format(current_date,'DDMMYYYY') from sysibm.sysdummy1"
I understand the basic TO_DATE usage in Oracle SQL. i google and found some guides to use TO_DATE which is to convert julien date to normal date.
the basic working code is :
SELECT TO_CHAR((TO_DATE(2016365, 'YYYYDDD'))) FROM DUAL)
However, i want to convert date that is in a column which has thousands of them.
What i did was :
SELECT TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM DUAL)
The changes is PREVDT because all my julian date is in PREVDT column. However, im getting invalid identifier.....can anyone help me?
I also tried this but no luck:
TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD')))
You need to provide the table name in which your column PREVDT is present.
select TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM yourTable
Oracle DUAL table does not have the column PREVDT. What you want to do is run the query against your table, the one with column PREVDT.
SELECT TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM your_table_name;
The PREVDT values should be in the specified date format otherwise you will get an error.
Read more about the DUAL table here.
Oracle SQL automatically converts my field D.START_DT to the following format:
TO_CHAR(D.START_DT,'YYYY-MM-DD')
Which makes it difficult for me to modify my own date format.
I've tried wrapping another TO_CHAR around it with no luck.
TO_CHAR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 'MM/DD')
And I've tried SUBSTR to select certain characters, with no luck. I think the hyphen is getting int he way.
SUBSTR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 6, 7) || '/' || SUBSTR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 9, 10)
What is the work around for this?
I agree with RMAN Express and see no problems converting dates to any format you need...
In case you still have problems try this (first to_char() in outer query is optional):
SELECT to_char(to_date(some_date, 'YYYY-MM-DD'), 'MM/DD') final_date
FROM
(
SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') some_date -- this is your "auto converted" date
FROM dual
)
/
A DATE datatype has no format. When you see a date printed on a screen, there was something that APPLIED the format you see. Could be a "default" in the program you are using (like SQL Developer) or your NLS setting, etc. But, a DATE datatype has no format. So, you have complete control over the format you see on screen.
The simplest is to use the TO_CHAR function:
select TO_CHAR(D.START_DT,'YYYY') from dual;
returns just the four digit year.
See TO_CHAR date format options.
http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements004.htm#CDEHIFJA
You should always supply the format in your code and not rely on some other "default" to supply it.
I've been looking for a reason that this isn't working for quite some time. I'm concatenating two fields and attempting to run some date comparisons but it throws the error ORA-01843: not a valid month. I'm not sure what I'm doing wrong here.
Here's my code:
SELECT
sm.semester_date || cp.start_year AS effective
FROM
database.table cp,
database.table2 sm
WHERE cp.semesters_id = sm.semesters_id
AND to_date(sm.semester_date || cp.start_year, 'MM/DD/YYYY') >= to_date('06/01/2011', 'MM/DD/YYYY')
It runs fine as long as I don't add that AND statement at the end. But I need to filter the dataset.
When you run it without the filter it returns 08/15/2010 etc.
I forgot to add the mask originally when I posted this, I've corrected that. However, it still returns this error ORA-01840: input value not long enough for date format.
EDIT SOLUTION FOUND:
Firstly, thanks to everyone for helping me with this, you guys are great. Secondly, my error was being caused by a course without a start year. Very frustrating since that shouldn't be able to happen. Because the concatenated item had no year on it, it was throwing the error. I only found this because you guys helped me fix my code up. Thank you.
Try adding a separator between date and year:
SELECT
sm.semester_date || '/' || cp.start_year AS effective
FROM
database.table cp,
database.table2 sm
WHERE cp.semesters_id = sm.semesters_id
AND to_date(sm.semester_date || '/' || cp.start_year) >= to_date('06/01/2011', 'MM/DD/YYYY')
If you run the query without the filter, what does it return?
My guess is that your default date format does not match the format of the string that you are passing to TO_DATE. Add a format string as a second parameter to the call (as you've done for the second call) to specify what the format of the concatenated string is.
Is the SEMESTER_DATE column a VARCHAR2? And if so, is it a string in the format 'MM/DD/'? Or just 'MM/DD'?
As lweller points out, the first TO_DATE is also missing the format mask which would be a problem if your session's NLS_DATE_FORMAT is anything other than 'MM/DD/YYYY'.
Assuming SEMESTER_DATE is a VARCHAR2 in the format 'MM/DD', I suspect you want
AND to_date( sm.semester_date || '/' || cp.start_year, 'MM/DD/YYYY' ) >=
to_date( '06/01/2011', 'MM/DD/YYYY' )
Your first TO_DATE() relies in the system's default format. Provide a second argument with a proper format string, such as 'MM/DD/YYYY'. Please compare:
SELECT SEMESTER_DATE || START_YEAR AS BARE_STRING, TO_DATE(SEMESTER_DATE || START_YEAR) AS CASTED_TO_DATE
FROM (
SELECT '12/31' AS SEMESTER_DATE, 2010 AS START_YEAR
FROM DUAL
);
SELECT SEMESTER_DATE || START_YEAR AS BARE_STRING, TO_DATE(SEMESTER_DATE || START_YEAR, 'MM/DD/YYYY') AS CASTED_TO_DATE
FROM (
SELECT '12/31' AS SEMESTER_DATE, 2010 AS START_YEAR
FROM DUAL
);
I'm working on a .Net WinForms appliation that is connecting to a legacy RDB database...
Some of the fields for dates are stored as integers (imagine 2010-01-04 was the integer 20100104)
In the .Net world, I'd much rather work with datetime objects and I've been able to convert the integer into a date, just that it looks so ugly, takes up lots of lines, is prone to errors and I'm wondering if anyone would be able to improve it...Thanks heaps!
Note - I cannot edit the database so creating any form of "function" is out of the question...
Here's my current way of selecting the integer as a datetime:
select
CAST(
SUBSTRING(DATE_COL AS VARCHAR(8)) FROM 1 FOR 4) ||
'-' ||
SUBSTRING(CAST(DATE_COL) AS VARCHAR(8)) FROM 5 FOR 2) ||
'-' ||
SUBSTRING(CAST(DATE_COL) AS VARCHAR(8)) FROM 7 FOR 2) ||
' 00:00:00'
AS TIMESTAMP) AS DATE_COL
from MY_TABLE
where ...
It's been a while since I had to mess with Rdb. I seem to recall that there was a way to convert to a TIMESTAMP datatype by doing something like
CAST('YYYYMMDDHHMMSSTT' AS TIMESTAMP)
WHERE 'YYYYMMDDHHNNSSTT' was a character string in year-month-day-hour-min-sec-fraction format. You may need to use DATE ANSI here instead of TIMESTAMP - like I say, it's been a while. Regardless, the following might work:
SELECT CAST((CAST(DATE_COL AS CHAR(8)) || '00000000') AS TIMESTAMP)...
which is still ugly but is perhaps better than all the substringing. Anyways, play with it a bit and I'm sure you'll get it.
In Oracle, you can use the TO_DATE after you've cast the date_col to a string:
TO_DATE(TO_CHAR(date_col), 'YYYYMMDD')
...or 'YYYYDDMM' for the date format.
References:
TO_DATE
TO_CHAR
Why not use LINQ library? It is very nice and powerful.
What are the typical queries that you are running (high-level pseudocode please).