SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 convert varchar to date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

DeNam
Starting Member

Sweden
15 Posts

Posted - 10/16/2013 :  02:34:47  Show Profile  Reply with Quote
data field is a varachar looks like 531112-0330, where the first six characters form a date.

I want to convert to a date field.

How should i do this?

tkizer
Almighty SQL Goddess

USA
36949 Posts

Posted - 10/16/2013 :  02:35:55  Show Profile  Visit tkizer's Homepage  Reply with Quote
How does 531112 convert to a date?

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/16/2013 :  02:52:42  Show Profile  Reply with Quote
is it julian date?
http://visakhm.blogspot.in/2012/05/julian-to-iso-date-conversion-function.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

DeNam
Starting Member

Sweden
15 Posts

Posted - 10/16/2013 :  03:31:46  Show Profile  Reply with Quote
My datafield is named person_number and the value is 531112-0330.
Data type is varchar(15).

So I am doing the following:

select convert(date, '19' + LEFT(person_number,6), 109) from dbo.Table1

The result I want is 1953-11-12 and datafield: Date.

I get the following message: Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.


At the same time, this works:

Declare @date varchar(100)
set @date = '20100320'
select CONVERT(date, @date,109).

What am i doing wrong?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/16/2013 :  03:39:01  Show Profile  Reply with Quote
try this

select convert(date, case when LEFT(person_number,2)>= 50 THEN '19' ELSE '20' END + LEFT(person_number,6), 109) from dbo.Table1 WHERE LEN(LEFT(person_number,CHARINDEX('-',person_number)-1))=6


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

DeNam
Starting Member

Sweden
15 Posts

Posted - 10/17/2013 :  08:29:23  Show Profile  Reply with Quote
Does not work.

Following message:

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.


Tried to convert the varchar to numeric before converting to date. Did not work either.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/17/2013 :  10:04:36  Show Profile  Reply with Quote
that means you've some data which is not having valid date values


select convert(date, case when LEFT(person_number,2)>= 50 THEN '19' ELSE '20' END + LEFT(person_number,6), 109) from dbo.Table1 WHERE LEN(LEFT(person_number,CHARINDEX('-',person_number)-1))=6 AND ISDATE(LEFT(person_number,CHARINDEX('-',person_number)-1))=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000