Wrox Programmer Forums
|
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 July 28th, 2003, 10:29 AM
Authorized User
 
Join Date: Jun 2003
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default datatype for time

Hello

I want to have a field in DB holding only times. Is there any other datatype than datetime and smalldatetime?

If I dont have any other options, how can I insert my time values in the smalldatetime field (I mean in what format should my string be)?

Thanks


Nick
 
Old July 28th, 2003, 01:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

There are only a datetime and smalldatetime datatypes. These types always store both a date and a time. You can't change that.

If you assign a value to a datetime type and use only a date, then the system will automatically assign a time of midnight. If you only assign a time, then the date will be automatically assigned a value of '1/1/1900'.

When you manipulate these columns, you simply ignore the portion you dont care about. Thus, store a time in a datetime column by:
Code:
UPDATE Yourtable SET YourTimeColumn='13:14:15' WHERE ...
will set the YourTimeColumn value to 15 seconds past 14 minutes past 1 PM on Jan 1, 1900. You can use a variety of formats to specify a datetime, such as hh:mm:ss or hh:mm:ss AM/PM, etc. See 'datetime data type, formats' in BOL.

To extract just the time portion from a datetime datatype, use the CONVERT function, e.g:
Code:
SELECT CONVERT(char(8),YourTimeColumn,8)
will return the time as a string in hh:mm:ss format. Again, see BOL for the CONVERT function.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
datatype umeshtheone SQL Server 2000 2 May 16th, 2007 11:56 AM
datatype of sql MunishBhatia SQL Server 2005 4 January 5th, 2007 10:22 PM
DataType Validation Baby_programmer ASP.NET 1.0 and 1.1 Basics 0 October 30th, 2004 08:00 PM
How Could I use C APIs with different DataType?? 6cet6 VS.NET 2002/2003 0 March 24th, 2004 09:20 PM
datatype ngang SQL Language 1 November 18th, 2003 09:55 AM





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