Actuarial Outpost
 
Go Back   Actuarial Outpost > Actuarial Discussion Forum > Software & Technology
FlashChat Actuarial Discussion Preliminary Exams CAS/SOA Exams Cyberchat Around the World Suggestions

D.W. Simpson and Company -- Actuary Salary Surveys
Pension, Life, Health and Investment Actuarial Jobs

Property and Casualty Actuarial Jobs   Registration Form


Reply
 
Thread Tools Display Modes
  #1  
Old 01-27-2010, 03:28 PM
jdog2345
Guest
 
Posts: n/a
Default SQL Server code to convert datetime field

Typically I have no problem converting a date field imported as varchar in a query, but for whatever reason, I have imported a field and it’s come over as ‘5/06/2009 0:00:00’ instead of the workable ‘5/06/2009’. Does anyone know how to convert this in the query code (I don’t want to go through each flat file source and remove the ‘0:00:00’.

I have tried month(CAST(CAST([datefield] AS varchar(10)) AS datetime)) and I get the error “The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.”

If I just month() & year() the field(s), it gives me the following error:

“Error converting data type varchar to numeric.”

Has anyone ever run into this problem with a date-type field?

Any help is certainly appreciated.
Reply With Quote
  #2  
Old 01-27-2010, 03:32 PM
Abused Student's Avatar
Abused Student Abused Student is offline
Member
SOA
 
Join Date: Feb 2007
Location: The Eighth Circle of Hell
Favorite beer: Cold and lots of it
Posts: 35,847
Default

Have you tried

Format([datefield],"mm/dd/yyyy") ?
__________________
GAME ON!!!!!!! Let your ness show. Join the D&D fun. Started but applications still accepted


Officially assigned the role of Dictator, 9/30/09. Bow to my whims.
Reply With Quote
  #3  
Old 01-27-2010, 04:42 PM
GadgetGeek's Avatar
GadgetGeek GadgetGeek is offline
Note Contributor
SOA AAA
 
Join Date: Sep 2001
Location: I'm here, where are you?
Studying for a way to escape
College: Illinois College ('87)
Favorite beer: Stag
Posts: 10,515
Default

The function might be char()
__________________
Some people are like clouds. When they disappear, it's a brighter day.
Reply With Quote
  #4  
Old 01-28-2010, 07:13 AM
TwoStep's Avatar
TwoStep TwoStep is offline
Member
CAS SOA
 
Join Date: Oct 2008
Studying for CAS Exam 9
Posts: 780
Default

‘5/06/2009 0:00:00’ is actually a SMALLDATETIME field
(DATETIME has milliseconds like ‘5/06/2009 0:00:00.000’ )


What datatype (ie VARCHAR(10), VARCHAR(15), NVARCHAR(10)) is it currently stored as?

Last edited by TwoStep; 01-28-2010 at 07:15 AM.. Reason: VARCHAR Percision
Reply With Quote
  #5  
Old 01-28-2010, 06:04 PM
TwoStep's Avatar
TwoStep TwoStep is offline
Member
CAS SOA
 
Join Date: Oct 2008
Studying for CAS Exam 9
Posts: 780
Default

I talked to a DBA today, SQL Server 2008 has a DATE datatype, but it wont work if the database was developed in SQL Server 2005
Reply With Quote
  #6  
Old 01-28-2010, 06:12 PM
Steve Grondin Steve Grondin is offline
Member
SOA AAA
 
Join Date: Nov 2001
Posts: 3,630
Default

Finally. You'd think they would have realized there are a whole class of data that doesn't care what time of day something is. Is it 4 bytes?
Reply With Quote
  #7  
Old 01-28-2010, 07:05 PM
TwoStep's Avatar
TwoStep TwoStep is offline
Member
CAS SOA
 
Join Date: Oct 2008
Studying for CAS Exam 9
Posts: 780
Default

DATE is three bytes.

There is also a datatype called TIME (which is just time), it takes 3-5 bytes.
Reply With Quote
  #8  
Old 01-28-2010, 08:27 PM
Steve Grondin Steve Grondin is offline
Member
SOA AAA
 
Join Date: Nov 2001
Posts: 3,630
Default

Quote:
Originally Posted by TwoStep View Post
DATE is three bytes.
Sweet! I didn't think there was anyone left who's as stingy with space as me!
Reply With Quote
  #9  
Old 01-28-2010, 10:30 PM
magillaG magillaG is offline
Member
 
Join Date: Jun 2007
Posts: 2,618
Default

You should be able to cast, for example, the string ‘5/06/2009 0:00:00’ :

cast(‘5/06/2009 0:00:00’ as datetime)

There is a good chance your error is because some of the dates are not valid. For example, maybe you 4/31/2009, or something like that.

A brute force method would be to use excel to make a list of all the valid date strings that are possible for you, and check that each string is in that list. There is probably a more clever way, but I don't know what it is.

Incidently you can pick off hte 0:00:00 by doing:
rtrim(left(s, len(s) - 7))
Reply With Quote
Reply

Thread Tools
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 On
HTML code is Off


All times are GMT -4. The time now is 10:48 PM.


Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
*PLEASE NOTE: Posts are not checked for accuracy, and do not
represent the views of the Actuarial Outpost or its sponsors.
Page generated in 0.29156 seconds with 7 queries