Link to home
Start Free TrialLog in
Avatar of jay-are
jay-areFlag for United States of America

asked on

Convert a varchar to a datetime field

Hello Experts!

I have a table that contains a colum with dates.  This column also contains some random data that I have to remove.  Once I've done this I need to insert this column into a table where the column is 'datetime'.  I can't insert/update this column into the table where it is a datetime because the original column is a varchar.

Here is my insert statement:

Insert Into Partsonhand (partnum, [desc], uqoh, pack, list, ucost, [ext-val], o, qoh, qori, qavl, markup, entry, udate , lsdate, lrcvdte, iodate, whs, ytd, pyr, ytdls, f, partnum2)

select partsonhandtemp.partnum, partsonhandtemp.[desc], partsonhandtemp.uqoh, partsonhandtemp.pack, partsonhandtemp.list, partsonhandtemp.ucost, partsonhandtemp.[ext-val], partsonhandtemp.o, partsonhandtemp.qoh, partsonhandtemp.qori, partsonhandtemp.qavl, partsonhandtemp.markup, partsonhandtemp.entry, partsonhandtemp.udate, partsonhandtemp.lsdate, partsonhandtemp.lrcvdte, partsonhandtemp.iodate, partsonhandtemp.whs, partsonhandtemp.ytd, partsonhandtemp.pyr, partsonhandtemp.ytdls, partsonhandtemp.f, partsonhandtemp.partnum2

from partsonhandtemp

left outer join partsonhand on partsonhandtemp.partnum = partsonhand.partnum

where partsonhand.partnum is null

So the column is "udate".  How do I use the convert function to fix this while inserting?
I've tried convert(datetime, udate) but it gives me an error.
Help!
Avatar of rafrancisco
rafrancisco

Try this one:

Insert Into Partsonhand (partnum, [desc], uqoh, pack, list, ucost, [ext-val], o, qoh, qori, qavl, markup, entry, udate , lsdate, lrcvdte, iodate, whs, ytd, pyr, ytdls, f, partnum2)

select partsonhandtemp.partnum, partsonhandtemp.[desc], partsonhandtemp.uqoh, partsonhandtemp.pack, partsonhandtemp.list, partsonhandtemp.ucost, partsonhandtemp.[ext-val], partsonhandtemp.o, partsonhandtemp.qoh, partsonhandtemp.qori, partsonhandtemp.qavl, partsonhandtemp.markup, partsonhandtemp.entry,

case when isdate(partsonhandtemp.udate) = 1 then cast(partsonhandtemp.udate as datetime) else null end,

partsonhandtemp.lsdate, partsonhandtemp.lrcvdte, partsonhandtemp.iodate, partsonhandtemp.whs, partsonhandtemp.ytd, partsonhandtemp.pyr, partsonhandtemp.ytdls, partsonhandtemp.f, partsonhandtemp.partnum2

from partsonhandtemp

left outer join partsonhand on partsonhandtemp.partnum = partsonhand.partnum

where partsonhand.partnum is null
Avatar of jay-are

ASKER

Works perfectly for the insert

How about the update?

update partsonhand
set partnum = partsonhandtemp.partnum, [desc] = partsonhandtemp.[desc],
uqoh = partsonhandtemp.uqoh, pack = partsonhandtemp.pack,
list = partsonhandtemp.list, ucost = partsonhandtemp.ucost,
[ext-val] = partsonhandtemp.[ext-val], o = partsonhandtemp.o,
qoh = partsonhandtemp.qoh, qori = partsonhandtemp.qori,
qavl = partsonhandtemp.qavl, markup = partsonhandtemp.markup,
entry = partsonhandtemp.entry, udate = convert(datetime,partsonhandtemp.udate),
lsdate = partsonhandtemp.lsdate, lrcvdte = partsonhandtemp.lrcvdte,
iodate = partsonhandtemp.iodate, whs = partsonhandtemp.whs,
ytd = partsonhandtemp.ytd, pyr = partsonhandtemp.pyr,
ytdls = partsonhandtemp.ytdls, f = partsonhandtemp.f,
partnum2 = partsonhandtemp.partnum2
from partsonhand inner join partsonhandtemp on partsonhand.partnum = partsonhandtemp.partnum
where partsonhandtemp.partnum = partsonhand.partnum
Avatar of jay-are

ASKER

udate = convert(datetime,partsonhandtemp.udate)

That's what I've tried...doesn't work  :(
ASKER CERTIFIED SOLUTION
Avatar of rafrancisco
rafrancisco

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott Pletcher
udate = CAST(CASE WHEN (partsonhandtemp.udate) = 1 THEN partsonhandtemp.udate ELSE NULL END AS DATETIME),
Avatar of jay-are

ASKER

Yep, worked just fine.  Guess I need to read up on "case"!
Thanks for your help.