Solved

SQL Syntax convert string to date

Posted on 2011-03-16
5
337 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

758 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

19 Experts available now in Live!

Get 1:1 Help Now