Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

From int to date and back to int - can I make this more efficient?

$
0
0

Hi,

We hold various date codes in a table in int format. I need to be able to add an amount of months and then return the output as anint. We hold it in YYYYMM format. I've created the below, which works; but I was wondering if there was a better way of achieving the same goal.

At the moment:
- Cast theint as a char and add "01" to the end (20151001)
- Convert to date (type 102) (2015.10.01)
- Dateadd the number of months in the @mnths variable (2016.03.01)
- Convert the date back to Char (20160301)
- Take the Left 6 characters (201603)
- Cast the field back to anint (201603)

Perhaps this is the way to do it but I could make it more readable.

Any help appreciated.

/****** Select Statement Month +/- int ID (with variable) ******/

declare @mnths INT
-- Set number of months here
set @mnths = 5
------------------------------
SELECT
CAST(
LEFT((
...


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>