I want to extract the week based on sysdate in Oracle SQL, what function do i use - sql

select datepart(year, '2017/08/25') as week;
I believe this is for mysql but does not work for oracle sql

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.
select to_char(trunc(sysdate),'WW') from dual;
Week of year (1-52 or 1-53) based on the ISO standard.
select to_char(trunc(sysdate),'IW') from dual;
Taken from Oracle docs

Related

local week of the year in postgresql

I have to generate year wise, weekly reports for some data. Now When I aggregate date on week number, and week number is calculated from extract from creation date.
Now the problem is these both queries return week number 52.
SELECT EXTRACT(WEEK FROM TIMESTAMP '2006-01-01');
SELECT EXTRACT(WEEK FROM TIMESTAMP '2006-12-31');
First query return 52 (52nd week of 2005) and 2nd query return 52 (52nd week of year 2006). thats documented behavior.
But I want to Calculate local week number, and results for first query should be 1 and other query would return 53.
You can't do this with the exctract() function, it only supports ISO weeks.
But the to_char() function has an option for this:
SELECT to_char(DATE '2006-01-01', 'WW')::int` --> 1
SELECT to_char(DATE '2006-12-31', 'WW')::int` --> 53
For date 2006-01-01 end week is start in 2005 year, that same problem is 1999 year.
Clausule EXTRACT(WEEK getting year where week is started not ending.
You can use this code:
SELECT floor(EXTRACT(doy FROM TIMESTAMP '2006-01-01')/7 + 1);
SELECT floor(EXTRACT(doy FROM TIMESTAMP '2006-12-31')/7 + 1);

Convert date into individual numerical columns for year month and day SQL

I have a date column in the format YY-MON-DD, e.g. 25-JUN-05. Is it possible to isolate this into 3 separate columns for year, month and day? Where month is converted from text to numerical, e.g. Year: 25, Month: 06, Day: 05?
MS SQL SERVER
As Nebi suggested, you can use DATEPART and extract each part and store it into different columns.
SELECT DATEPART(DAY,'2008-10-22'); -- Returns DAY part i.e 22
SELECT DATEPART(MONTH,'2008-10-22'); -- Returns MONTH part i.e 10
SELECT DATEPART(YEAR,'2008-10-22'); -- Returns YEAR part i.e 2008
Try with the below script,if you are using SQL Server.
SELECT 'Year: '+CAST(LEFT(YourdateColumn,2) as VARCHAR(2))+', Month: ' +CAST(MONTH('01-'+SUBSTRING(YourdateColumn,4,3)+'-15')as VARCHAR(2))+', Day:'+CAST(RIGHT(YourdateColumn,2)as VARCHAR(2))
FROM Yourtable
sample output :
You didn't specify your DBMS.
The following is standard SQL assuming that column really is a DATE column
select extract(year from the_column) as the_year,
extract(month from the_column) as the_month,
extract(day from the_column) as the_day
from the_table;

Extracting Day of Week as an Integer with Netezza SQL

This should be doable, but how can I extract the day of the week from a field containing data in date format with Netezza SQL? I can write the following query:
SELECT date_part('day',a.report_dt) as report_dt
FROM table as a
but that gives me the day of the month.
thanks for any help
The below queries give day numbers for any week,month,year for a particular date.
--Day of Week
SELECT EXTRACT(dow FROM report_dt) FROM table;
--Day of Month
SELECT DATE_PART('day', report_dt) FROM table;
--Day of Year
SELECT EXTRACT(doy FROM report_dt) FROM table;
Netezza is just ANSI SQL, originally derived from PostgreSQL. I'd expect this to work.
select extract(dow from a.report_dt) as report_dt
from table as a
Returns values should range from 0 to 6; 0 is Sunday. You might expect that to be an integer, but in PostgreSQL at least, the returned value is a double-precision floating point.
If you want to extract directly the day name :
Select to_char(date, 'Day') as Day_Name From table;
In Netezza SQL, SELECT EXTRACT(dow FROM report_dt) would return values 1 to 7. 1 is Sunday, 7 is Saturday.

SSRS BIDS 2008 Expression

What is the correct expression to use for todays date plus 1 year.
I assume it starts with Now()+ but im unsure from there
This page has lots of great examples, including:
=DateAdd(DateInterval.Month, 6, Parameters!StartDate.Value)
From that and the example before it, it looks like you want:
=DateAdd(DateInterval.Year, 1, Today())
this should be what your looking for:
--midnight last day of last month
select DateAdd(mm,-0,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
--midnight last day of this month
select DateAdd(mm,+1,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
--midnight last day of last month 1 year ago
select DateAdd(yy,-1,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
--midnight last day of this month 1 year ago
select DateAdd(yy,-1,DateAdd(mm,+1,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))))

How to calculate Last Week of Month by WeekNO and Year in SQL

I want to calculate Last Week Number of Month in SQL. I am having Week Number and Year.
Eg. If I pass WeekNo=51 , Year=2008 , than function should return LastWeekofMonth= 52.
I want to calculate Week number using below standards.
According to ISO 8601:1988 that is used in Sweden the first week of the year is the first week that has at least four days within the new year.
So if your week starts on a Monday the first Thursday any year is within the first week. You can DateAdd or DateDiff from that.
Please Help me..........
Thanks in advance.
SELECT WEEK(LAST_DAY(STR_TO_DATE('2008-51-Mon', '%x-%v-%a')));
Should do the trick for getting the last week number of month with MySQL :
I first convert to a date, then I get the last day of the month (here: 2008-12-31), then I compute the week of the last day of the month (52).
It should be easy to turn it into a function.
Hope this helps.
This is fairly straightforward if you use a calendar table. The month you need is given by this query.
select iso_year, month_of_year
from calendar c
where iso_year = 2008 and iso_week = 51
group by iso_year, month_of_year
--
iso_year month_of_year
2008 12
So you can use that result in a join on the calendar table, like this.
select max(c.iso_week) as last_week_of_month
from calendar c
inner join
(select iso_year, month_of_year
from calendar c
where iso_year = 2008 and iso_week = 51
group by iso_year, month_of_year) m
on m.iso_year = c.iso_year and m.month_of_year = c.month_of_year;
--
last_week_of_month
52
Here's one example of a calendar table, but it's pretty thin on CHECK constraints.
If you're using SQL Server, you can perform a calculation by using a master table, without creating a calendar table. This fellow gives you a very good explanation, which I recommend that you read. His SQL for calculating the first and last Sundays of each month can be adapted for your use:
declare #year int
set #year =2011
select min(dates) as first_sunday,max(dates) as last_sunday from
(
select dateadd(day,number-1,DATEADD(year,#year-1900,0))
as dates from master..spt_values
where type='p' and number between 1 and
DATEDIFF(day,DATEADD(year,#year-1900,0),DATEADD(year,#year-1900+1,0))
) as t
where DATENAME(weekday,dates)='sunday'
group by DATEADD(month,datediff(month,0,dates),0)
Edit: Once you have the date of the Thursday, you can get the week number from that date like this:
DECLARE #Dt datetime
SELECT #Dt='02-21-2008'
SELECT DATEPART( wk, #Dt)