If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > How do I remove time for date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-01-08, 16:19
Datanalysis Datanalysis is offline
Registered User
 
Join Date: Oct 2008
Posts: 23
How do I remove time for date

example when I create a table how when I define datetime as datatype is there a way to define it so only date shows and never time? i need it for a birthday field.

CREATE TABLE Customer
(
Cust_ID int Identity (1000,1) primary key NOT NULL,
Cust_Birthday datetime NULL,
);
Reply With Quote
  #2 (permalink)  
Old 11-01-08, 16:30
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
SQL Server? Which version?
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #3 (permalink)  
Old 11-01-08, 16:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
there are several comingled issues -- retrieving only the date portion of a DATETIME column when it contains datetime values that include a time portion other than 00:00:00 (that's one), ensuring that only datetime values are entered into the column, so that nothing actually need to be done other than retrieve the value (that's two), and retrieving a value from a DATETIME column and disregarding the time portion when displaying it in the app, whether the time portion is 00:00:00 or not (that's three)...

in my opinion you should not store times for birthdates so there's really no problem that needs solving here

the real WTF here is that i talked myself out of giving the dateadd/datediff solution i normally offer

over to you, poots

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 11-09-08, 01:06
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 697
Quote:
Originally Posted by Datanalysis
example when I create a table how when I define datetime as datatype is there a way to define it so only date shows and never time? i need it for a birthday field.

CREATE TABLE Customer
(
Cust_ID int Identity (1000,1) primary key NOT NULL,
Cust_Birthday datetime NULL,
);
To answer "is there a way to define it so only date shows", generally, no. Data types say what data can go in there, not how it looks.

You can, however, force the time to be something reliable like 00:00:00 by adding a check constraint that HOUR(Cust_Birthday) = 0 AND MINUTE(Cust_Birthday) = 0 AND SECOND(Cust_Birthday) = 0. That will simplify your date arithmetic because you will be guaranteed that any difference between two dates is an integral number of days.

Further, you can create a query that only retrieves the date and that formats it pretty well however you like. Rudy's web site has a few examples of e.g. calculating ages.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On