Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Convert a varchar to a datetime field

Posted on 2005-04-28
6
Medium Priority
?
323 Views
Last Modified: 2010-03-19
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!
0
Comment
Question by:jay-are
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13886975
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
0
 

Author Comment

by:jay-are
ID: 13887017
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
0
 

Author Comment

by:jay-are
ID: 13887026
udate = convert(datetime,partsonhandtemp.udate)

That's what I've tried...doesn't work  :(
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 28

Accepted Solution

by:
rafrancisco earned 2000 total points
ID: 13887047
Here's for 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 = case when isdate(partsonhandtemp.udate) = 1 then cast(partsonhandtemp.udate as datetime) else null end,


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
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 13887054
udate = CAST(CASE WHEN (partsonhandtemp.udate) = 1 THEN partsonhandtemp.udate ELSE NULL END AS DATETIME),
0
 

Author Comment

by:jay-are
ID: 13887077
Yep, worked just fine.  Guess I need to read up on "case"!
Thanks for your help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

580 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