Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 convert data format

Author  Topic 

jung1975
Aged Yak Warrior

503 Posts

Posted - 2005-08-08 : 16:15:32
I have a column called [date of birth] with Varchar(20) data type
the data looks like '18-May-53'

I would like to convert this data format to '05/18/53'. How can I do this?


X002548
Not Just a Number

15586 Posts

Posted - 2005-08-08 : 16:20:06
[code]DECLARE @date varchar(20)
SELECT @date = '18-May-53'
SELECT CONVERT(datetime,@date,1), ISDATE(@date)

[/code]



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-09 : 01:48:52
>> have a column called [date of birth] with Varchar(20) data type

Why did you use Varchar datatype to store date when datetime field is available?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

kapilarya
Yak Posting Veteran

86 Posts

Posted - 2005-08-09 : 03:37:02
check this out

DECLARE @date varchar(10)
SELECT @date = '18-May-53'
SELECT convert(varchar,CONVERT(datetime,@date,1),1)

Kapil Arya
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-09 : 04:45:58
This type of conversions are not necessary if proper data types are used

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jung1975
Aged Yak Warrior

503 Posts

Posted - 2005-08-09 : 11:44:56
Problem is there are 6000 records , so I can't manually input the data like you said..

DECLARE @date varchar(10)
SELECT @date = '18-May-53'
SELECT convert(varchar,CONVERT(datetime,@date,1),1)




Go to Top of Page

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-08-09 : 11:56:38
Just as a small clarification, the style parameter of '1' doesn't do anything with the datetime datatype, only with the varchar datatype. Therefore, the conversion should be:

SELECT CONVERT(varchar, CONVERT(datetime, @date), 1)
Go to Top of Page

jung1975
Aged Yak Warrior

503 Posts

Posted - 2005-08-09 : 12:09:49
I am trying to use a cursor to convert every records in the table..but gettign an errer: "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."

declare @date varchar(20)

declare cc cursor
fast_forward
for

select [date of birth] from f_labone_staging
Open cc
fetch next from cc into @date
while @@fetch_status = 0
begin

update f_labone_staging
set [date of birth] = CONVERT(varchar, CONVERT(datetime, [date of birth]), 1)
Fetch next from cc into @date
end
close cc
deallocate cc



Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-08-09 : 12:33:55
First, skip the cursor. Just issue the UPDATE statement and let it run as a set-based operation. It will be amazingly faster than the cursor.

Second, if you're getting and out-of-range value error message, that is probably because you have an incorrectly entered date. You'll need to do some searching and scrubbing of the data. Perhaps search for records that have only one hyphen or that have more than two hyphens. Or, with 6000 records, it wouldn't take long to just do a visual scan looking for a date that is out of alignment.

---------------------------
EmeraldCityDomains.com
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-08-09 : 12:46:17
What does this give you?

select * from f_labone_staging ISDATE([date of birth])=0




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-08-09 : 12:51:57
Oh, SURE, Brett! That's just like you to come up with an easier way.

---------------------------
EmeraldCityDomains.com
Go to Top of Page

jung1975
Aged Yak Warrior

503 Posts

Posted - 2005-08-09 : 13:13:55
Thanks! I found the problem...




Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-08-09 : 13:15:50
So....

You're not gonna share?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -