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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change text to datetime

Status
Not open for further replies.

scc

Programmer
Apr 30, 2001
218
0
0
US
I have a field that is char(14) and I would like to write a query (for a view) that will return a datetime field that will be recognized by MS Access as a datetime field.

Here's a sample of the data in stored in SQL Server:

20041215163407

the field is named DeliveryTime

Can someone help with this?

TIA
 
SELECT
convert(smallDateTime,''''
+ substring(txt,5,2) + '/'
+ substring(txt,7,2) + '/'
+ substring(txt,1,4) + ' '
+ substring(txt,9,2) + ':'
+ substring(txt,11,2) + ':'
+ substring(txt,13,2)
)
FROM myTable

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Darn - forgot the end

SELECT
convert(smallDateTime,''''
+ substring(txt,5,2) + '/'
+ substring(txt,7,2) + '/'
+ substring(txt,1,4) + ' '
+ substring(txt,9,2) + ':'
+ substring(txt,11,2) + ':'
+ substring(txt,13,2)
+ '''')
)
FROM myTable

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
above gives error

Syntax error converting datetime from character string.

this does it:
Code:
declare @string char (14)
set @string = '20041215163407'
set dateformat MDY
SELECT CONVERT(datetime,
''+ substring(@string,5,2) + '/'
   + substring(@string,7,2) + '/'
   + substring(@string,1,4) + ' '
   + substring(@string,9,2) + ':'
   + substring(@string,11,2) + ':'
   + substring(@string,13,2))

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
I'm having problems getting either of these to work.

dbomrrsm: I don't know how I can use a field to bring back the data instead of hardcoding it...

mwolf00: this brings back an error that says--

The data type int is invalid for the substring function. Allowed types are: char/varchar, nchar/nvarchar, and binary/varbinary.
 
Code:
SELECT
  convert(smallDateTime,'' 
   + cast(substring(txt,5,2) as char(2)) + '/'
   + cast(substring(txt,7,2) as char(2)) + '/'
   + cast(substring(txt,1,4) as char(4)) + ' '
   + cast(substring(txt,9,2) as char(2)) + ':'
   + cast(substring(txt,11,2) as char(2)) + ':'
   + cast(substring(txt,13,2) as char(2))
   + '')
 )
FROM myTable



Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Exactly what I needed!

Thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top