SQL snipets


Query to get comma separated values into each row

SELECT TRIM(regexp_substr(:l_comma_code,'[^,]+', 1, level)) Comma_col_value
                           FROM dual
                     CONNECT BY regexp_substr(:l_comma_code,'[^,]+', 1, level) is not null;


=>Query to get all dates in a month for a given date:

    SELECT  TRUNC(TO_DATE('2024-01-01', 'YYYY-MM-DD'), 'MM') + LEVEL - 1 AS day_in_month
    FROM  dual CONNECT BY 
    TRUNC(TO_DATE('2024-01-01', 'YYYY-MM-DD'), 'MM') + LEVEL - 1 <= LAST_DAY(TO_DATE('2024-01-01', 'YYYY-MM-DD'));

=>SQL to get ISO week and month for date:

select TO_CHAR(SYSDATE, 'IW') AS wk_Bucket, TO_CHAR(SYSDATE, 'MM') AS MN_Bucket
 FROM DUAL;

=>SQL query to get last ISO week for a given month and year

SELECT TO_CHAR(LAST_DAY(TO_DATE(:month_num || '-' || :year_num, 'MM-YYYY')), 'IW') AS last_iso_week FROM DUAL;

=> while executing / compiling a sql script with '&" values within it and still not interpret as input value: 
SET DEFINE OFF;  -- to avoid accepting input values for '&'


for example: set define off will allow executing the script without any prompt to enter value

update fnd_user
set description = 'SAM & NIK'
WHERE USER_ID = 123;


Comments

Popular posts from this blog

BOM Open Interface

Supplier SQL query

PO & Receipt Query