Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 18th, 2004, 11:08 AM
Authorized User
 
Join Date: Jul 2003
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting Date part out of Datetime in Stored Pr

Hi.
What is the best way to get a Date part out of GetDate() in my Stored Procedure.

Thank You
Pavel

 
Old February 18th, 2004, 01:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You can use the datepart method to access the individual pieces of the date and concatenate them together. That's the only way I know how; I'm sure someone else will know a better solution.
 
Old February 18th, 2004, 03:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can't really "...get a Date part out of GetDate() ..." (or any other datetime type for that matter) because by definition the datatype contains both a date and a time.

You can however choose to ignore the time portion by setting it to midnight - if all the datetimes you deal with have their time portion set to the same value you are in effect ignoring the time. This can be accomplished by exploiting the fact that SQL Server will by default set the time portion to midnight when converting a string representation of a date which does not include a time to a datetime type.

Thus the expression:
Code:
CONVERT(char(8),<yourdatetime>,112)
will convert <yourdatetime> to a string in the format yyyymmdd, discarding any time portion of the data. If you then convert that string back to a datetime, the time portion will be set to midnight, thus in effect discarding the time. So, for your question using GetDate():
Code:
CAST(CONVERT(char(8),GetDate(),112) as datetime)
results in a datetime type of today's date without the time.

This BS will eventually go away, as the next release of SQL Server ('Yukon') will finally support the DATE and TIME datatypes which behave as you'd expect.


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old February 18th, 2004, 04:26 PM
Authorized User
 
Join Date: Jul 2003
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks to everybody.

I'm using this solution:
CAST(CONVERT(char(8),GetDate(),112) as datetime)

Pavel






Similar Threads
Thread Thread Starter Forum Replies Last Post
DATE from DATETIME latha Classic ASP Databases 2 May 30th, 2006 11:02 AM
Datetime to Date during Query krashed SQL Language 1 April 4th, 2006 11:59 PM
Seperating Date part from a datetime field ctranjith SQL Server 2000 2 October 25th, 2004 06:42 AM
Convert Part of String to Date twsinc Access VBA 6 October 20th, 2004 03:31 PM
Conversion part of string to date MRvLuijpen Access 5 May 18th, 2004 02:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.