How to combine two columns together to create a new column in a select statement?
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.
开发者_如何学JAVACheers, :)
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().
精彩评论