| Author |
Topic |
|
MevaD
Starting Member
32 Posts |
Posted - 2008-10-23 : 12:01:18
|
| Hi,I need to alter data from one column of a table and insert it into another column in the same table. I'm using MS-SQL 2005 as the DB.Here is what the table looks like:Table1------------idfNamelNamedateOfBirthThe dateOfBirth data is imported from another source in the format ddmmmyy as pure text so I can't use date functions to convert.It looks like this:31Jan9918Apr0712Dec68Is it possible to convert to:990131070418681212and then insert the results back into the same table?Thank you. |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-23 : 12:38:29
|
| select replace(convert(varchar(8),cast('31JAN99' as datetime),11),'/','') |
 |
|
|
MevaD
Starting Member
32 Posts |
Posted - 2008-10-23 : 12:50:09
|
| I tried the following:select replace(convert(varchar(8),cast(dateOfBirth as datetime),11),'/','') from Table1Getting the following error:Msg 241, Level 16, State 1, Line 1Conversion failed when converting datetime from character string.Any ideas? |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-23 : 13:03:40
|
| One of your dateOfBirth values isn't in the correct date format.show me some :select * from tabl1 where isdate(dateOfBirth)=0 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-23 : 13:04:22
|
make a datetime field and store into it like thisSELECT CONVERT(datetime,STUFF(STUFF(dateOfBirth,3,0,' '),7,0,' '),6) from table1 |
 |
|
|
MevaD
Starting Member
32 Posts |
Posted - 2008-10-23 : 13:11:23
|
| Thanks hanbingl.I ran select * from tabl1 where isdate(dateOfBirth)=0The results are 377 rows of blank data and 5 rows with a value of 0This data comes from another source so I don't have much control over what is in the column. |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-23 : 13:13:32
|
juse filter out your query:select replace(convert(varchar(8),cast(dateOfBirth as datetime),11),'/','') from Table1where isdate(dateOfBirth )=1 The question is how do you want to deal with blanks and 0s... |
 |
|
|
MevaD
Starting Member
32 Posts |
Posted - 2008-10-23 : 13:22:00
|
| I'll probably just dump them out as errors with the front-end application.Thanks hanbingl and visakh!!!I really appreciate your help. |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-23 : 13:24:30
|
| YOU BETCHA! |
 |
|
|
|