Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2005, 04:45 PM
Authorized User
 
Join Date: Oct 2003
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default convert string to date

Hi,

I have a field in my table called reportdate varchar (50). I import a csv file to that table that receives dates in 2 forms for example
09122005 and 9122005. I need to convert Reportdate to numeric and then to date in the following format : 09/12/2005.
I am not sure how to accomplisht that.
Any help would be appreciated.
Thanks

 
Old March 10th, 2005, 12:37 AM
Authorized User
 
Join Date: Feb 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not clear on objective to convert date into NUMERIC first and then in the format dd/mm/yyyy.......why not directly to dd/mm/yyyy format?

This may be the solution you are looking for.....

1. Import the raw data as it is in the table.

2. UPDATE <TableName>
   SET ReportDate='0'+ReportDate
   WHERE LEN(REportDate)=7

3. UPDATE <TableName>
   SET ReportDate = LEFT(ReportDate,2)+'/'+SUBSTRING(ReportDate,3,2)+'/'+RIGHT(ReportDate,4)

Cheers,
Pooja Falor
 
Old March 10th, 2005, 05:57 AM
Authorized User
 
Join Date: Oct 2003
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, that worked great.

 
Old March 15th, 2007, 10:12 AM
Registered User
 
Join Date: Mar 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by collie
 Thanks, that worked great.

I tried this with a text field that I wanted to convert to YYYYMMDD format and got the message: String or binary data would be truncated. Any clue why?

Used the same code as above except that the year portion is LEFT instead of RIGHT.

 
Old March 15th, 2007, 03:50 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you will need to look at your data and see the different types of dates that people entered.

For example if you have the following
YYYYMMDD
DDMMYYYY
MMDDYYYY
all in the same field you can't fix it easily with one statement.

Try changing the data a few rows at a time till you come across the field that has the issue. So for example only do it where the first four characters are 2000 so you know thats the year, then piece by piece do the rest. You probably have some bad data that will error out with the valid commands given to you earlier.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string to Date rprasad012 ASP.NET 1.0 and 1.1 Basics 1 March 19th, 2008 07:07 AM
how to convert string to date tgopal Javascript 2 March 18th, 2008 03:33 AM
convert string to date deeptibg SQL Server DTS 1 December 22nd, 2005 08:47 PM
Convert Part of String to Date twsinc Access VBA 6 October 20th, 2004 03:31 PM
Convert String Date to Date for a SQL Query tdaustin Classic ASP Basics 4 July 7th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.