Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

Varchar to Datetime conversion

I have the following

2009/12/11 01:35:58.347000  in varchar/string variable.

I need the time part or both date &time as DATETIME,So that i can compare this with current datetime.

Could any one let me know how to convert this string to DATETIME.

I tried using COVERT() function,doesn't work.I got the following error message
"Conversion failed when converting datetime from character string'.


Thanks



0
parpaa
Asked:
parpaa
  • 2
1 Solution
 
Aneesh RetnakaranDatabase AdministratorCommented:
select CAST(LEFT('2009/12/11 01:35:58.347000',23) as datetime)
0
 
parpaaAuthor Commented:
Thanks it's wotking fine
0
 
parpaaAuthor Commented:
Fast Response
0
 
TheRatManCommented:
Here is sample SQL.  Problem is that the string contains extra information, you need to trim it

DECLARE @s varchar(200), @d datetime
SET @s = '2009/12/11 01:35:58.347000'
SET @d = CONVERT(DATETIME, LEFT(@s, 23))
--@d now contains the value in a datetime variable
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now