How to extract week number in sql - sql

I have a transdate column of varchar2 type which has the following entrees
01/02/2012
01/03/2012
etc.
I converted it in to date format in another column using to_date function. This is the format i got.
01-JAN-2012
03-APR-2012
When I'm trying to extract the weekno, i'm getting all null values.
select to_char(to_date(TRANSDATE), 'w') as weekno from tablename.
null
null
How to get weekno from date in the above format?

After converting your varchar2 date to a true date datatype, then convert back to varchar2 with the desired mask:
to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW')
If you want the week number in a number datatype, you can wrap the statement in to_number():
to_number(to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW'))
However, you have several week number options to consider:
WW Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.

Try to replace 'w' for 'iw'.
For example:
SELECT to_char(to_date(TRANSDATE, 'dd-mm-yyyy'), 'iw') as weeknumber from YOUR_TABLE;

Select last_name, round (sysdate-hire_date)/7,0) as tuner
from employees
Where department_id = 90
order by last_name;

Use 'dd-mon-yyyy' if you are using the 2nd date format specified in your answer. Ex:
to_date(<column name>,'dd-mon-yyyy')

Related

How can I convert a string quarter year to a timestamp in Databricks SQL

In Databricks SQL, how can I convert a date in string format "2021Q2" to a timestamp with the last day of that quarter?
select
to_timestamp(
last_day(
to_date(
(left('2021Q4',4)||'-'||int(right('2021Q4',1)*3))||'-'||'1')))
from
my_table
It is a pity that Q is not working in formatting from string to date object (it is working only in reverse) - parsing Q with to_date(date, "YYYY'QQ") sadly will not work.
According to https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html:
Symbols of ‘E’, ‘F’, ‘q’ and ‘Q’ can only be used for datetime formatting, e.g. date_format. They are not allowed used for datetime parsing, e.g. to_timestamp.
Because of that we have to separate quarter and multiple by 4. Than convert it to date object (parse_ and take last_date of the month:
SELECT
last_day(
to_date(
concat(left("2021Q4", 4), int(right("2021Q4", 1))*3),
"yyyyMM")
) as last_day_of_quarter
Simple way :
select to_timestamp(last_day(concat('2021','-',0,4*3,'-01'))) as last_date_queter
Logic :
calculate the last month of quarter by using multiple with 3. example 4th quarter's last month calculated 12 (4*3)
concat (year,'-',-01) so that we can get the first day of respective month 2021-12-01
last_day we can use last date of the given date month.
finally , we can convert the date into timestamp to_timestamp

Getting day of week from date column in prestosql?

I have a date column called day such as 2019/07/22 if I want to create a custom field that translates that date to the actual day of week it is such as Sunday or Monday how is this possible? I cant seem to find a method that works for presto sql.
Thanks for looking
You can use the format_datetime function to extract the day of week from a date or timestamp:
SELECT format_datetime(day, 'E')
FROM (
VALUES DATE '2019-07-22'
) t(day)
produces:
_col0
-------
Mon
If you want the full name of the day, use format_datetime(day, 'EEEE'):
_col0
-------
Monday
You can try extract('day' from day ) as day_name.

HOW TO FETCH DATA BETWEEN 2 DATES IN ORACLE SQL DEVELOPER

I'm new to oracle sql . I want to fetch data between 2 dates .
Date is in this format in db : 13-DEC-10
This is the query I have written but its giving me error . How to proceed next
select sum(TOTAL_AMOUNT) from table a
where trn_date between
TO_DATE(01-APR-17, 'DD-MON-YYYY') AND TO_DATE(31-MAR-17, 'DD-MON-YYYY') ;
A date does not have a format - it is stored internally to the database as 7-bytes (representing year, month, day, hour, minute and second) and it is not until whatever user interface you are using (i.e. SQL/Plus, SQL Developer, Java, etc) tries to display it to you, the user, and converts it into something you would find meaningful (usually a string) that the date has a format.
To fix your query you just need to surround the date string in single quotes and to use YY to match the 2-digit year format (otherwise Oracle will assume that 17 in the format YYYY is the year 0017 and the century will not be as you expect):
select sum(TOTAL_AMOUNT)
from table a
where trn_date between TO_DATE('01-APR-17', 'DD-MON-YY')
AND TO_DATE('31-MAR-17', 'DD-MON-YY');
However, you can also use date literals (and skip having to match the date format model):
select sum(TOTAL_AMOUNT)
from table a
where trn_date between DATE '2017-04-01'
AND DATE '2017-05-31';
Alternatively you may use the year format of RR format against centurial problems, Don't forget to keep quotes for date values, and you may prefer calling sql with bind variables :
select sum(TOTAL_AMOUNT)
from table a
where trn_date between
TO_DATE('&date_1', 'DD-MON-RR') AND TO_DATE('&date_2', 'DD-MON-RR') ; -- where date_1 is 31-MAR-17 and date_2 is 01-APR-17, in your case.
What I mentioned by centurial problems :
The RR Datetime Format Element
The RR datetime format element is similar to the YY datetime format
element, but it provides additional flexibility for storing date
values in other centuries. The RR datetime format element lets you
store 20th century dates in the 21st century by specifying only the
last two digits of the year.
If you use the TO_DATE function with the YY datetime format element,
then the year returned always has the same first 2 digits as the
current year. If you use the RR datetime format element instead, then
the century of the return value varies according to the specified
two-digit year and the last two digits of the current year.
That is:
If the specified two-digit year is 00 to 49, then
If the last two digits of the current year are 00 to 49, then the
returned year has the same first two digits as the current year.
If the last two digits of the current year are 50 to 99, then the
first 2 digits of the returned year are 1 greater than the first 2
digits of the current year.
If the specified two-digit year is 50 to 99, then
If the last two digits of the current year are 00 to 49, then the
first 2 digits of the returned year are 1 less than the first 2 digits
of the current year.
If the last two digits of the current year are 50 to 99, then the
returned year has the same first two digits as the current year.

Oracle date conversion outputs 2000s instead of required 1990s

During execution of below query, the output is 2090.
select to_char(to_date('10-OCT-90', 'DD-MON-YY'), 'YYYY') from dual;
Required output is 1990, the date in the snippet is Birth Date.
EDIT:
The input date is fetched from a table, so the date is in DD-MON-YY format.
For alternative resolving the double-digit years in strings, Oracle has the RR format element. Thus, your query would be
select to_char(to_date('10-OCT-90', 'DD-MON-RR'), 'YYYY') from dual;
From the referenced Oracle doc:
The RR datetime format element is similar to the YY datetime format element, but it provides additional flexibility for storing date values in other centuries. The RR datetime format element lets you store 20th century dates in the 21st century by specifying only the last two digits of the year.
Use RR instead of YY. See here: https://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm#SQLRF00215
Try to use RR like this:
select to_char(to_date('10-OCT-90', 'DD-MON-RR'), 'YYYY') from dual;
SQL FIDDLE DEMO
Also note that you should store the dates in YYYY format as it will be confusing to make the correct assumption as to the birth year is in 2000 or 1900 or 1800 or... ;)
The Oracle docs says:
YY allows you to retrieve just two digits of a year, for example, the
99 in 1999. The other digits (19) are automatically assigned to the
current century. RR converts two-digit years into four-digit years by
rounding.
50-99 are stored as 1950-1999, and dates ending in 00-49 are stored as
2000-2049. RRRR accepts a four-digit input (although not required),
and converts two-digit dates as RR does. YYYY accepts 4-digit inputs
butdoesn't do any date converting
I had the same issue for my Date column (DOB) if DOB is 27-08-1947. I use below conversion format to get correct year to calculate age.
TO_DATE(TO_CHAR(DOB,'DD-MON-YYYY'), 'DD-MON-YYYY')
TO_DATE(TO_CHAR(DOB,'DD-MM-YYYY'), 'DD-MM-YYYY')
SELECT TRUNC(SYSDATE) TODAY,
TO_DATE(BIRTH_DATE,'DD-MON-RR') DOB,
BIRTH_DATE
FROM TM_DM_CLIENT
WHERE CLIENT_NO IN ('22943179')
output
Today DOB BIRTH_DATE
10/1/2019 1/1/2041 1/1/1941

Get name of weekday in netezza

I can get week number using extract dow function in netezza for a date.
Select extract(Dow from date) from table
How can I get name of the weekday?
I haven't tried with a date datatype, but to get the day name from a timestamp you would use
select to_char(date, 'Day') from table
That should give results of Sunday, Monday, Tuesday, etc. Try it with your date column and please let us know if it works.