在插入时连接两个值 - SQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9535224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate Two Values On Insert - SQL
提问by RogueSpear00
I'm attempting to accomplish what seems to be something simple, but I'm unable to find a solution - I'm probably wording it incorrectly.
我正在尝试完成似乎很简单的事情,但我找不到解决方案 - 我可能措辞不正确。
I have a third party software that outputs date and time as two separate values, one for date and the other for time, and I need to be able to INSERT
that data as one datetime
field into SQL.
我有一个第三方软件,它将日期和时间输出为两个单独的值,一个用于日期,另一个用于时间,我需要能够INSERT
将该数据作为一个datetime
字段输入 SQL。
The third party software is built on a proprietary language based on .NET, and using SQL Server 2005.
第三方软件基于 .NET 的专有语言构建,并使用 SQL Server 2005。
Here are my two values in the software:
这是我在软件中的两个值:
let cbdate1 = cbdate
let cbtime1 = cbhr & ":" & cbmn & ":00 " & ampm
And then the SQL would go as:
然后 SQL 将变成:
DOSQL "INSERT INTO Leads (DateTimeField) VALUES (cbdate1 + ' ' + cbtime1)"
So essentially, I am attempting to join the two values upon insert into the table. That does not work, and I'm unable to direct myself to find an appropriate answer.
所以基本上,我试图在插入表时连接这两个值。那不起作用,我无法指导自己找到合适的答案。
Additional Information:
附加信息:
The Date (cbdate1) is presented as "MM/DD/YYYY" and the Time is simply joined as presented "HH:MM:00 AM"
日期 (cbdate1) 显示为“MM/DD/YYYY”,时间简单地连接为“HH:MM:00 AM”
回答by Laurence Burke
You are currently using double quotes you should instead use single quotes since that is a valid string in SQL.
您当前使用的是双引号,您应该使用单引号,因为它是 SQL 中的有效字符串。
DOSQL "INSERT INTO Leads (DateTimeField) VALUES (cbdate1 + ' ' + cbtime1)"
Edit:
编辑:
Now if you get further problems it might be because your DateTimeField
is a datetime
datatype. Now you could then after concatenating convert or cast the string to the correct format.
现在,如果您遇到更多问题,可能是因为您DateTimeField
是datetime
数据类型。现在您可以在连接后转换或将字符串转换为正确的格式。
Like:
喜欢:
DOSQL "INSERT INTO Leads (DateTimeField) VALUES (Convert(datetime, cbdate1 + ' ' + cbtime1))"
Edit #2:
编辑#2:
Without a 24 hour part you would need a mon dd yyyy format ex: Oct 22 2012. Otherwise you might have to try and get the time part into a 24 hour format.
如果没有 24 小时制,您将需要 mon dd yyyy 格式,例如:Oct 22 2012。否则您可能必须尝试将时间部分设为 24 小时制。
回答by Christian Hayter
Join the date and time values together in your source code first, then insert the single value into the database. You don't say what programming language you are using, so I can't tell you how to do that. However, it is simple to do in VB6 and any .NET language.
首先在源代码中将日期和时间值连接在一起,然后将单个值插入到数据库中。你没有说你使用的是什么编程语言,所以我不能告诉你怎么做。但是,在 VB6 和任何 .NET 语言中都很容易做到。
回答by ImGreg
What you need to do is merge the strings together, then cast as a datetime type. Make sure that the strings are in the proper format. (ie. 01-01-01, 01/01/01, etc.)
您需要做的是将字符串合并在一起,然后转换为日期时间类型。确保字符串的格式正确。(即 01-01-01、01/01/01 等)
INSERT INTO LEADS (DateTimeField) VALUES (CAST((cbdate1 + ' ' + cbtime1) as datetime)
Refer to the msdn for output formats depending on what you want.
根据您的需要,请参阅 msdn 以获取输出格式。
http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx
http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx