I have this value:
Thu Jan 01 01:00:00 UZT 1970
I need this value from the above value:
Thu Jan 1970
Need Substring from this value.
I have this value:
Thu Jan 01 01:00:00 UZT 1970
I need this value from the above value:
Thu Jan 1970
Need Substring from this value.
Looks like a string. If that's so, here's one (actually, two) options:
SQL> with test (col) as
2 (select 'Thu Jan 01 01:00:00 UZT 1970' from dual)
3 select substr(col, 1, instr(col, ' ', 1, 2)) || substr(col, -4) result,
4 regexp_substr(col, '(\w+ ){2}') || regexp_substr(col, '\d+$') result2
5 from test;
RESULT RESULT2
------------ ------------
Thu Jan 1970 Thu Jan 1970
SQL>
[EDIT]
How to convert it to a different format? By applying TO_DATE
(to convert "Thu Jan 1970", which is a string, to a valid DATE format), and then TO_CHAR
with appropriate format mask to get what you want.
SQL> with test (col) as
2 (select 'Thu Jan 01 01:00:00 UZT 1970' from dual),
3 inter as
4 (select substr(col, 1, instr(col, ' ', 1, 2)) || substr(col, -4) result,
5 regexp_substr(col, '(\w+ ){2}') || regexp_substr(col, '\d+$') result2
6 from test
7 )
8 select to_char(to_date(result, 'dy mon yyyy', 'nls_date_language = english'), 'dd-mm-yyyy') new_result
9 from inter;
NEW_RESULT
----------
01-01-1970
SQL>
Use TO_DATE
then TO_CHAR
:
SELECT TO_CHAR(
TO_DATE(
'Thu Jan 01 01:00:00 UZT 1970',
'Dy Mon DD HH24:MI:SS "UZT" YYYY'
),
'Dy Mon YYYY'
) AS substring
FROM DUAL
| SUBSTRING | | :----------- | | Thu Jan 1970 |
db<>fiddle here
If you want it as DD-MM-YYYY
format then change the format model used in TO_CHAR
to 'DD-MM-YYYY'
but if you are going to be storing it in the database it would be better to just store it as a date value rather than as a formatted string.
I have some columns "UZT" and some "UZST" then getting error on "UZST"
If those are the only time zones and you are not worried if there is an hours difference between the two zones then you can use:
SELECT TO_CHAR(
TO_DATE(
REPLACE( your_column, 'UZST', 'UZT' ),
'Dy Mon DD HH24:MI:SS "UZT" YYYY'
),
'Dy Mon YYYY'
) AS substring
FROM your_table
or if you need to account for the time zones (replace the time zone values as appropriate):
SELECT TO_CHAR(
TO_TIMESTAMP_TZ(
REPLACE(
REPLACE(
your_column,
'UZST',
'+05:00'
),
'UZT',
'+05:00'
),
'Dy Mon DD HH24:MI:SS TZH:TZM YYYY'
) AT TIME ZONE '+05:00',
'Dy Mon YYYY'
) AS substring
FROM your_table