Solved

Converting String to date

Posted on 2006-06-21
5
247 Views
Last Modified: 2012-08-13
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
Comment
Question by:jcbergman
  • 2
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
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

by:MageDribble
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

by:wesbird
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

by:jcbergman
ID: 16954950
thanks guys!
0
 

Author Comment

by:jcbergman
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

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.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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…

803 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