Type DateTime

T

Tony

Hello!

If I want to store a date and time like this 2003-10-08 06:00:49 in the
database is then always the best choice to use type datetime
instead of using varchar.

//Tony
 
L

Leon

Hello!

If I want to store a date and time like this 2003-10-08 06:00:49 in the
database is then always the best choice to use type datetime
instead of using varchar.

//Tony

Yep.. the data type is made for it.

..L.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello!

If I want to store a date and time like this 2003-10-08 06:00:49  in the
database is then always the best choice to use type datetime
instead of using varchar.

//Tony

IS that a question?
if so the answer is yes, the varchar is only a representation of the
date,
Ask yourself this, what if you now want to display it like MM-DD-
YYYY ? if you have a DAteTime it's trivial, if you have a varchar you
have to parse the string and make the conversion.
 
J

Jeff Johnson

If I want to store a date and time like this 2003-10-08 06:00:49 in the
database is then always the best choice to use type datetime
instead of using varchar.

If by "like this" you meant "in this exact format," then you need to
understand that a date is a date is a date; it has no inherent format. A
format is merely a STRING REPRESENTATION of data and can be generated at any
time. You should always store values as values and worry about presentation
later. This means dates, numbers, and certain esoteric things like GUIDs
should always be stored in a native data type and formatted in your
presentation layer.

About the only exception I can think of off hand to "always store the raw
value" is if you were storing phone numbers for presentation. Then you might
really want to store 1-800-HOT-CHKS instead of 18004682427, but even then
you might consider making two columns, one for the raw value and one for the
formatted value.

If on the other hand you simply meant "with this level of precision," then
the answer would be yes, use a datetime (instead of a shortdatetime because
that's only accurate to the minute), but you would still want to use a date
data type and not varchar.
 
A

Arne Vajhøj

Tony said:
If I want to store a date and time like this 2003-10-08 06:00:49 in the
database is then always the best choice to use type datetime
instead of using varchar.

Yes.

With VARCHAR you will sooner or later end up with a lot
of horrible string manipulation in your SQL, because
the functionality you need are not available for
VARCHAR.

You will have to write a lot of code to validate the
before being inserted. Or you will up with 2008-02-31 12:00:00
in the database.

Arne
 
J

Jeff Johnson

You will have to write a lot of code to validate the
before being inserted. Or you will up with 2008-02-31 12:00:00
in the database.

Ah, February 31st, 2008. What a great day that was....
 
H

henon

Hello!

If I want to store a date and time like this 2003-10-08 06:00:49  in the
database is then always the best choice to use type datetime
instead of using varchar.

//Tony

Hi Tony,
If you store a date as string, then you can not easily query the
stored values by date. Also it could happen, that reinterpreting the
date using DateTime.Parse won't work because of different culture. But
if you really must, then save and load dates to and from string
representations using invariant culture.

-- Henon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top