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
 datediff issue
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masterdineen
Aged Yak Warrior

United Kingdom
517 Posts

Posted - 07/23/2012 :  08:46:30  Show Profile  Reply with Quote
Hello there.

I want to use the datediff function but i also require a variable which will be the month.

so i have created the following stored proc.



alter proc RenewalDateTest

@month varchar(2)
as
declare @date datetime

declare @current datetime

select @date = (select convert(varchar(4),datepart(year,getdate()))+'-'+@month+'-'+convert(varchar(2),datepart(day,getdate()))+' '+'00:00:00.000')

select @current = (select getdate())

select datediff(day,@date,@current)

go

exec RenewalDateTest 3

result = 1900-05-03 00:00:00.000

my question is why am i not getting a datediff in days.





madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 07/23/2012 :  09:33:22  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Can you double check? It should return only number

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

masterdineen
Aged Yak Warrior

United Kingdom
517 Posts

Posted - 07/23/2012 :  09:38:25  Show Profile  Reply with Quote
within the stored proc

i was selecting

select @date

union all

select datediff(day,@date,@current)

so i removed the select @date and now it works.

strange. shouldnt it still return the day difference
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 07/23/2012 :  09:42:57  Show Profile  Visit webfred's Homepage  Reply with Quote
UNION returns ONE record set and in this record set there can be only ONE datatype for a column hence an implicit conversion has happened.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 07/23/2012 :  10:02:12  Show Profile  Reply with Quote
quote:
Originally posted by masterdineen

within the stored proc

i was selecting

select @date

union all

select datediff(day,@date,@current)

so i removed the select @date and now it works.

strange. shouldnt it still return the day difference


that explains it

since first value was of datetime type it converted second integer value returned by datediff also to datetime time. so in effect what you get will be integer amount of days added to base date which is 1900-01-01 00:00:00.
see below to understand how date manipulation works with integer values

http://visakhm.blogspot.com/2012/07/generate-datetime-values-from-integers.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

masterdineen
Aged Yak Warrior

United Kingdom
517 Posts

Posted - 07/23/2012 :  10:04:57  Show Profile  Reply with Quote
thank you very much for your help.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 07/24/2012 :  04:46:00  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by masterdineen

within the stored proc

i was selecting

select @date

union all

select datediff(day,@date,@current)

so i removed the select @date and now it works.

strange. shouldnt it still return the day difference


You should have posted this in your original question itself

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 07/24/2012 :  09:33:10  Show Profile  Reply with Quote
quote:
Originally posted by masterdineen

thank you very much for your help.


welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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