SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Subtracting periods that are in YYYYMM format
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Fezi
Starting Member

United Kingdom
2 Posts

Posted - 03/28/2014 :  06:06:38  Show Profile  Reply with Quote
Hi,

I have a Column in my data that gives a financial period value in the YYYYMM format. i.e. an asset was re-valued in a particular period for example 201301. I need to find out the number(count) of periods(months) between another given period for example current period (201403) and the period provided in the table i.e. 201301.

Is this possible in the SQL Database?

Thanks,
Fezi

Fezi

MuralikrishnaVeera
Posting Yak Master

India
106 Posts

Posted - 03/28/2014 :  06:36:41  Show Profile  Reply with Quote
DECLARE @TEMP TABLE ([StartYear] DATE,[EndYear] DATE)
INSERT INTO @TEMP VALUES('20130327','20140227')
SELECT
(CAST(DATEDIFF(MONTH,[StartYear],[EndYear]) AS VARCHAR)+' '+'Months') AS Count FROM @TEMP

---------------
Murali Krishna

You live only once ..If you do it right once is enough.......

Edited by - MuralikrishnaVeera on 03/28/2014 06:46:09
Go to Top of Page

Fezi
Starting Member

United Kingdom
2 Posts

Posted - 03/28/2014 :  07:39:49  Show Profile  Reply with Quote
Hi Murali,

That would work except that my period values are YYYYMM so I will need the query to look something like the following but then it wouldn't work because that is not the correct date format.


DECLARE @TEMP TABLE ([StartYear] DATE,[EndYear] DATE)
INSERT INTO @TEMP VALUES('201203','201402')
SELECT
(CAST(DATEDIFF(MONTH,[StartYear],[EndYear]) AS VARCHAR)) AS Count FROM @TEMP

Any ideas?

Thanks,

Fezi
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000