Extract values from IN parameter and store it in local variables in postgreSQL - sql

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

Related

SQL group by 7am to 7am

How do I simply group by a 24 hour interval from 7am to 7am in a manner similar to:
select format(t_stamp,'yyyy-MMM')
from mytable
group by format(t_stamp,'yyyy-MMM')
if input is like
3,Wed Mar 23 20:40:40 EDT 2022
3,Wed Mar 23 20:40:39 EDT 2022
4,Wed Mar 23 03:36:10 EDT 2022
3,Wed Mar 22 15:46:44 EST 2022
3,Tue Mar 22 04:16:52 EST 2022
4,Sat Mar 22 03:13:08 EDT 2022
3,Sat Mar 22 03:13:05 EDT 2022
4,Sat Mar 21 04:10:36 EDT 2022
output should be like
6, Mar 23
7, Mar 22
10, Mar 21
4, Mar 20

Databricks: replicate columns

Suppose I am having the following Dataframe :
YEAR MONTH Value
2019 JAN 100
2019 JAN 200
2019 MAR 400
2019 MAR 100
And I do the pivot group by YEAR. ( df.groupBy().pivot()....)
YEAR JAN MAR
2019 300 500
But I also wanted to replicate the column of the Months through out the year even there are no data in that month ...
which means I would like to have
YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
2019 300 0 500 0 0 0 0 0 0 0 0 0
Thanks

Convert or cast varchar rows like (Mon Jul 18 19:28:36 EDT 2018) To DateTime

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

presto sql: select the data that before or after a datetime

There is table called t1, and there are columns id, created_at, text, for example, as following table:
id created text
1 Thu Jun 30 01:00:57 +0000 2016 I like this movie1
2 Thu Jun 30 02:59:57 +0000 2016 I like this movie2
3 Thu Jun 30 03:49:57 +0000 2016 I like this movie3
4 Thu Jun 30 04:59:50 +0000 2016 I like this movie4
5 Thu Jun 30 05:39:57 +0000 2016 I like this movie5
6 Thu Jun 30 06:39:57 +0000 2016 I like this movie6
7 Thu Jun 30 06:29:57 +0000 2016 I like this movie6
8 Thu Jun 30 07:09:57 +0000 2016 I like this movie7
9 Thu Jun 30 07:39:57 +0000 2016 I like this movie8
10 Thu Jun 30 08:39:57 +0000 2016 I like this movie9
11 Thu Jun 30 09:39:57 +0000 2016 I like this movie10
12 Thu Jun 30 10:29:57 +0000 2016 I like this movie11
13 Thu Jun 30 11:29:57 +0000 2016 I like this movie12
12 Thu Jun 30 12:29:57 +0000 2016 I like this movie13
I want to select data separated by hour time.
For example, I want to select all the data that hour is less or equal 06, then I want to select the data that hour is more than 07. Since the data of column is datetime form: Thu Jun 30 12:29:57 +0000 2016, I don't know how to deal with this. Thanks for your help!
The sql is presto(presto sql):
select id, created, text from t1 where created_at <= 6
You could use datepart for this if you are using mssql:
select
id,
created,
text
from
t1
where
datepart(hour, created) <= 6
References:
DATEPART (Transact-SQL)
I done it, use the hour(datestamp)can solve it.
select id, created, text from t1 where hour(created_at) <= 6

Changing date format for PostgreSQL query result

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