Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert CHAR to DATE

Status
Not open for further replies.

strom99

MIS
Nov 28, 2000
126
US
I have the following field process_datetime which looks like this...(20020617094637) it is a CHAR field, I need to convert this to a date field that looks like '2002-06-17' or '06/17/2002'. I've tried both CONVERT and CAST with no luck in getting it to work. Any ideas? Thanks much!!

==========
select
tbl1.ref_nbr,
convert(datetime,tbl1.process_datetime,120),
cast(tbl1.process_datetime AS datetime)
from
i12 tbl1

where
tbl1.ref_nbr like 'm000013'

===========

ERRORS:
Server: Msg 241, Level 16, State 1, Line 1
Syntax error converting datetime from character string.
 

try this:


select
tbl1.ref_nbr,
cast(left(tbl1.process_datetime,8) AS datetime)
from
i12 tbl1

where
tbl1.ref_nbr like 'm000013'

 
This should help out a bit ...

declare @String Char(14)
declare @NewDate DateTime

SET @String = '20020617094637'

SET @newdate = CONVERT(DateTime,SUBSTRING(@String,1,8),110)

print convert(char,@NewDate,101)

Thanks

J. Kusch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top