如何组合两个数据列?

时间:2012-09-17 22:19:07

标签: sql sql-server-2008-r2

我有这个SQL语句我希望将'date'和'dayOfWeek'列结合起来

 SELECT CONVERT(date,startTimeStamp) AS DATE
      , datename(dw,CONVERT(date,startTimeStamp)) as dayOfWeek
      ,CONVERT(time,[startTimeStamp])AS StartTime
      ,CONVERT(time,[endTimeStamp])AS EndTime
      ,DATEDIFF(HH,[startTimeStamp] ,[endTimeStamp])
  FROM [TaskManagementSystem_DB].[dbo].[Timesheet_entry]

1 个答案:

答案 0 :(得分:2)

听起来你想要这个:

SELECT CONVERT(date,startTimeStamp) AS DATE
      , datename(dw,CONVERT(date,startTimeStamp)) as dayOfWeek
      ,cast(datename(dw,CONVERT(date, startTimeStamp)) as varchar(50)) -- add this
        + ' ' +                                                     -- add this
        CONVERT(varchar(50),CONVERT(date,startTimeStamp)) FullDate -- add this
      ,CONVERT(time,[startTimeStamp])AS StartTime
      ,CONVERT(time,[endTimeStamp])AS EndTime
      ,DATEDIFF(HH,[startTimeStamp] ,[endTimeStamp])
  FROM [TaskManagementSystem_DB].[dbo].[Timesheet_entry]

要将字段连接在一起,您必须cast()将它们设置为相同的数据类型,类似于:

select cast(datename(dw,CONVERT(date, getdate())) as varchar(50)) 
  + ' ' +
  CONVERT(varchar(50) ,getdate()) newdate

请参阅SQL Fiddle with Demo