SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Number of days between two integer date columns


Number of days between two integer date columns

Author
Message
sunder.mekala
sunder.mekala
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 55
Hi Friends,
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,

--
Thanks,
Sam.
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39317 Visits: 38532
sunder.mekala (3/4/2013)
Hi Friends,
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,

--
Thanks,
Sam.


This: datediff(day,cast(cast(startdate_SK as varchar(8)) as date), cast(cast(enddate_SK as varchar(8)) as date))

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26074 Visits: 17534
sunder.mekala (3/4/2013)
Hi Friends,
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,

--
Thanks,
Sam.


[rant]
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.
[/rant]

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)
as
(
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
from cte
)

select datediff(day, startdate_SK, enddate_SK)
from convertedDates



_______________________________________________________________

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)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26074 Visits: 17534
Looks like Lynn beat me to the punchline. :-D

_______________________________________________________________

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)
sunder.mekala
sunder.mekala
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 55
It worked, Thankyou.
sunder.mekala
sunder.mekala
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 55
Lynn yours worked, that is what i needed, Thanks again
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search