I need to convert date format in ORACLE SQL Developer
The current format is yyyy/mm/dd-hh:mm:ss:sss and I need to convert it to yyyy-mm-dd hh:mm:ss CST
I don't really know SQL but did some research.
Here is the command that I consultanted other people on the forum. but it throws me unrecognized command error. table name is B and column name is First
UPDATAE B
set First = concat(to_char(substring(FIRST,1,4) + '-' + substring(FIRST, 6, 2) + '-' + substring(FIRST, 9, 2) + ' ' + substring(FIRST, 12, 8));
Could anyone here help me with it? thanks in advance.
The "unrecognized command" is merely a misspelling of UPDATE:
UPDATAE B
// Should be
UPDATE B
To verify the result is what you expect before executing the UPDATE statement, use a SELECT:
SELECT
to_char(substr(FIRST,1,4) || '-' || substr(FIRST, 6, 2) || '-' || substr(FIRST, 9, 2) || ' ' || substr(FIRST, 12, 8)) AS Test
FROM B
Umm... I'm either missing something extremely obvious or everyone else is.
You want to date operations? Use to_date and to_char. I'm going to assume this ss:sss means, seconds, then fractional seconds. You date appears to be a string so we need to convert it twice:
update b
set first = to_char( to_date( my_date, 'yyyy/mm/dd-hh:mi:ss:ff3')
,'yyyy-mm-dd hh:mi:ss' )
Generally, when using dates it's far, far easier to only use date functions and the provided formats.
As an added point if you have a date, store it as a date. It'll save a world of bother later on.
Related
I have an SQLite database containing dates in the following format: 20190621 22:35:18. I'm trying to cast this one into DATETIME to further manipulate it.
Thing is, running the following:
strftime('%Y%m%d %H:%M:%S', my_datetime)
Returns null
I've tried understanding what's going on, by trying to "breaking" the string into smaller parts. So I've run the following:
strftime('%Y', substr(my_datetime, 0, 5))
This time, I've got -470. (Where I would expect to get 2019)
Adding month using the same logic returns: -41609. (Where I would expect to get 2019-06)
Adding any additional information returns null. So I guess that the problem is the way strftime reacts to the year. I have no idea why would SQLite act this way. Any ideas what to do?
Thanks to the comments I understood that SQLite only work with the format: YYYY-MM-DD hh:mm:ss
So I've converted the string to the relevant format using:
UPDATE my_table set my_datetime = substr(my_datetime, 0, 5) || '-' || substr(my_datetime, 5, 2) || '-' || SUBSTR(my_datetime, 7, 2) || SUBSTR(my_datetime, 9);
And now everything works.
Hello guys someone can help me converting this query to work on Oracle ?
SELECT CONVERT(VARCHAR(10),
CAST(DATEADD(DAY,CONVERT(INT,
Convert(nvarchar(50),(ASCII(SUBSTRING(A1_USERLGI,12,1)) - 50))+Convert(nvarchar(50),(ASCII(SUBSTRING(A1_USERLGI,16,1)) - 50))),
'1996-01-01') AS DATETIME),103) FROM SA1010 where A1_USERLGI <> ' ';
This code for decoding a econded field that's save user from system cod and the date of change.
Just with a few changes the Sql can be put in the Oracle flavor.
CONVERT of the date to string becomes TO_CHAR.
SUBSTRING becomes SUBSTR.
+ becomes || or CONCAT.
DATEADD of n days to a date becomes date + n
ASCII remains ASCII.
CAST works for both.
SELECT
TO_CHAR((CAST('01-JAN-1996' AS DATE) +
CAST(CONCAT((ASCII(SUBSTR(A1_USERLGI, 12, 1))-50),
(ASCII(SUBSTR(A1_USERLGI, 16, 1))-50)) AS NUMBER)), 'DD/MM/YYYY') AS dt
FROM SA1010 where A1_USERLGI <> ' ';
My DB contains a period(month) and a year - I am trying to convert it to a date. I don't care for the actual day of the month so I have just made it "1" (the 1st of the month).
In my code I had to convert the "13th" period to the 12th because of the 12 months of the year, so my decode did that part... Ultimately, I want it to return as a date. I have a concatenation to make it 'look' like a date, but not actually a date.
What i do with the data is query it and return it in Excel for further manipulation. When imported to Excel, it does not import as a date nor does it let me convert to a date format.
SELECT DIA_PROJECT_DETAIL.FY_DC || '/' ||
decode(DIA_PROJECT_DETAIL.PER_DC,1,1,2, 2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,12)||
'/01' as "Date"
FROM AMS.DIA_PROJECT_DETAIL DIA_PROJECT_DETAIL
There has to be an easier or more effective way to do this!
There is no much simpler way. DECODE is fine for converting month 13 to month 12, but you use it a bit too complicated. Then you shouldn't rely on session settings, but explicitly tell the DBMS the date format your assembled string represents. Use TO_DATE with the appropriate format.
select
to_date(fy_dc || to_char(decode(per_dc, 13, 12, per_dc), '00') || '01', 'yyyymmdd')
as "Date"
from ams.dia_project_detail dia_project_detail;
Just use least():
SELECT (DIA_PROJECT_DETAIL.FY_DC || '/' ||
LEAST(DIA_PROJECT_DETAIL.PER_DC, 12) ||
'/01'
) as "Date"
FROM AMS.DIA_PROJECT_DETAIL DIA_PROJECT_DETAIL;
I have a set of data that I am importing into SqlLite, but don't really have the opportunity to manipulate it before insertion. I am trying to calculate "age" of the date, but this is proving very difficult in its current format.
I am looking for a select that I can use to update the data and then begin writing queries the way I want.
Data Samples
9/20/1983
2/18/1986
8/1/1994
5/29/1999
Desired
1983-09-20
1986-02-18
1994-08-01
1999-05-29
Once I have data in that format, I will calculate the date using the following
(strftime('%Y', 'now') - strftime('%Y', Born)) - (strftime('%m-%d', 'now') < strftime('%m-%d', BOrn))
I guess if there's a way to cast the date into the right format and calculate the age in one query, that would save a step, but I haven't been able to find a way so far.
You could use the following update statement to change the date format to YYYY-MM-DD:
update t
set Born =
substr(Born, -4) || '-' ||
substr('0' || substr(Born, 1, instr(Born, '/')-1), -2) || '-' ||
substr('0' || substr(Born, instr(Born, '/')+1,
length(Born)-5-instr(Born, '/')), -2)
where substr(Born, -5, 1) = '/'
and Born LIKE '%/%/%'
The where clause is there to only update dates that have a d/m/yyyy format, where d and m could be two digits as well.
I have a number of corrupt Dates in an Advantage database, but luckily I have a reverse date field which is intact.
I need to use SQL to recreate the Date field in the form 'DD/MM/YYYY' from the RDate string 'YYYYMMDD'.
Something equivalent to:
UPDATE table SET Date = FormatDate('DD/MM/YY',StrToDate('YYYYMMDD',RDate))
I don't know if this is possible to select character positions and use variables this way in SQL or if I'll have to write a program (in Delphi) to do the operation.
Thanks
Try something like
update table set
date = cast(substring(rdate, 5, 2) + '/' +
right(rdate, 2) + '/' +
left(rdate, 4) as date)
/* where ... */
(assuming your connection's date format setting is the default 'MM/DD/YYYY')
TOndrej I'm not sure if it was a syntax error or if cast is not recognised but I achieved the desired result with the following SQL:
update members set "Expiry Date" = ((substring(RExpiryDate,7,2)) + '/' + (substring(RExpiryDate,5,2)) + '/' + (substring(RExpiryDate,1,4)))
Thanks for your help