I have following query
select substring(listDate from '............$') as v_end_date,
substring(listDate from '^...............') as v_start_date
Now listDate value can be like
select substring('06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
' from '............$') as v_end_date,
substring('06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
' from '^............') as v_start_date
Above query results in
V_END_DATE V_START_DATE
26 Jan 2014 06 Jan 2014
Now I need to have v_end_date and v_start_date format like yyyy-mm-dd and like
Mon 06 Jan 2014.
Convert your string to an actual date with to_date() and use to_char() to get pretty much any format you like.
Demo:
SELECT to_char(day, 'YYYY-MM-DD') AS format1
, to_char(day, 'Dy DD Mon YYYY') AS format2
FROM (SELECT to_date('26 Jan 2014', 'DD Mon YYYY') AS day) sub
Related
Looking for an Oracle SQL query to show Month and Year starting from the current year- 1y and current year+1y.
Eg: December 2019, January 2020, February 2020,......December 2021
You can use the hierarchy query as follows:
SQL> SELECT trunc(ADD_MONTHS(ADD_MONTHS(sysdate,-12), LEVEL-1), 'Mon') as month_year
2 FROM DUAL CONNECT BY LEVEL <= 24 + 1;
MONTH_YEAR
--------------
December 2019
January 2020
February 2020
March 2020
April 2020
May 2020
June 2020
July 2020
August 2020
September 2020
October 2020
November 2020
December 2020
January 2021
February 2021
March 2021
April 2021
May 2021
June 2021
July 2021
August 2021
September 2021
October 2021
November 2021
December 2021
25 rows selected.
SQL>
There are multiple methods for doing this. I think simple examples like this are a good opportunity to learn about recursive CTEs:
with dates(yyyymm, n) as (
select trunc(sysdate, 'Mon') as yyyymm, 1 as n
from dual
union all
select add_months(yyyymm, -1), n + 1
from dates
where n <= 12
)
select yyyymm
from dates;
WITH d AS (
SELECT
'JAN' m,
2021 y
FROM
dual
), d1 AS (
SELECT
to_date(m || y, 'MONYYYY') first_day,
last_day(to_date(m || y, 'MONYYYY')) last_day1,
last_day(to_date(m || y, 'MONYYYY')) - to_date(m || y, 'MONYYYY') no_of_days
FROM
d
)
SELECT
level - 1 + first_day dates
FROM
d1
CONNECT BY
level <= no_of_days + 1;
I have below values in a column
Q1 2018
Q2 2018
Q3 2018
Q4 2018
feb 2018
mar 2018
Q1 2019
Q2 2019
Q3 2019
jan 2018
sep 2018
dec 2018
jan 2019
feb 2019
mar 2019
I have above values which gets calculated on some parameters. for some data this value comes with month and for some this comes as quarter.
Is there any way to order them using order by when all the values are on the same column, means monthly values and quarterly value both should be sorted.
output should be like
Q1 2018
Q2 2018
Q3 2018
Q4 2018
Q1 2019
Q2 2019
Q3 2019
jan 2018
feb 2018
mar 2018
sep 2018
dec 2018
jan 2019
feb 2019
mar 2019
I think this does what you want:
order by len(col), -- put the quarters first
substr(col, 4), -- order by year
(case when col not like 'Q%'
then to_date(col, 'MON YYYY')
end), -- order months by date
col -- order quarters by quarter
I have a column varchar type with dates like:
Fri Mar 3 12:55:17 EST 2017
Thu Jul 27 10:12:07 EDT 2017
Fri Jul 21 12:11:35 EDT 2017
Wed Jan 31 13:15:34 EST 2018
And I would like to return just the date and time something like:
03/03/2017 12:55:17
07/27/2017 10:12:07
07/21/2017 12:11:35
01/31/2018 13:15:34
I tried several ways with substring and convert statement but nothing work.
Any assistance in this regard will be greatly appreciated.
Perhaps something like this
Example
Declare #YourTable table (SomeCol varchar(50))
Insert Into #YourTable values
('Fri Mar 3 12:55:17 EST 2017'),
('Thu Jul 27 10:12:07 EDT 2017'),
('Fri Jul 21 12:11:35 EDT 2017'),
('Wed Jan 31 13:15:34 EST 2018')
Select *
,AsDateTime = try_convert(datetime,substring(SomeCol,4,len(SomeCol)-11)+right(SomeCol,4))
From #YourTable
Returns
SomeCol AsDateTime
Fri Mar 3 12:55:17 EST 2017 2017-03-03 12:55:17.000
Thu Jul 27 10:12:07 EDT 2017 2017-07-27 10:12:07.000
Fri Jul 21 12:11:35 EDT 2017 2017-07-21 12:11:35.000
Wed Jan 31 13:15:34 EST 2018 2018-01-31 13:15:34.000
current SQL to get 20 Dec 2016 is:
Convert(char(11), getdate(), 13) DATE,
SELECT FORMAT(cast('20 DEC 2016' as date),'dd MMMM yyyy')
I have :listDate:String IN parameter that I'm passing to my proc which contains the dynamic values separated by ,
This :listDate:String is passed to proc from Java front end and it contains the value selected by the user
There can be many combinations
Case 1 listDate can have
1. 30 Dec 2013 to 05 Jan 2014
so v_start_date is 30 Dec 2013 and
v_end_date is 05 Jan 2014
Case 2 listdate can have
30 Dec 2013 to 05 Jan 2014,
06 Jan 2014 to 12 Jan 2014
so v_start_date is 30 Dec 2013 and
v_end_date is 12 Jan 2014
Case 3 listDate can have
06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
so v_start_date is 06 Jan 2014 and
v_end_date is 26 Jan 2014
How can I extract the values as shown above into v_start_date and v_end_date ?
I was able to resolve it by using
select substring(listDate from '............$') as v_end_date,
substring(listDate from '^...............') as v_start_date