DATE Manipulation - sql

I would appreciate any pointers with this, I'm trying to ultimately get the Day of the week for each date. Unfortunately my DATESTRG in format 02-JUL-13 is ending up as 13/07/0002 rather than 02-07-2013 ( European date format ), and I get an error when I try to get the Day of Week DOW. Thank you.
WITH DATEDATE AS
(
SELECT
SUBSTR ( SRT.CREATED_DATE,1,10) AS DATESTRG
FROM SMS.REVIEW_TEXT SRT
)
SELECT
DATESTRG,
TO_DATE ( DATESTRG, 'YYYY-MM-DD' )
TO_CHAR ( DATE DATESTRG, 'DY') AS DOW
FROM DATEDATE

Try this:
WITH DATEDATE AS
(
SELECT
SUBSTR ( '2017-09-08',1,10) AS DATESTRG
FROM dual
)
SELECT
TO_CHAR (to_date(DATESTRG,'YYYY-MM-DD'), 'DY') AS DOW
FROM DATEDATE

Related

SQL BigQuery - How to convert date(2020-11-14 16:04:15 UTC) to WEEKDAY value (0-6)

I am currently working through this. Here's some relevant documentation
I have a time date column in { 2020-11-14 16:04:15 UTC } format. I want to end up with a column with an integer 1-7 corresponding to the day of the week. 1 = Sunday and 7 = Saturday.
Currently, I have this that works for a specific date.
SELECT EXTRACT(DAYOFWEEK FROM DATE '2013-12-25') AS the_day
How could I apply this function to a whole column?
thank you for any help.
SQL BigQuery
Below is the example.
WITH
org_table AS (
SELECT DATE('2013-12-25') as org_col
UNION ALL SELECT '2013-12-26'
UNION ALL SELECT '2013-12-28'
UNION ALL SELECT '2013-12-29'
)
SELECT
org_col,
EXTRACT(DAYOFWEEK FROM org_col) AS the_day
FROM org_table
ORDER BY org_col
;
p.s.
To apply with your existing query, replace org_table.
WITH
org_table AS (
SELECT `your_column` as org_col
FROM `your_table`
)
...
Adapted from the documentation of date functions 'Extract' example 2.
https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions?hl=ar-YE#extract
SELECT
date,
EXTRACT(DAYOFWEEK FROM date) AS the_day
FROM UNNEST(GENERATE_DATE_ARRAY('2015-12-23', '2016-01-09')) AS date
ORDER BY date;

Date Time to String Snowflake

Need to convert a Snowflake TIMESTAMP_TZ(9) to String format,but throwing this error
"Date '27/02/2020' is not recognized"
Tried all of these:
TO_CHAR( date, 'DD-MM-YYYY') as date,
TO_VARCHAR(date, 'DD/MM/YYYY') as date,
TO_CHAR( date, 'DD.MM.YYYY') as date,
I can reproduce the problem if I write a query like this:
select to_char(date, 'DD-MM-YYYY')
from (
select '27/02/2020'::date date
)
-- Date '27/02/2020' is not recognized
And I can fix that query with this:
select to_char(date, 'DD-MM-YYYY')
from (
select to_date('27/02/2020', 'DD/MM/YYYY') date
)
So if that's your problem, first you need to parse the dates with to_date() in that particular format.

How to find the first date of a month from a given datetime dataset (in SQL Oracle)?

Does any in-built function exists for that please?
I've created this one;
to_char( add_months( ( last_day( sysdate ) + 1 ), -1 ), 'dd/mm/yyyy' )
Thanks in advance. Chri$
You can use TRUNC() with a date format argument:
select trunc(sysdate, 'MON') from dual

get first day of a given week number in oracle

Please help to derive first day of a given week_no in oracle not from given date.
You can try following query:-
SELECT NEXT_DAY(MAX(d), 'SUN') REQUESTED_SUN
FROM (SELECT TO_DATE('01-01-2015', 'DD-MM-YYYY') + (ROWNUM-1) d FROM DUAL CONNECT BY LEVEL <= 366)
WHERE TO_CHAR(d, 'WW') = Your_Desired_WEEK_NO-1;
This might be helpful to you.
Use this query
Select TRUNC (Trunc(sysdate,'yyyy')+(:num-1)*7,'IW') from duaL;
:num is number of week from year 2015, or put year what you need instead of sysdate.
You can use this function to get the date of the ISO week:
CREATE FUNCTION TO_ISO_WEEK_DATE(
week NUMBER,
year NUMBER
) RETURN DATE DETERMINISTIC
IS
BEGIN
RETURN NEXT_DAY(
TO_DATE( TO_CHAR( year, '0000' ) || '0104', 'YYYYMMDD' )
- INTERVAL '7' DAY, 'MONDAY'
)
+ ( week - 1 ) * 7;
END TO_ISO_WEEK_DATE;
/

How can I extract and display the date range based on Quarter in Oracle?

I'm building a summary table as part of a stored procedure and I have two columns. The first column needs to show the start and the second column needs to show the end of a date range that is based from an input parameter that is a number designating the quarter. I was able to extract the following from AskTom but I have some questions.
Open C1 FOR
SELECT ( SELECT TRUNC (SYSDATE, 'Q')-1+1 AS 'StartOf' FROM DUAL ),
SELECT ( SELECT TRUNC(ADD_MONTHS (SYSDATE, +3), 'Q')-2 AS 'EndOf' FROM DUAL )
FROM DUAL;
Question 1. Will the Math here account for LeapYears... I don't think it will but I'm not sure how to handle that.
Question 2. How do I add the input parameter, 'inQuarter' as the specific quarter? I've tried putting it in place of sysdate but I need to reformat it into date first I think?
Thanks in advance for any responses.
Tom Kyte has givven you the answer:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:250288900346075009
Open C1 FOR
select add_months( dt, (inQuarter-1)*3 ),
last_day(add_months( dt, (inQuarter-1)*3+2 ) )
from (
select to_date( inYear || '0101', 'yyyymmdd' ) dt
from dual)
You can convert a numeric year and numeric quarter parameter to a DATE
SELECT add_months( trunc( to_date( to_char( <<numeric year>> ),
'YYYY' ),
'YYYY' ),
3 * <<numeric quarter>> ) first_of_quarter,
add_months( trunc( to_date( to_char( <<numeric year>> ),
'YYYY' ),
'YYYY' ),
4 * <<numeric quarter>> ) - 1 last_of_quarter,
FROM dual
The time component of both dates will be midnight on the last day of the quarter (i.e. 24 hours before the beginning of the next quarter). You may want the last of the quarter to be 23:59:59 on the last day of the quarter if you want the range to be inclusive of all possible dates in the quarter.
I suggest:
Open C1 FOR
SELECT TRUNC (d_inQuarter, 'Q') AS "StartOf",
TRUNC(ADD_MONTHS (d_inQuarter, +3), 'Q') AS "EndOf"
FROM (SELECT add_months(to_date(to_char(i_yr)||'-01-01','YYYY-MM-DD'), (i_q-1)*3)
AS d_inQuarter FROM DUAL);
- with integer parameters i_yr and i_q representing the year and quarter, respectively.
Note that EndOf will represent midnight on the first day of the next quarter, so any selection should be based on conditions < "EndOf", not <= "EndOf". (This should ensure that all times on the last day of the quarter are included.)