August 31, 2010 at 10:50 pm
Comments posted to this topic are about the item What will happen?
September 1, 2010 at 12:02 am
:Whistling:
September 1, 2010 at 12:38 am
Good Question. Thanks
Thanks
September 1, 2010 at 12:50 am
thanks for the question
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
September 1, 2010 at 1:37 am
Thanks for the question!:-)
September 1, 2010 at 2:58 am
ziangij (8/31/2010)
DECLARE @a INT
DECLARE @b-2 DATETIME
implicit conversion from int to datetime works fine 🙂
Is this dependent on the sql server version?
Saurabh Dwivedy
___________________________________________________________
My Blog: http://tinyurl.com/dwivedys
For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537
September 1, 2010 at 7:00 am
mbova407 (9/1/2010)
This is how we get the date for the current day without time elementDECLARE @a INT
DECLARE @b-2 DATETIME
set @b-2 = GETDATE()-.5
SET @a = CONVERT(INT,@b)
SELECT CONVERT(DATETIME, @a), @b-2
the -.5 is half a day because CONVERT(INT,@b) rounds
If I run the code above I get:
(No column name) | (No column name)
2010-09-01 00:00:00.000 | 2010-08-31 20:54:59.743
Looks like the time is still there but just 0.
Here's another way to get the current date without the time:
Select CONVERT(varChar(10), getDate(), 101);
This returns: 09/01/2010
September 1, 2010 at 7:07 am
Good question. More than anything, QoTD seems to reinforce.....be explicit.
September 1, 2010 at 7:17 am
tushkieeeee (9/1/2010)
please try the code below. I think this is what the auhtor wanted to convey.
I don't think so. I think the author wanted to convey that implicit conversion from datetime to int is not allowed. In this discussion, the examples somehow switched to implicit conversion from int to datetime, which is fine.
Saurabh Dwivedy (9/1/2010)Is this dependent on the sql server version?
I think the above also answers this - as far as I know, the behaviour is the same in all SQL Server versions (though not when using the new date and time datatypes instead of datetime!): implicit conversion is always allowed from int to datetime, and never from datetime to int.
mbova407 (9/1/2010)
This is how we get the date for the current day without time element(...)
the -.5 is half a day because CONVERT(INT,@b) rounds
I don't really like this method - the addition of an integer value to a datetime, though documented, is wacky, and it doesn't work anymore when switching to the new date or time datatypes; the addition of a non-integer to a datetime is (as far as I know) not even documented.
Dave62 (9/1/2010)
Looks like the time is still there but just 0.Here's another way to get the current date without the time:
Select CONVERT(varChar(10), getDate(), 101);
This returns: 09/01/2010
As long as you keep the datetime datatype, you'll always have a time part. But it can be very useful to ensure the time part is 0, if you need only the day part - it makes comparisons a lot easier. When running SQL2008, you can of course use date instead, but many people are still running SQL2005.
Your alternative is fine if you need the date in character format for presentation purposes. When using it for calculations, you'd have to convert back to datetime. That would at least require you to use a locale-neutrall format (09/01/2010 is the ninth of january in most of the world!), but then you'd still be taking the performance hit of two expensive converstions (datetime to char and char to datetime). Here is my favorite way to force the time part of a datetime to midnight:
SELECT DATEADD(day, DATEDIFF(day, '20100101', CURRENT_TIMESTAMP), '20100101');
For much more information, see http://www.karaszi.com/SQLServer/info_datetime.asp
September 1, 2010 at 7:45 am
SELECT DATEADD(day, DATEDIFF(day, '20100101', CURRENT_TIMESTAMP), '20100101');
The only thing I don't like about this method is the hard-coded string. I suppose a dynamic expression could replace it but it would start to become a little tedious.
I don't mind having 2 converts in there because mbova407's original example had 2. This method below will return the same result and datatype as mbova407's without the need for the integer addition.
Select Convert(Datetime, CONVERT(varChar(10), getDate(), 101));
September 1, 2010 at 8:06 am
September 1, 2010 at 8:21 am
DECLARE @a INT
DECLARE @b-2 DATETIME
The above code executed successfully for me in SQL 2005 but gave the error listed in the answer when I ran it in SQL 2000**. So I think it is dependent on the SQL version, at least in the form expressed above.
- webrunner
** Version: Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
Aug 6 2000 00:57:48 Copyright (c) 1988-2000 Microsoft Corporation
Personal Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
-------------------
A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
Viewing 15 posts - 1 through 15 (of 26 total)
You must be logged in to reply to this topic. Login to reply