开发者

How to combine two columns together to create a new column in a select statement?

开发者 https://www.devze.com 2023-04-02 12:56 出处:网络
Hope you can help. I have a Date column and a time column and i want to be able to combine these together within a select statement so there is just one column for [Date and Time]. Everything ive trie

Hope you can help. I have a Date column and a time column and i want to be able to combine these together within a select statement so there is just one column for [Date and Time]. Everything ive tried seem to add them together instead of combining/appending.

开发者_如何学JAVA

Cheers, :)


In SQL Server 2008 R2 you can use this(not in 2005):

DECLARE @TESTTBL TABLE ( dt DATE, tm TIME)
INSERT INTO @TESTTBL VALUES('2011-02-03', '01:02:03')
INSERT INTO @TESTTBL VALUES('2011-02-04', '02:03:04')

SELECT CAST(dt AS DATETIME) + CAST(tm AS DATETIME) FROM @TESTTBL

Result will be:

2011-02-03 01:02:03.000

2011-02-04 02:03:04.000

If you want the text's together, use varchar instead of datetime in the cast().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号