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
 General SQL Server Forums
 New to SQL Server Programming
 Insert two columns data in one

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2010-05-18 : 06:57:03
Hi,

I want to insert the data from two columns into one columns and also want to convert it from int to datetime datatype.
I have two tables

1 . create table date( rundate datetime)
2 . create table fromdate ( lastdate int ,
lasttime int)
fromdate tabel contains interger data like this (20100407,161358)


I want to combine the this two columns into one in table date and want
to store it into this format 2010-04-07 16:13:58 .could someone please help me one this?


Thanks
vijay

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-05-18 : 07:15:29

insert into date(rundate )

Select cast(cast(lastdate as varchar(10)) + ' '+left(cast(lasttime as varchar(10)),2)
+':'+substring(cast(lasttime as varchar(10)),3,2)+':'+right(cast(lasttime as varchar(10)),2) as datetime) from fromdate


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2010-05-18 : 07:21:29
Thank you very much it is working...



quote:
Originally posted by senthil_nagore


insert into date(rundate )

Select cast(cast(lastdate as varchar(10)) + ' '+left(cast(lasttime as varchar(10)),2)
+':'+substring(cast(lasttime as varchar(10)),3,2)+':'+right(cast(lasttime as varchar(10)),2) as datetime) from fromdate


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/


Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-05-18 : 07:22:38
Welcome :)

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

prakash.mestry
Starting Member

4 Posts

Posted - 2010-05-19 : 05:07:28
try below one, which is simple one

declare @s int, @s2 int
set @s = 20100407
Set @s2 = 161358

Select convert(datetime,convert(char(8),@s) + ' '+ left(@s2,2) + ':' +
right(left(@s2,4),2) + ':' +
right(@s2,2))
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-19 : 05:34:36
I can't believe that in your integer column are always values with two digits for the hour part.
What's about 9:00 in the morning?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

prakash.mestry
Starting Member

4 Posts

Posted - 2010-05-20 : 01:33:02
good question..

quote:
Originally posted by webfred

I can't believe that in your integer column are always values with two digits for the hour part.
What's about 9:00 in the morning?


No, you're never too old to Yak'n'Roll if you're too young to die.

Go to Top of Page
   

- Advertisement -