Week end Date in netezza - netezza

I am looking for week start and end date in Netezza but my week starts from Mon and ends on Sun
Date Week Start Date Week End Date
11/30/2015 11/30/2015 Mon 12/06/2015 Sun

Day of Week functions begin on Sunday in Netezza
Template Patterns
Extract Function

You'll need to use modulo in order to deal with Sunday.
select
current_date
,current_date - mod(extract(dow from current_date) - 2),7) week_start
,week_start + 6 week_end

Related

Create a rolled up week column using the day column and populate the value with first monday of the week: Snowflake database

Snowflake Database:
I have a day level table , i am trying use the day column to create a week column with the value of first Monday's of the week as a week value using following function. even though the table have a data for all seven days ( Monday through Sunday ) of the week. The following function will create a week column with five working days ( Monday through Friday) only rolled up to a week leaving Saturday and Sunday. is there any function which i can use to grab all seven days of data under one week in SNOWFLAKE DATABASE (first monday of the week).
CURRENT FUNCTION USED:
select dateadd('day', (extract('dayofweek_iso', current_date()) * -1) +1 , current_date() );
Snowflake provides DATE_TRUNC() to roll up date to corresponding year/month/week/day.
For the mentioned use case following query will roll up to Monday of the week.
select date_trunc('WEEK',current_date()) ;
Snowflake last_day() function can be used to achieve that. You can easily get the last day of week (Sunday) and subtract 6 days to get the first day of that week (Monday) like that:
select last_day(current_date(), 'week')-6 as first_day_of_week;

Getting Month Names From Current Month to April

How can I get The months names from Current month to April only using SQL
Ex:-if the current month is July then I want the month names from July to April.
Current month should be the running month.
Any function or something like that....(I need to get the data from Db for those months)
This should work:
WITH Dates AS
(
SELECT GETDATE() D
UNION ALL
SELECT DATEADD(MONTH,1,D) FROM Dates WHERE DATEPART(MONTH,D)!=4
)
SELECT DATENAME(MONTH,D) [Month Name] FROM Dates
Result
Month Name
------------------------------
September
October
November
December
January
February
March
April

Produce data extracts using day field for the Friday before the weekend

Using TSQL on SQL Server...
I need to produce extracts that use a pay day column in a database that only holds the day number, for example 26 to produce extracts for the 26th day of the month with a twist if that pay day falls on a weekend then the data for that extract should be extracted the Friday before the weekend.
Has anybody attempted this and able to offer some ways of achieving this through TSQL?
Thanks
You have said in today's comment above that you are assuming the current month.
First turn your payday day number into a proper date:
select dateadd(month,((year(getdate())-1900)*12)+month(getdate())-1,payday-1)
as paydate
and then use a case statement against it:
case datename(weekday,paydate)
when 'Saturday' then dateadd(day,-1,paydate)
when 'Sunday' then dateadd(day,-2,paydate)
else paydate
end as paydateadjust
(Obviously these will need rewriting into a proper SQL statement. Date formula borrowed from Michael Valentine Jones' super useful SQL Server date function.)
I'm assuming you have the month and year available too, in which case you can construct a real date, and get the day of week from it as a 1-based number starting at Sunday. So, 6 equals Friday. This will produce 5 for today, as Thursday = 5
SELECT DATEPART(dw, DATEFROMPARTS(2018, 07, 26)) AS [DayOfWeek]
EDIT:
I'll make another assumption, this time that this is somehow possible, like it's always run for the current month. This is a bit dodgy as it all falls apart if it's run too early or too late, but at least it's achievable.
SELECT DATEPART(dw, DATEFROMPARTS(
DATEPART(YEAR, GETDATE()),
DATEPART(MONTH, GETDATE()),
26)) AS [DayOfWeekThisMonth]
EDIT 2:
OK, with the understanding we are working on this month, we can do this:
DECLARE #PayDayNumber INT = 29; -- Replace this with a SELECT to get your value
SELECT CASE(DATEPART(dw, DATEFROMPARTS(DATEPART(YEAR, GETDATE()), DATEPART(MONTH, GETDATE()), #PayDayNumber)))
WHEN 1 THEN #PayDayNumber - 2
WHEN 7 THEN #PayDayNumber - 1
ELSE #PayDayNumber
END AS [WeekendSafePayDay]
For this month (August 2018) the 29th will return 29th. You can change DATEPART(MONTH, GETDATE()) for 9 to test September, which will return 28 because 29th September is a Saturday, or change it to 7 to test July, which will return 27 because 29th July is a Sunday.

Date search range previous month to current month

I had a working SQL date search...
(DATEADD (dd,-30, getdate()) <= pymnt_pstd_dt)
it gives me data from the past 30 days, but what I actually need is a comparison of the previous month MTD with the current month MTD
I am currently struggling with a good way to write that for SQL
Any help would be appreciated.
Here are WHERE statements that will get month to date and previous month to date for your field, pymnt_pstd_dt:
WHERE pymnt_pstd_dt BETWEEN DATEADD(month,DATEDIFF(MONTH,0,GETDATE()),0) AND GETDATE() --Month to date
WHERE pymnt_pstd_dt BETWEEN DATEADD(month,DATEDIFF(MONTH,0,GETDATE())-1,0) AND DATEADD(MONTH,-1,GETDATE()) --Previous month to date

SQL Server 2005: arithmetic with dates

I would like to write a simple SELECT statement in SQL Server 2005 which does the following computation with date arithmetic:
Starting from the present date (this means getdate()), determine the previous Monday, and then subtract 70 days from that Monday, showing in output the resulting date.
How could I achieve this?
My difficulty is mainly to determine the previous Monday.
Of course, if getdate() is Monday, the previous Monday is getdate()
Thank you in advance for your kind help.
UltraCommit
EDIT: Please note that in Italy the first day of the week is Monday and not Sunday, so if the input is Sunday, July 29th, 2012, the output has to be 23rd July, and not 30th July.
This will retrieve monday for the current week
Select DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
and then you need to subtract 70 from the above day
SELECT Dateadd(DAY,-70,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
Edit : Please go through the answer posted in SO
Monday is displayed as Current Week because DATEFIRST which indicates the 1st day of the week is set to monday .In order to set it to Sunday ,you need to change the setting to Sunday
Set DATEFIRST 7
Else as suggested in the above SO link ,you need to change your code
DECLARE #dt DATE = '1905-01-01';
SELECT [start_of_week] = DATEADD(WEEK, DATEDIFF(WEEK, #dt, CURRENT_TIMESTAMP), #dt);
To get last monday look at
http://blog.sqlauthority.com/2007/08/20/sql-server-find-monday-of-the-current-week/
This should get you started. It will find the past Monday for the current week.
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) MondayOfCurrentWeek
To substract 70 days, just add -70 to the end:
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)-70 as SomeMondayInHistory

Resources