Solved

SQL 2008 - Convert string to date

Posted on 2013-01-25
5
584 Views
Last Modified: 2013-02-12
Good morning,

I have a number of string fields that represent dates.  I would like to convert them to date fields like so:

From: 25-JAN-13
To: 01/25/2013

Thanks in advance for any help.
0
Comment
Question by:ttist25
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 38818843
0
 
LVL 6

Assisted Solution

by:esolve
esolve earned 250 total points
ID: 38818863
SET DATEFORMAT DMY

DECLARE @var NVARCHAR(100)
SET @var = '25-JAN-13'
SELECT CONVERT(varchar, CONVERT(DATETIME, @var, 101), 101)
0
 
LVL 1

Author Comment

by:ttist25
ID: 38819147
Thanks for the responses guys.  

Angel - I did a brief review of the article you sent.  Thank you.  I will need to review it again (probably several times) to get it through my thick skull before I try to implement it.  :)

Esolve - thanks for your response as well.  In your example you use the date from my example (25-JAN-13).  I should've been more specific and said I am trying to "batch" convert the dates that are in the format dd-mmm-yy to mm/dd/yyyy (although after reading the article I'm not sure the slash format is what I want to do).  

Thanks again for your help and especially the article (teach a man to fish although a fish would taste great right now).
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 38819567
If you're going to store dates in a varchar format, use 'YYYYMMDD'.  This format is always interpreted correctly, so you never need any error-prone and time-consuming conversion codes to use it.
0
 
LVL 1

Author Closing Comment

by:ttist25
ID: 38881745
Thanks - sorry with the delay awarding points.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

706 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