Solved

Convert DateTime and Date Variables to Integers with Microsoft SQL Server

Posted on 2011-09-27
8
222 Views
Last Modified: 2012-08-14
Using Microsoft Server Management Studio I am trying to work out the difference in days between 2 dates. The two dates are:
[BetDate] - datetime
[FirstPlacedDate] - date

I'm a bit of a SQL newbie. I've tried converting them to integers using Cast, Convert and DateDiff functions but haven't been able to get any of them to work
0
Comment
Question by:xyobi
  • 4
  • 4
8 Comments
 
LVL 22

Expert Comment

by:Thomasian
ID: 36714261
Have you tried:
SELECT DATEDIFF(day,[FirstPlacedDate],[BetDate])

Open in new window


If that didn't work for you, can you post the sql query you tried and the error message you got?
0
 

Author Comment

by:xyobi
ID: 36714287
it tells me
Conversion failed when converting date and/or time from character string.

i do have it inside a ceiling function which may be impacting it???

Ceiling(DateDiff(day,[FirstPlacedDate],[BetDate]) / 25)

Open in new window

0
 
LVL 22

Expert Comment

by:Thomasian
ID: 36714299
No, that should not cause the error.

Are you sure that the datatype for both fields are date/datetime? The error should be caused by trying to change an invalid char/varchar data to date/datetime data type.


Btw, you should change "/ 25" to "/ 25.0" otherwise it will always return an integer (rounded down).
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:xyobi
ID: 36714319
They are definitely dates.

Another thing I just thought of, would it have an impact if some of the fields were null?
0
 
LVL 22

Expert Comment

by:Thomasian
ID: 36714337
>>Another thing I just thought of, would it have an impact if some of the fields were null?
It will return null if any of the fields is null.

Are you sure that that statement is the one that causes the error? Can you post the whole query you tried?
0
 

Author Comment

by:xyobi
ID: 36714346
SELECT [Datawarehouse].[dbo].[fct_AccountSummary].FirstPlacedDate
	, [Datawarehouse].[dbo].[fct_AccountSummary].FirstPlacedSport
	, Ceiling(DateDiff(day,[Datawarehouse].[dbo].[fct_AccountSummary].FirstPlacedDate,[Datawarehouse].[dbo].[fct_Bets].BetDate) / 25.0)
	, [Datawarehouse].[dbo].[fct_Bets].Sport
	, COUNT([Datawarehouse].[dbo].[fct_Bets].DWAccountID) as "Number of Bets"
FROM [Datawarehouse].[dbo].[fct_AccountSummary] (nolock)
Left Join [Datawarehouse].[dbo].[fct_Bets] (nolock)
On [Datawarehouse].[dbo].[fct_AccountSummary].DWAccountID = [Datawarehouse].[dbo].[fct_Bets].DWAccountID
Where ([Datawarehouse].[dbo].[fct_AccountSummary].FirstPlacedDate between '24-09-2008' and '25-09-2010')
	and ([Datawarehouse].[dbo].[fct_Bets].BetDate between '24-09-2008' and '25-09-2011')
	and [Datawarehouse].[dbo].[fct_AccountSummary].FirstPlacedDate is not null
	and [Datawarehouse].[dbo].[fct_Bets].BetDate is not null
Group By [Datawarehouse].[dbo].[fct_AccountSummary].FirstPlacedDate
	, [Datawarehouse].[dbo].[fct_AccountSummary].FirstPlacedSport
	, Ceiling(DateDiff(day,[Datawarehouse].[dbo].[fct_AccountSummary].FirstPlacedDate,[Datawarehouse].[dbo].[fct_Bets].BetDate) / 25.0)
	, [Datawarehouse].[dbo].[fct_Bets].Sport

Open in new window

0
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
ID: 36714355
The problem is you are entering the dates in 'dd-mm-yyyy' format while sql server interprets them as 'mm-dd-yyyy'. To avoid these errors, always enter dates in 'yyyy-mm-dd'

e.g.

'24-09-2008' and '25-09-2011'

should be

'2008-09-24' and '2011-09-25'
0
 

Author Comment

by:xyobi
ID: 36714359
hey... you are a legend. apologies for having to deal with the rookie mistake
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question