Solved

SQL Syntax convert string to date

Posted on 2011-03-16
5
340 Views
Last Modified: 2012-05-11
This should be easy, but just not working...How does one change an 8 character string 20100204 to a date using either sql syntax or SSIS - tried the DAta conversion but it fails,,,and SQL gives me something funky Dec 9 2 no matter what i use as an arg on the convert function...
0
Comment
Question by:Gray5452
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35151975
CONVERT(datetime, yourfield, 112)

http://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 

Author Comment

by:Gray5452
ID: 35152101
it looks correct when I do it in a select but when I run the update the dates come out looking like Nov 13 2
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35152238
try this.
CONVERT(varchar,CONVERT(datetime,date_col,112),101)

Open in new window

check this example
declare @col varchar(20) = '20100204'
select @col = CONVERT(varchar,CONVERT(datetime,@col,112),101)
select @col -- 02/04/2010

Open in new window

0
 

Expert Comment

by:BlakeRogers
ID: 35152874
Try setting it as a variable first and seeing if that works with Sharaths CONVERT.

The other option is to do a CAST

CAST('2007-05-08 12:35:29.123' AS datetime) AS 'datetime' 

Open in new window

0
 
LVL 3

Accepted Solution

by:
kumarnimavat earned 500 total points
ID: 35165192
You may try selecting by using the below given query
select convert(datetime, '20100204')

I tried updating a datetime field using the query given below which worked fine. You may give it a try.
update Table_1 set test = convert(datetime, '20100204')
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Replication question 9 40
SSRS 2013 - Overlapping reports 2 19
Find results from sql within a time span 11 29
Create snapshot on MSSQL 2012 3 16
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now