Treating datetime fields as separate date and time fields

Treating datetime fields as separate date and time fields

Post by Dan Sumpti » Thu, 18 Apr 2002 18:01:57



I have an application which needs to store date and time fields
separately, but there don't seem to be any basic date and time types
in SQL Server, just datetimes.

I am currently using datetime for both the date fields and the time
fields - what would be the best way of ensuring that only the date
portion of date fields and the time portion of time fields are
extracted from the database? Is there a way I can do this as a rule
for those fields, or alternatively what would be the SQL code to get
just the date and time portions?

Dan Sumption

 
 
 

Treating datetime fields as separate date and time fields

Post by Tibor Karasz » Thu, 18 Apr 2002 18:17:47


You could have the same time for all rows in the column where you only want the date, and vice
versa. Most often, you then opt for standard date 1 jan 1900 and standard time 00:00:00.

You can have a default for the column where you "only want the date" as:
DEFAULT CONVERT(char(8), CURRENT_TIMESTAMP, 112)

And where you "only want the date":
DEFAULT CONVERT(char(8), CURRENT_TIMESTAMP, 114)

You most probably want CHECK constraints as well. Just extract the date portion (where you only
want time) and make sure it is '19000101', using technique above. And similar for time portion.

As for reading data. As there are no date-only and vice versa, you need to use CONVERT and make
the datetime data strings. Use CONVERT for that, see examples about for starter...
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sql...


Quote:> I have an application which needs to store date and time fields
> separately, but there don't seem to be any basic date and time types
> in SQL Server, just datetimes.

> I am currently using datetime for both the date fields and the time
> fields - what would be the best way of ensuring that only the date
> portion of date fields and the time portion of time fields are
> extracted from the database? Is there a way I can do this as a rule
> for those fields, or alternatively what would be the SQL code to get
> just the date and time portions?

> Dan Sumption


 
 
 

1. Query datetime-field by date when field contains date + time

I have tablefield (sqlserver2000) which datatype is datetime. I have
inserted value "insert into ... datetimefield = {ts '2001-11-24
08:24:12'}..." in it. How can I find (query) that row by date ?

I have tried :
... where datetimefield like {d '2001-11-24'}...
... where datetimefield = {d '2001-11-24'}...
... where datetimefield like {d '2001-11-24%'}...
... where datetimefield like {d '%2001-11-24%'}...

..but query never returns that row. Whats wrong ?!?!

This works :
... where datetimefield = {ts '2001-11-24 08:24:12'}...
but time is unknown in "real life".

Connectionstring  is like:
"Provider=SQLOLEDB;Data Source=SERVERNAME;Initial
Catalog=tempdbase;User Id=sa;Password="

And same problem in  access database ?
... where datetimefield = #11/24/2001#...
... where datetimefield like #11/24/2001#...

Maybe its better to use string-datatype instead datetime...

Jaska

2. JDBC question: getting update row count from Oracle stored procedures

3. Concatenating datetime field and int field to make a date time

4. Urgent Help!!! ORACLE data block corrupted

5. Comparing datetime field (containing date, time values) to a date only string

6. Stored Proc Problem

7. Time Without the Date in datetime field

8. ado find help?

9. java.sql.Date producing a time element when passed to SQL Server datetime field

10. FoxPro Date Field to SQL datetime field

11. how do I separately retrieve the time and date from a datetime field

12. insert datetime based on a date field + fix time (12:00 noon)

13. Storing the date and time in a datetime field