November 5, 2018 at 5:05 pm
I have start date column. how can i get rest of 4 columns of attached file. financial year begins from feb 2018. so feb,mar,april-quarter 1 2018 but belongs to fiscal year 2019.
SELECT
startdate,
CASE
WHEN MONTH(startdate) BETWEEN 2 AND 4 THEN convert(char(4), YEAR(startdate) - 1) + 'Q1'
WHEN MONTH(startdate) BETWEEN 5 AND 7 THEN convert(char(4), YEAR(startdate) - 1) + 'Q2'
WHEN MONTH(startdate) BETWEEN 8 AND 10 THEN convert(char(4), YEAR(startdate) - 0) + 'Q3'
WHEN MONTH(startdate) BETWEEN 11 AND 1 THEN convert(char(4), YEAR(startdate) - 0) + 'Q4'
END AS Quarter
FROM
mytable
November 5, 2018 at 6:40 pm
Papil - Monday, November 5, 2018 5:05 PMI have start date column. how can i get rest of 4 columns of attached file. financial year begins from feb 2018. so feb,mar,april-quarter 1 2018 but belongs to fiscal year 2019.
SELECT
startdate,
CASE
WHEN MONTH(startdate) BETWEEN 2 AND 4 THEN convert(char(4), YEAR(startdate) - 1) + 'Q1'
WHEN MONTH(startdate) BETWEEN 5 AND 7 THEN convert(char(4), YEAR(startdate) - 1) + 'Q2'
WHEN MONTH(startdate) BETWEEN 8 AND 10 THEN convert(char(4), YEAR(startdate) - 0) + 'Q3'
WHEN MONTH(startdate) BETWEEN 11 AND 1 THEN convert(char(4), YEAR(startdate) - 0) + 'Q4'
END AS Quarter
FROM
mytable
Hope this gets you started:
declare @ThisDate datetime = '20180201';
with TwelveMonths as (select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11))dt(n))
select
dateadd(month,n,@ThisDate)
, dateadd(month,11,dateadd(month,n,@ThisDate))
, datepart(year,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalYear]
, datepart(quarter,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalQuarter]
, datepart(month,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalMonth]
from
[TwelveMonths]
November 5, 2018 at 7:06 pm
Lynn Pettis - Monday, November 5, 2018 6:40 PMPapil - Monday, November 5, 2018 5:05 PMI have start date column. how can i get rest of 4 columns of attached file. financial year begins from feb 2018. so feb,mar,april-quarter 1 2018 but belongs to fiscal year 2019.
SELECT
startdate,
CASE
WHEN MONTH(startdate) BETWEEN 2 AND 4 THEN convert(char(4), YEAR(startdate) - 1) + 'Q1'
WHEN MONTH(startdate) BETWEEN 5 AND 7 THEN convert(char(4), YEAR(startdate) - 1) + 'Q2'
WHEN MONTH(startdate) BETWEEN 8 AND 10 THEN convert(char(4), YEAR(startdate) - 0) + 'Q3'
WHEN MONTH(startdate) BETWEEN 11 AND 1 THEN convert(char(4), YEAR(startdate) - 0) + 'Q4'
END AS Quarter
FROM
mytableHope this gets you started:
declare @ThisDate datetime = '20180201';with TwelveMonths as (select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11))dt(n))
select
dateadd(month,n,@ThisDate)
, dateadd(month,11,dateadd(month,n,@ThisDate))
, datepart(year,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalYear]
, datepart(quarter,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalQuarter]
, datepart(month,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalMonth]
from
[TwelveMonths]
is it possible to get without variable. i have a column in my table and i want to add rest of these columns to get all data in one temp table?
November 5, 2018 at 7:10 pm
Papil - Monday, November 5, 2018 7:06 PMLynn Pettis - Monday, November 5, 2018 6:40 PMPapil - Monday, November 5, 2018 5:05 PMI have start date column. how can i get rest of 4 columns of attached file. financial year begins from feb 2018. so feb,mar,april-quarter 1 2018 but belongs to fiscal year 2019.
SELECT
startdate,
CASE
WHEN MONTH(startdate) BETWEEN 2 AND 4 THEN convert(char(4), YEAR(startdate) - 1) + 'Q1'
WHEN MONTH(startdate) BETWEEN 5 AND 7 THEN convert(char(4), YEAR(startdate) - 1) + 'Q2'
WHEN MONTH(startdate) BETWEEN 8 AND 10 THEN convert(char(4), YEAR(startdate) - 0) + 'Q3'
WHEN MONTH(startdate) BETWEEN 11 AND 1 THEN convert(char(4), YEAR(startdate) - 0) + 'Q4'
END AS Quarter
FROM
mytableHope this gets you started:
declare @ThisDate datetime = '20180201';with TwelveMonths as (select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11))dt(n))
select
dateadd(month,n,@ThisDate)
, dateadd(month,11,dateadd(month,n,@ThisDate))
, datepart(year,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalYear]
, datepart(quarter,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalQuarter]
, datepart(month,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalMonth]
from
[TwelveMonths]is it possible to get without variable. i have a column in my table and i want to add rest of these columns to get all data in one temp table?
The code I posted is just a sample of how to do it.
November 5, 2018 at 7:39 pm
Lynn Pettis - Monday, November 5, 2018 7:10 PMPapil - Monday, November 5, 2018 7:06 PMLynn Pettis - Monday, November 5, 2018 6:40 PMPapil - Monday, November 5, 2018 5:05 PMI have start date column. how can i get rest of 4 columns of attached file. financial year begins from feb 2018. so feb,mar,april-quarter 1 2018 but belongs to fiscal year 2019.
SELECT
startdate,
CASE
WHEN MONTH(startdate) BETWEEN 2 AND 4 THEN convert(char(4), YEAR(startdate) - 1) + 'Q1'
WHEN MONTH(startdate) BETWEEN 5 AND 7 THEN convert(char(4), YEAR(startdate) - 1) + 'Q2'
WHEN MONTH(startdate) BETWEEN 8 AND 10 THEN convert(char(4), YEAR(startdate) - 0) + 'Q3'
WHEN MONTH(startdate) BETWEEN 11 AND 1 THEN convert(char(4), YEAR(startdate) - 0) + 'Q4'
END AS Quarter
FROM
mytableHope this gets you started:
declare @ThisDate datetime = '20180201';with TwelveMonths as (select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11))dt(n))
select
dateadd(month,n,@ThisDate)
, dateadd(month,11,dateadd(month,n,@ThisDate))
, datepart(year,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalYear]
, datepart(quarter,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalQuarter]
, datepart(month,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalMonth]
from
[TwelveMonths]is it possible to get without variable. i have a column in my table and i want to add rest of these columns to get all data in one temp table?
The code I posted is just a sample of how to do it.
Even if i put select statement.it only displays those rows that were showing up before.
declare @ThisDate datetime
SELECT @thisdate = ET.Thisdate
FROM mytable ET
;
--declare @ThisDate datetime = '20180201';
with TwelveMonths as (select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11))dt(n))
select
dateadd(month,n,@ThisDate)
, dateadd(month,11,dateadd(month,n,@ThisDate))
, datepart(year,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalYear]
, datepart(quarter,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalQuarter]
, datepart(month,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalMonth]
from
[TwelveMonths]
November 5, 2018 at 8:00 pm
Papil - Monday, November 5, 2018 5:05 PMI have start date column. how can i get rest of 4 columns of attached file. financial year begins from feb 2018. so feb,mar,april-quarter 1 2018 but belongs to fiscal year 2019.
SELECT
startdate,
CASE
WHEN MONTH(startdate) BETWEEN 2 AND 4 THEN convert(char(4), YEAR(startdate) - 1) + 'Q1'
WHEN MONTH(startdate) BETWEEN 5 AND 7 THEN convert(char(4), YEAR(startdate) - 1) + 'Q2'
WHEN MONTH(startdate) BETWEEN 8 AND 10 THEN convert(char(4), YEAR(startdate) - 0) + 'Q3'
WHEN MONTH(startdate) BETWEEN 11 AND 1 THEN convert(char(4), YEAR(startdate) - 0) + 'Q4'
END AS Quarter
FROM
mytable
Papil - Monday, November 5, 2018 7:39 PMLynn Pettis - Monday, November 5, 2018 7:10 PMPapil - Monday, November 5, 2018 7:06 PMLynn Pettis - Monday, November 5, 2018 6:40 PMPapil - Monday, November 5, 2018 5:05 PMI have start date column. how can i get rest of 4 columns of attached file. financial year begins from feb 2018. so feb,mar,april-quarter 1 2018 but belongs to fiscal year 2019.
SELECT
startdate,
CASE
WHEN MONTH(startdate) BETWEEN 2 AND 4 THEN convert(char(4), YEAR(startdate) - 1) + 'Q1'
WHEN MONTH(startdate) BETWEEN 5 AND 7 THEN convert(char(4), YEAR(startdate) - 1) + 'Q2'
WHEN MONTH(startdate) BETWEEN 8 AND 10 THEN convert(char(4), YEAR(startdate) - 0) + 'Q3'
WHEN MONTH(startdate) BETWEEN 11 AND 1 THEN convert(char(4), YEAR(startdate) - 0) + 'Q4'
END AS Quarter
FROM
mytableHope this gets you started:
declare @ThisDate datetime = '20180201';with TwelveMonths as (select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11))dt(n))
select
dateadd(month,n,@ThisDate)
, dateadd(month,11,dateadd(month,n,@ThisDate))
, datepart(year,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalYear]
, datepart(quarter,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalQuarter]
, datepart(month,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalMonth]
from
[TwelveMonths]is it possible to get without variable. i have a column in my table and i want to add rest of these columns to get all data in one temp table?
The code I posted is just a sample of how to do it.
Even if i put select statement.it only displays those rows that were showing up before.
declare @ThisDate datetime
SELECT @thisdate = ET.Thisdate
FROM mytable ET
;--declare @ThisDate datetime = '20180201';
with TwelveMonths as (select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11))dt(n))
select
dateadd(month,n,@ThisDate)
, dateadd(month,11,dateadd(month,n,@ThisDate))
, datepart(year,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalYear]
, datepart(quarter,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalQuarter]
, datepart(month,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalMonth]
from
[TwelveMonths]
Yes... please post some sample data from your table in a readily consumable format so that one of us can demonstrate. Please see the first link in my signature line below for one way how to do that. You'll also find that most people won't even look at attached spreadsheets.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 5, 2018 at 9:54 pm
Jeff Moden - Monday, November 5, 2018 8:00 PMPapil - Monday, November 5, 2018 5:05 PMI have start date column. how can i get rest of 4 columns of attached file. financial year begins from feb 2018. so feb,mar,april-quarter 1 2018 but belongs to fiscal year 2019.
SELECT
startdate,
CASE
WHEN MONTH(startdate) BETWEEN 2 AND 4 THEN convert(char(4), YEAR(startdate) - 1) + 'Q1'
WHEN MONTH(startdate) BETWEEN 5 AND 7 THEN convert(char(4), YEAR(startdate) - 1) + 'Q2'
WHEN MONTH(startdate) BETWEEN 8 AND 10 THEN convert(char(4), YEAR(startdate) - 0) + 'Q3'
WHEN MONTH(startdate) BETWEEN 11 AND 1 THEN convert(char(4), YEAR(startdate) - 0) + 'Q4'
END AS Quarter
FROM
mytablePapil - Monday, November 5, 2018 7:39 PMLynn Pettis - Monday, November 5, 2018 7:10 PMPapil - Monday, November 5, 2018 7:06 PMLynn Pettis - Monday, November 5, 2018 6:40 PMPapil - Monday, November 5, 2018 5:05 PMI have start date column. how can i get rest of 4 columns of attached file. financial year begins from feb 2018. so feb,mar,april-quarter 1 2018 but belongs to fiscal year 2019.
SELECT
startdate,
CASE
WHEN MONTH(startdate) BETWEEN 2 AND 4 THEN convert(char(4), YEAR(startdate) - 1) + 'Q1'
WHEN MONTH(startdate) BETWEEN 5 AND 7 THEN convert(char(4), YEAR(startdate) - 1) + 'Q2'
WHEN MONTH(startdate) BETWEEN 8 AND 10 THEN convert(char(4), YEAR(startdate) - 0) + 'Q3'
WHEN MONTH(startdate) BETWEEN 11 AND 1 THEN convert(char(4), YEAR(startdate) - 0) + 'Q4'
END AS Quarter
FROM
mytableHope this gets you started:
declare @ThisDate datetime = '20180201';with TwelveMonths as (select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11))dt(n))
select
dateadd(month,n,@ThisDate)
, dateadd(month,11,dateadd(month,n,@ThisDate))
, datepart(year,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalYear]
, datepart(quarter,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalQuarter]
, datepart(month,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalMonth]
from
[TwelveMonths]is it possible to get without variable. i have a column in my table and i want to add rest of these columns to get all data in one temp table?
The code I posted is just a sample of how to do it.
Even if i put select statement.it only displays those rows that were showing up before.
declare @ThisDate datetime
SELECT @thisdate = ET.Thisdate
FROM mytable ET
;--declare @ThisDate datetime = '20180201';
with TwelveMonths as (select n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11))dt(n))
select
dateadd(month,n,@ThisDate)
, dateadd(month,11,dateadd(month,n,@ThisDate))
, datepart(year,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalYear]
, datepart(quarter,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalQuarter]
, datepart(month,dateadd(month,11,dateadd(month,n,@ThisDate))) as [FiscalMonth]
from
[TwelveMonths]Yes... please post some sample data from your table in a readily consumable format so that one of us can demonstrate. Please see the first link in my signature line below for one way how to do that. You'll also find that most people won't even look at attached spreadsheets.
CREATE TABLE mytable(
StartDate DATE NOT NULL PRIMARY KEY
,month INTEGER NOT NULL
,year INTEGER NOT NULL
,fy_year INTEGER NOT NULL
,fy_qtr INTEGER NOT NULL
);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-02-01',2,2018,2019,1);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-03-01',3,2018,2019,1);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-04-01',4,2018,2019,1);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-05-01',5,2018,2019,2);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-06-01',6,2018,2019,2);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-07-01',7,2018,2019,2);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-08-01',8,2018,2019,3);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-09-01',9,2018,2019,3);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-10-01',10,2018,2019,3);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-11-01',11,2018,2019,4);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2018-12-01',12,2018,2019,4);
INSERT INTO mytable(StartDate,month,year,fy_year,fy_qtr) VALUES ('2019-01-01',1,2019,2019,4);
November 6, 2018 at 7:01 am
I have start date but want to get rest of columns in my table. I have attached consumable script above.
November 6, 2018 at 9:29 am
Okay, here you go.
create table [dbo].[mytable]
(
[StartDate] date not null primary key
, [CalendarMonth] as datepart(month,[StartDate]) persisted
, [CalendarYear] as datepart(year,[StartDate]) persisted
, [fy_year] as datepart(year,dateadd(month,11,[StartDate])) persisted
, [fy_qtr] as datepart(quarter,dateadd(month,11,[StartDate])) persisted
, [fy_month] as datepart(month,dateadd(month,11,[StartDate])) persisted
);
go
INSERT INTO mytable(StartDate)
values
('2018-02-01'),
('2018-03-01'),
('2018-04-01'),
('2018-05-01'),
('2018-06-01'),
('2018-07-01'),
('2018-08-01'),
('2018-09-01'),
('2018-10-01'),
('2018-11-01'),
('2018-12-01'),
('2019-01-01');
go
select * from [dbo].[mytable];
go
drop table [dbo].[mytable]; -- clean up my SANDBOX DATABASE!!!!!
go
November 6, 2018 at 9:38 am
Using Lynn's code and replacing the code for the sample date with your actual table field.
SELECT *
, datepart(year,dateadd(month,11,StartDate)) as [FiscalYear]
, datepart(quarter,dateadd(month,11,StartDate)) as [FiscalQuarter]
, datepart(month,dateadd(month,11,StartDate)) as [FiscalMonth]
FROM mytable
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
November 6, 2018 at 9:46 am
drew.allen - Tuesday, November 6, 2018 9:38 AMUsing Lynn's code and replacing the code for the sample date with your actual table field.
SELECT *
, datepart(year,dateadd(month,11,StartDate)) as [FiscalYear]
, datepart(quarter,dateadd(month,11,StartDate)) as [FiscalQuarter]
, datepart(month,dateadd(month,11,StartDate)) as [FiscalMonth]FROM mytable
Drew
I just persisted the data in the table when inserting StartDates. Makes it easier.
November 6, 2018 at 9:57 am
Thanks both .it works.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply