Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Syntax convert string to date

Posted on 2011-03-16
5
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

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 41

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

721 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