I have a requirement where i have to write a Sql for :
basically i have 2 columns 'startdate_SK' and 'enddate_SK' columns in table 'A'
'startdate_SK' is a integer ex:99971231
'enddate_SK' is a integer ex: 17530101
requirement: need to find the number of days between the above two columns
ex: 'startdate_SK' - 20130101 and 'enddate_SK' - 20130201 then my result should be 31
IMP:: both columns are integers in the table.
i can use datediff but they are not date columns instead they are integers.
Please suggest, any help is appreciated,
ACK!!! Why do so many people continue to use incorrect datatypes? We have a datetime datatype, that is what should ALWAYS be used for datetime data.
You are going to have to convert your integers to datetime first.
Something like this should work for you.
;with cte (startdate_SK, enddate_SK)
select 20130101, 20130201
, convertedDates as
select convert(datetime, (stuff(stuff(cast(startdate_SK as char(8)), 5, 0, '/'), 8, 0, '/'))) as startdate_SK,
convert(datetime, (stuff(stuff(cast(enddate_SK as char(8)), 5, 0, '/'), 8, 0, '/'))) as enddate_SK
select datediff(day, startdate_SK, enddate_SK)
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter
.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)