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.
希望你能帮忙。我有一个Date列和一个时间列,我希望能够在select语句中将它们组合在一起,因此[Date and Time]只有一列。我尝试的所有东西似乎都将它们加在一起而不是组合/附加。
Cheers, :)
干杯,:)
1 个解决方案
#1
5
In SQL Server 2008 R2 you can use this(not in 2005):
在SQL Server 2008 R2中,您可以使用它(不是在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-03 01:02:03.000
2011-02-04 02:03:04.000
2011-02-04 02:03:04.000
If you want the text's together, use varchar instead of datetime in the cast().
如果您希望文本在一起,请在cast()中使用varchar而不是datetime。
#1
5
In SQL Server 2008 R2 you can use this(not in 2005):
在SQL Server 2008 R2中,您可以使用它(不是在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-03 01:02:03.000
2011-02-04 02:03:04.000
2011-02-04 02:03:04.000
If you want the text's together, use varchar instead of datetime in the cast().
如果您希望文本在一起,请在cast()中使用varchar而不是datetime。