|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 10:25 AM
Points: 18,754,
Visits: 12,337
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, April 08, 2010 5:03 PM
Points: 2,
Visits: 1
|
|
| And what about the old CONVERT(VARCHAR(12),GETDATE(),101), will this work in some cases?
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, April 08, 2010 5:03 PM
Points: 2,
Visits: 1
|
|
| I concur... thanks Seth, great article and I have added these tips and documentation to my notes.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, November 19, 2012 5:59 AM
Points: 20,
Visits: 50
|
|
Hi, Great to see all the ways to manipulate dates in SQL.
Here is another way of doing it rather quickly.
SELECT CAST(CAST(GETDATE() AS CHAR(11)) AS DATETIME)
Cool stuff, Hanri
Whenever I get sad, I stop being sad and be awesome instead… true story!
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, November 19, 2012 5:59 AM
Points: 20,
Visits: 50
|
|
hi, You can change the first day of the week by using the @@DATEFIRST keyword.
http://msdn.microsoft.com/en-us/library/ms181598(SQL.90).aspx
Regards, Hanri
Whenever I get sad, I stop being sad and be awesome instead… true story!
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:36 PM
Points: 32,931,
Visits: 26,820
|
|
SQLJeff (4/8/2010) And what about the old CONVERT(VARCHAR(12),GETDATE(),101), will this work in some cases?
Most likely... but the problem with that is as I previously stated... it uses twice as much CPU time and takes twice as long duration wise. If you're only working with a handful of rows, you certainly won't notice the difference. BUT, if you're working with many millions of rows like I usually have to, combined with other CPU saving methods, it makes all the difference in the world. Every microsecond counts for the stuff I usually have to do.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 1:12 AM
Points: 207,
Visits: 531
|
|
Great article, Seth! Seeing as how I've tried method after method for accomplishing this and I've never been pleased with what I've done, I think this is clever, elegant, and darned handy. Thanks for sharing!
Regards,
Mike M
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Wednesday, November 07, 2012 4:08 PM
Points: 1,525,
Visits: 4,047
|
|
Hanri Naude (4/8/2010) hi, You can change the first day of the week by using the @@DATEFIRST keyword.
http://msdn.microsoft.com/en-us/library/ms181598(SQL.90).aspx
Regards, Hanri
You can change the day of the week, but as I mentioned in an earlier comment, DATEDIFF is not affected by DATEFIRST settings. I'll add a section about that into the article when I get a moment and can submit that and a few other changes.
Also, as Jeff mentioned, converting to a char/varchar and then back to datetime is considerably slower... but we're talking about fractions of a second per row, so if you're only doing a couple it's not a big deal. For instance, doing anything to GETDATE() to store in a variable... not gonna make much of a difference. But If you need to do it to the column of a table with a million rows, you'll definitely see the difference.
Seth Phelabaum Consistency is only a virtue if you're not a screwup. 
Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 12:42 PM
Points: 134,
Visits: 135
|
|
| Is this because SQL Server stores dates as a numeric value? Then CONVERT to VARCHAR() would cause an implicit conversion?
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Monday, October 31, 2011 1:10 PM
Points: 480,
Visits: 1,163
|
|
Thank you for the article. This function has always thrown me off on SQL server.
I dreaded dates before this! Thanks again.
--
|
|
|
|