December 18, 2018 at 1:39 am
I have year(YYYY) and Month(M01) as data.
How can I have a logic where when user select year and month, it will deduct T-1month
I tried to query it but the year does not change.
Example if Year=2018 and Month=M01 it still appear as Year 2018 and Month M12
The expected result is Year 2017 and Month M12
December 18, 2018 at 3:56 am
I am sure there is a more elegant way of doing this, and without seeing your code this is just a bit of an assumption.
However it would seem your year is dependent on the month, therefore you could use a case test on the month value and calculate the year accordingly. However I don't know how far you data goes back.
based purely on what you have said you could try the following.
CASE WHEN Month = 'M01' THEN Year = 2017 ELSE 2018 END
December 21, 2018 at 6:24 pm
1. Hopefully this teaches why you should never store formatted dates in a table. Leave such formatting to when you need to present the data.
2. Hopefully this also teaches you the fallacy of storing parts of dates in more than 1 column instead of a single date column, which can be easily manipulated to display just about any format.
Since the table you're working with has neither of those advantage, the following code will work for any legal year and month representation you have in your two columns.
DECLARE @YearCol CHAR(4) = '2018'
,@MonthCol CHAR(3) = 'M01'
;
SELECT YYYY = DATENAME(yy,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))
,Mxx = 'M'+RIGHT(DATEPART(mm,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))+100,2)
--FROM dbo.SomeTable
;
--Jeff Moden
Change is inevitable... Change for the better is not.
January 10, 2019 at 1:05 am
Jeff Moden - Friday, December 21, 2018 6:24 PM1. Hopefully this teaches why you should never store formatted dates in a table. Leave such formatting to when you need to present the data.
2. Hopefully this also teaches you the fallacy of storing parts of dates in more than 1 column instead of a single date column, which can be easily manipulated to display just about any format.Since the table you're working with has neither of those advantage, the following code will work for any legal year and month representation you have in your two columns.
DECLARE @YearCol CHAR(4) = '2018'
,@MonthCol CHAR(3) = 'M01'
;
SELECT YYYY = DATENAME(yy,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))
,Mxx = 'M'+RIGHT(DATEPART(mm,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))+100,2)
--FROM dbo.SomeTable
;
Is it possible if I would like to consolidate more than one @MonthCol.
@MonthCol will input more than 1 month example @MonthCol=M12,M11
January 10, 2019 at 8:57 am
girl_bj - Thursday, January 10, 2019 1:05 AMJeff Moden - Friday, December 21, 2018 6:24 PM1. Hopefully this teaches why you should never store formatted dates in a table. Leave such formatting to when you need to present the data.
2. Hopefully this also teaches you the fallacy of storing parts of dates in more than 1 column instead of a single date column, which can be easily manipulated to display just about any format.Since the table you're working with has neither of those advantage, the following code will work for any legal year and month representation you have in your two columns.
DECLARE @YearCol CHAR(4) = '2018'
,@MonthCol CHAR(3) = 'M01'
;
SELECT YYYY = DATENAME(yy,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))
,Mxx = 'M'+RIGHT(DATEPART(mm,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))+100,2)
--FROM dbo.SomeTable
;Is it possible if I would like to consolidate more than one @MonthCol.
@MonthCol will input more than 1 month example @MonthCol=M12,M11
What do you expect the output from that to be? 1 row or 2?
--Jeff Moden
Change is inevitable... Change for the better is not.
January 10, 2019 at 9:01 am
Jeff Moden - Thursday, January 10, 2019 8:57 AMgirl_bj - Thursday, January 10, 2019 1:05 AMJeff Moden - Friday, December 21, 2018 6:24 PM1. Hopefully this teaches why you should never store formatted dates in a table. Leave such formatting to when you need to present the data.
2. Hopefully this also teaches you the fallacy of storing parts of dates in more than 1 column instead of a single date column, which can be easily manipulated to display just about any format.Since the table you're working with has neither of those advantage, the following code will work for any legal year and month representation you have in your two columns.
DECLARE @YearCol CHAR(4) = '2018'
,@MonthCol CHAR(3) = 'M01'
;
SELECT YYYY = DATENAME(yy,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))
,Mxx = 'M'+RIGHT(DATEPART(mm,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))+100,2)
--FROM dbo.SomeTable
;Is it possible if I would like to consolidate more than one @MonthCol.
@MonthCol will input more than 1 month example @MonthCol=M12,M11
What do you expect the output from that to be? 1 row or 2?
2 rows.
January 10, 2019 at 9:41 am
girl_bj - Thursday, January 10, 2019 9:01 AMJeff Moden - Thursday, January 10, 2019 8:57 AMgirl_bj - Thursday, January 10, 2019 1:05 AMJeff Moden - Friday, December 21, 2018 6:24 PM1. Hopefully this teaches why you should never store formatted dates in a table. Leave such formatting to when you need to present the data.
2. Hopefully this also teaches you the fallacy of storing parts of dates in more than 1 column instead of a single date column, which can be easily manipulated to display just about any format.Since the table you're working with has neither of those advantage, the following code will work for any legal year and month representation you have in your two columns.
DECLARE @YearCol CHAR(4) = '2018'
,@MonthCol CHAR(3) = 'M01'
;
SELECT YYYY = DATENAME(yy,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))
,Mxx = 'M'+RIGHT(DATEPART(mm,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))+100,2)
--FROM dbo.SomeTable
;Is it possible if I would like to consolidate more than one @MonthCol.
@MonthCol will input more than 1 month example @MonthCol=M12,M11
What do you expect the output from that to be? 1 row or 2?
2 rows.
Use a string splitter (like DelimitedSplit8K) to split the single row into 2 and then use the method I previously demonstrated on the set of two rows.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 10, 2019 at 6:46 pm
Jeff Moden - Thursday, January 10, 2019 9:41 AMgirl_bj - Thursday, January 10, 2019 9:01 AMJeff Moden - Thursday, January 10, 2019 8:57 AMgirl_bj - Thursday, January 10, 2019 1:05 AMJeff Moden - Friday, December 21, 2018 6:24 PM1. Hopefully this teaches why you should never store formatted dates in a table. Leave such formatting to when you need to present the data.
2. Hopefully this also teaches you the fallacy of storing parts of dates in more than 1 column instead of a single date column, which can be easily manipulated to display just about any format.Since the table you're working with has neither of those advantage, the following code will work for any legal year and month representation you have in your two columns.
DECLARE @YearCol CHAR(4) = '2018'
,@MonthCol CHAR(3) = 'M01'
;
SELECT YYYY = DATENAME(yy,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))
,Mxx = 'M'+RIGHT(DATEPART(mm,DATEADD(mm,-1,DATEFROMPARTS(@YearCol,RIGHT(@MonthCol,2),1)))+100,2)
--FROM dbo.SomeTable
;Is it possible if I would like to consolidate more than one @MonthCol.
@MonthCol will input more than 1 month example @MonthCol=M12,M11
What do you expect the output from that to be? 1 row or 2?
2 rows.
Use a string splitter (like DelimitedSplit8K) to split the single row into 2 and then use the method I previously demonstrated on the set of two rows.
Hi, DelimitedSplit8K not supported in sql server? Invalid object name.
January 10, 2019 at 7:05 pm
Did you run the code to create the function? Sounds like you didn't.
January 10, 2019 at 7:14 pm
pietlinden - Thursday, January 10, 2019 7:05 PMDid you run the code to create the function? Sounds like you didn't.
Any sample that I can refer?
Thanks
January 10, 2019 at 9:50 pm
girl_bj - Thursday, January 10, 2019 7:14 PMpietlinden - Thursday, January 10, 2019 7:05 PMDid you run the code to create the function? Sounds like you didn't.Any sample that I can refer?
Thanks
Apologies. I forgot to post the link.
http://www.sqlservercentral.com/articles/Tally+Table/72993/
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply