Solved

# Converting String to date

Posted on 2006-06-21
250 Views
I'm trying to substring and contat a field to a date format, problem is the string is not always 8 numbers, sometimes it's 7:

1132006 -> 1/13/2006
12132006 -> 12/13/2006

The field is TTXDA

SELECT     TTXDA AS DATE
FROM         TAXTRANS
ORDER BY DATE DESC

I would like the date in 01/13/2006 format, thanks!!
0
Question by:jcbergman
[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
• 2
• 2

LVL 5

Accepted Solution

MageDribble earned 50 total points
ID: 16954743
SELECT
CASE len(ttxda)
WHEN 8 THEN  LEFT(TTXDA, 2) + '/' + SUBSTRING(TTXDA,3,2) + '/' + RIGHT(TTXDA,4)
WHEN 7 THEN  LEFT(TTXDA, 1) + '/' + SUBSTRING(TTXDA,2,2) + '/' + RIGHT(TTXDA,4)
ELSE 'Unknown'
END
FROM TAXTRANS.....
0

LVL 5

Expert Comment

ID: 16954752
WHOOPS didn't see you wanted 1/13/2006 as 01...

Change WHEN 7... to the following:

WHEN 7 THEN  '0' + LEFT(TTXDA, 1) + '/' + SUBSTRING(TTXDA,2,2) + '/' + RIGHT(TTXDA,4)
0

LVL 7

Expert Comment

ID: 16954760
Don't forget the leading zero if it's 7 long

SELECT
CASE LEN(TTXDA)
WHEN 7 THEN '0' + LEFT(TTXDA,1) + '/' + RIGHT(LEFT(TTXDA,3),2) + '/' + RIGHT(TTXDA,4)
WHEN 8 THEN LEFT(TTXDA,2) + '/' + RIGHT(LEFT(TTXDA,4),2) + '/' + RIGHT(TTXDA,4)
ELSE NULL
END
0

Author Comment

ID: 16954950
thanks guys!
0

Author Comment

ID: 16955036
Hrm... when I try and order by it, it doesn't sort the date right.... anyway

How do I property convert this to a date so i can sort by it?

Thanks
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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
###### Suggested Courses
Course of the Month8 days, 10 hours left to enroll