SQL Query / Regular Expression to Split Custom string into columns - sql

Hi I have source data in a field in different formats like below
1Y3M6D (1 Year, 3 Months, 6 Days) I need to split this into 3 fields Year, Month, Days but the source data format can change like month can come first as 3M1Y6D OR source data can only have 3M with no year and day. How do I write a query to get the preceding number from M, Y or D?
Thanks in advance for help.

Thanks everyone, Unoembre command helped.
select my_value, REGEXP_SUBSTR(my_value,'(\d+)Y',1,1,NULL,1) REG_Y, REGEXP_SUBSTR(my_value,'(\d+)M',1,1,NULL,1) REG_M, REGEXP_SUBSTR(my_value,'(\d+)D',1,1,NULL,1) REG_D from ( select '3M6Y2D' my_value from dual );

Related

Extracting date in SQL

I have a column due date in the format 20210701 (YYYYMMDD), using SQL I want to extract all the dates apart from 5th of particular month ( As highlighted in the pic below )
I used the below code:
SELECT Due_Date_Key
FROM Table
WHERE Due_Date_Key <>20210705
However the error in the above code is it will exclude only the month of jul but not for other months.
How can extract the dates apart from 5th from the entire column.
Help would be much appreciated.
Note that column DUE_DATE_KEY is numeric.
A more SQLish way would be to convert string to date and then check if day is not 5
SELECT * FROM Table
WHERE DATE_PART('day', to_date(cast(DUE_DATE_KEY as varchar), 'YYYYMMDD')) != 5
Using modulo operator to determine whether the last two digits of DUE_DATE_KEY are 05.
select * from T where DUE_DATE_KEY % 100 <> 5
Using your sample data, the above query returns the following:
due_date_key
20210701
20210708
20210903
Refer to this db fiddle

How to calculate difference of dates in different formats in Snowflake?

I am merging 2 huge tables in Snowflake and I have 2 columns (one on each table):
"Year_birth" and "Exam_date" and the info inside looks like this respectively:
"1918" and "2007-03-13" (NUMBER(38,0) and VARCHAR(256))
I only want to merge the rows where the difference (i.e., age when the exam was made) is ">18" and "<60"
I was playing around with SELECT DATEDIFF(year,Exam_date, Year_birth) with no success.
Any ideas on how would I do it in Snowflake?
Cheers!
You only have a year, so there is not much you can do about the specific day of the year -- you need to deal with approximations.
So, extract the year from the date string (arggh! it should really be a date) and just compare them:
where (left(datestr, 4)::int - yearnum) not between 18 and 60
I would strongly advise you to fix the database and store these values using a proper date datatype.
You will need to convert the integer year into a date before doing a datediff
example:
set YearOfBirth = 1918;
set ExamDate = '2007-03-03'::DATE;
-- select $YearofBirth as YearofBirth, $ExamDate as ExamDate;
select $YearofBirth as YearofBirth,($YearofBirth::TEXT||'-01-01')::DATE as YearofBirthDate, $ExamDate as ExamDate, datediff(year,($YearofBirth::TEXT||'-01-01')::DATE,$ExamDate) as YearsSinceExam;
USE YEARS_DIFF IN WHERE CLAUSE TO FILTER DIFFERENCE BETWEEN 18 & 60
SELECT DATEDIFF( YEAR,'2007-03-03',TO_DATE(2018::CHAR(4),'YYYY')) YEARS_DIFF;

How do I average the last 6 months of sales within SQL based on period AND year?

How do I average the last 6 months of sales within SQL?
Here are my tables and fields:
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR,
And I need to average these fields
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
The hard part I'm having is understanding how to average the last whole 6 months, ie. fsicalcalperiod 2-6(inside fiscalcalyear 2017).
I'm hoping for some help on what the SQL command text should look like since I'm very new to manipulating SQL outside of the UI.
Sample Data
My Existing SQL String:
SELECT IM_ItemWhseHistoryByPeriod.ITEMCODE,
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR
FROM MAS_AME.dbo.IM_ItemWhseHistoryByPeriod
IM_ItemWhseHistoryByPeriod
ScaisEdge Attempt #1
if fiscalyear and fiscalperiod are number you could use
select avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
or for each item code
select itemcode, avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
group by itemcode
Try the following solution and see if it works for you:
select avg(DOLLARSSOLD) as AvgDollarSod,
avg(QUANTITYSOLD) as AvgQtySold
from IM_ItemWhseHistoryByPeriod
where FISCALCALYEAR = '2017
and FISCALCALPERIOD between 2 and 6

Oracle SQL. Display data for fixed number of periods from a dynamic start period

Here is the query I have: I would like to display 6 periods back and 12 periods forward from the value in the PERIOD column:
SELECT
item_id,
min(period),
min(picks_class),
min(vau_class),
min(warehouse_code),
min(stocked)
FROM
(select
i.warehouse_code,
ih.item_id,
ih.picks_class,
ih.period,
ih.vau_class,
ih.stocked,
substr(i.pareto_set, 6, 6) AS "Policy"
from d_item_history ih
join d_item_snapshot i on ih.item_id=i.id
where ih.picks_class='P2'
and i.supplier_code='DC'
and ih.valid_for_calc='Y'
and ih.vau_class in ('C1', 'C2')
and i.warehouse_code='YAFD'
-- and ih.item_id='1427084842208'
order by ih.period asc
)
group by item_id
;
Here is the output of the code: I would like to display the picks class for each one of the ITEM_IDs; 6 periods back from queried period and 12 months forward.
Please let me know if that is at all possible if/or how. All help is appreciated
When you want to calculate 6 periods back or 12 periods forward, Are you looking to move by months ? If so, the following code snippet might help you :
select to_char(add_months(to_date(period,'YYYYMM'),-6),'YYYYMM') PERIOD_6,
to_char(add_months(to_date(period,'YYYYMM'),12),'YYYYMM')PERIOD_12
from dual

Date in sql database

i want to access date from my table and i only want only particular dates to display (for eg: date=1) .
Is there any way to access date,month and year individually from date format?
please help
Avinash is this what you are looking for ?
select MONTH('10/15/1981') --10
select DAY('10/15/1981') --15
select YEAR('10/15/1981') --1981
Select Dates
from tablename
Where MONTH(Dates) = 10