Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
in my table one column called starttime in varchar(50) datatype format..I added one column called starttime1 of datetime.how to copy data from starttime to starttime1..
can you please help me in writing query.
thanks in advance..

What I have tried:

SQL
alter table website_Tracker alter column Starttime datetime

update  Website_Tracker set starttime1=CONVERT(datetime,starttime,101)


update Website_Tracker
set starttime1 = convert (datetime,Starttime)


UPDATE Website_Tracker
SET starttime1 = CONVERT(NVARCHAR(50),CONVERT(SMALLDATETIME, starttime,103))
Posted
Updated 12-Feb-16 2:27am
v2
Comments
Jörgen Andersson 12-Feb-16 5:10am    
What's the format of the dates in the "starttime" column?
Maciej Los 12-Feb-16 8:28am    
What database engine: sql server, MySQL, Oracle, PostgreSQL, other?

Hi Rakhesh,
Can you post some value of starttime? If the format value is same as datetime format in your SQL Server, there should be no problem with that.

I was trying to reproduce your case with this script, but it works for me.
SQL
DECLARE @tempTable TABLE (
	sDate VARCHAR(50) NULL,
	dtmDate DATETIME NULL
)

INSERT INTO @tempTable(sDate, dtmDate)
SELECT '2016-02-12 17:06:32.273', null

SELECT * FROM @tempTable

UPDATE @tempTable SET dtmDate = sDate

SELECT * FROM @tempTable


Cheers. :)
 
Share this answer
 
Comments
Rakhesh baradwaj 12-Feb-16 7:32am    
12/24/2015 1:28:51 AM,my format like this(mm/dd/yyyy hh:mm:ss AM/PM)
please tell me the query to copy records without losing data
If you're using Microsoft SQL Server 2012 or later, use TRY_PARSE[^]:
SQL
UPDATE Website_Tracker
SET StartTime1 = TRY_PARSE(StartTime As datetime USING 'en-US')

You should also consider using the datetime2[^] data type, rather than the older datetime type. datetime requires 8 bytes, and only supports dates from 1753 onwards. datetime2(0) would only use 6 bytes, and supports the same range of dates as the .NET DateTime type.
SQL
UPDATE Website_Tracker
SET StartTime1 = TRY_PARSE(StartTime As datetime2(0) USING 'en-US')


If you're using a different DBMS, or a version of SQL Server earlier than 2012, then you'll need to tell us what you're using. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900