![]() |
|
|
|||||||
| FlashChat | Actuarial Discussion | Preliminary Exams | CAS/SOA Exams | Cyberchat | Around the World | Suggestions |
D.W. Simpson and Company -- Actuary Salary
Surveys |
![]() |
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 its 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 dont 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. |
|
#2
|
||||
|
||||
|
Have you tried
Format([datefield],"mm/dd/yyyy") ?
__________________
GAME ON!!!!!!! Let your ness show. Join the D&D fun. Started but applications still acceptedOfficially assigned the role of Dictator, 9/30/09. Bow to my whims. |
|
#4
|
||||
|
||||
|
‘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 |
|
#9
|
|||
|
|||
|
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)) |
![]() |
| Thread Tools | |
| Display Modes | |
|
|