Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to insert datetime column into table


how to insert datetime column into table

Author
Message
denis.gendera
denis.gendera
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 90
All,
I'm having strange issue trying to do the following which I thought was just going to be straight forward

select top 5 machine.name, machine.model, machine.scantime
'INSERT INTO [SMS_000].[dbo].[BMCMachines] ([ComputerName],[MachineModel],[stime]) VALUES (' + '''' + machine.name + ''',' + '''' + machine.model + ''',' + convert(datetime,machine.scantime,112) + ''')' from machine

when I run the query comes back with following error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

I can confirm that machine.scantime is "DateTime" column.
OTF
OTF
Old Hand
Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)

Group: General Forum Members
Points: 316 Visits: 4128
denis.gendera (3/20/2013)
All,
I'm having strange issue trying to do the following which I thought was just going to be straight forward

select top 5 machine.name, machine.model, machine.scantime
'INSERT INTO [SMS_000].[dbo].[BMCMachines] ([ComputerName],[MachineModel],[stime]) VALUES (' + '''' + machine.name + ''',' + '''' + machine.model + ''',' + convert(datetime,machine.scantime,112) + ''')' from machine

when I run the query comes back with following error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

I can confirm that machine.scantime is "DateTime" column.



Take a closer look at your Convert function and be sure that is what you are actually wanting to do.
devilsid
devilsid
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 90
Just confirm if scantime is date or date time ???

also if scantime is already date, why convert it again..
Steve JP
Steve JP
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 328
denis.gendera (3/20/2013)
All,
I'm having strange issue trying to do the following which I thought was just going to be straight forward

select top 5 machine.name, machine.model, machine.scantime
'INSERT INTO [SMS_000].[dbo].[BMCMachines] ([ComputerName],[MachineModel],[stime]) VALUES (' + '''' + machine.name + ''',' + '''' + machine.model + ''',' + convert(datetime,machine.scantime,112) + ''')' from machine

when I run the query comes back with following error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

I can confirm that machine.scantime is "DateTime" column.




The issue with the convert is that the 3rd arguement is the style and is used to set the style when you convert from a datetime to a varchar, not the other way around.

What is the datatype of the [BMCMachines].[stime] column because the other issue is that the convert style of 112 is for an ISO date with no time element to it??
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 2763
please provide table structure

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9025 Visits: 19036
Steve JP (3/26/2013)
[quote]...The issue with the convert is that the 3rd arguement is the style and is used to set the style when you convert from a datetime to a varchar, not the other way around....


It works both ways as documented in BOL under [b]CAST and CONVERT (Transact-SQL)
.

SELECT DateAsCharacter = CONVERT(VARCHAR(25),GETDATE(),100)
-- 'Mar 26 2013 12:49PM'
SELECT DateAsDate = CONVERT(DATETIME,'Mar 26 2013 12:49PM',100)
-- 2013-03-26 12:49:00.000



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9025 Visits: 19036
[b]select top 5 machine.name, machine.model, machine.scantime
'INSERT INTO [SMS_000].[dbo].[BMCMachines] ([ComputerName],[MachineModel],[stime]) VALUES (' + '''' + machine.name + ''',' + '''' + machine.model + ''',' + convert(datetime,machine.scantime,112) + ''')' from machine


This query is syntactically incorrect. Try putting a comma after machine.scantime. What is it that you are really trying to do? How about this?

INSERT INTO [SMS_000].[dbo].[BMCMachines] 
([ComputerName],[MachineModel],[stime])
SELECT top 5
machine.name,
machine.model,
machine.scantime
FROM machine



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Steve JP
Steve JP
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 328
SELECT DateAsCharacter = CONVERT(VARCHAR(25),GETDATE(),100)
-- 'Mar 26 2013 12:49PM'

SELECT DateAsDate = CONVERT(DATETIME,'Mar 26 2013 12:49PM',100)
-- 2013-03-26 12:49:00.000



Dont totally agree with this as the following will error which is as I expect because the style is not vailid
 -- Just to 100% prove the style is invalid
SELECT DateAsCharacter = CONVERT(VARCHAR(25),GETDATE(),25510)

Msg 281, Level 16, State 1, Line 1
25510 is not a valid style number when converting from datetime to a character string.




But the following seems to want to ignore the style arguement.


SELECT [DateAsDate AM] = CONVERT(DATETIME,'Mar 26 2013 02:49AM' )
SELECT [DateAsDate PM] = CONVERT(DATETIME,'Mar 26 2013 02:49PM' )
SELECT [DateAsDate mm-dd-yy] = CONVERT(DATETIME,'Mar 26 2013 02:49PM' , 110 )
SELECT [DateAsDate yyyymmdd] = CONVERT(DATETIME,'Mar 26 2013 02:49PM' , 112 )
SELECT [DateAsDate No Such format] = CONVERT(DATETIME,'Mar 26 2013 02:49PM' ,25510) -- invalid style so Error? or Ignore?




DateAsDate AM = 2013-03-26 02:49:00.000

DateAsDate PM = 2013-03-26 14:49:00.000

DateAsDate mm-dd-yy = 2013-03-26 14:49:00.000

DateAsDate yyyymmdd = 2013-03-26 14:49:00.000

DateAsDate No Such format = 2013-03-26 14:49:00.000



ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9025 Visits: 19036
Steve JP (3/27/2013)
SELECT DateAsCharacter = CONVERT(VARCHAR(25),GETDATE(),100)
-- 'Mar 26 2013 12:49PM'

SELECT DateAsDate = CONVERT(DATETIME,'Mar 26 2013 12:49PM',100)
-- 2013-03-26 12:49:00.000



Dont totally agree with this as the following will error which is as I expect because the style is not vailid
 -- Just to 100% prove the style is invalid
SELECT DateAsCharacter = CONVERT(VARCHAR(25),GETDATE(),25510)

Msg 281, Level 16, State 1, Line 1
25510 is not a valid style number when converting from datetime to a character string.



- and your point is?





But the following seems to want to ignore the style arguement.


SELECT [DateAsDate AM] = CONVERT(DATETIME,'Mar 26 2013 02:49AM' )
SELECT [DateAsDate PM] = CONVERT(DATETIME,'Mar 26 2013 02:49PM' )
SELECT [DateAsDate mm-dd-yy] = CONVERT(DATETIME,'Mar 26 2013 02:49PM' , 110 )
SELECT [DateAsDate yyyymmdd] = CONVERT(DATETIME,'Mar 26 2013 02:49PM' , 112 )
SELECT [DateAsDate No Such format] = CONVERT(DATETIME,'Mar 26 2013 02:49PM' ,25510) -- invalid style so Error? or Ignore?




DateAsDate AM = 2013-03-26 02:49:00.000

DateAsDate PM = 2013-03-26 14:49:00.000

DateAsDate mm-dd-yy = 2013-03-26 14:49:00.000

DateAsDate yyyymmdd = 2013-03-26 14:49:00.000

DateAsDate No Such format = 2013-03-26 14:49:00.000




The style argument for converting a date as string in the format 'Mar 26 2013 12:49PM' to a datetime data type using CONVERT() is 100. Other style arguments are likely to fail. I'm sorry but I still don't see your point. None of this appears to prove anything either way. Have a quick read of CAST and CONVERT in BOL, specifically CONVERT relating to dates. The explanation is quite good. If you're still unsure, post back.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Steve JP
Steve JP
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 328
Steve JP (3/26/2013)
....

The issue with the convert is that the 3rd arguement is the style and is used to set the style when you convert from a datetime to a varchar, not the other way around.

...


ChrisM@Work (3/27/2013)
....
The style argument for converting a date as string in the format 'Mar 26 2013 12:49PM' to a datetime data type using CONVERT() is 100. Other style arguments are likely to fail. I'm sorry but I still don't see your point. None of this appears to prove anything either way. Have a quick read of CAST and CONVERT in BOL, specifically CONVERT relating to dates. The explanation is quite good. If you're still unsure, post back......



Chris, I thought my example was clear in that the style code ( of 25510 ) that I first used doesnt exist. I know this & you know this, but others may not, as this is the newbies section hence I posted the code. The next code block shows that the Style arguement is often ignored for a conversion from varchar to datetime. It also doesnt have any affect on the time. ie the 103 (which is English) will take a datetime to a varchar as only a date of format dd/mm/yyyy. But from a varchar to datetime will take the time element if it exists as well so can be a bit misleading for some. How many times have people asked how to truncate the time element from a datetime value.

If the style arguement doest exist but cant be applied then it will error. Other times like I have shown it will ignore the style arguement if the actual data contradicts the style.

One of the most reliable methods I have found to enter datetime is yyyy-mm-dd hh:mm:ss:mmm (or the full iso style)
Another option is to use SET DATEFORMAT, although I'm not certain what lifespan this has long term.


I havent got all day for these forums and can only post while jobs are running etc. So yes some of the time my comments are a bit too brief, but mainly I try and post a bit more than "go read the BOLs".
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search