使用日期函数

时间:2017-04-26 14:15:25

标签: sql-server database

我有一个这样的SQL存储过程查询结果(见下文)

EmpID   FirstName Dept        JoinDate    Code1     Code2   Code3   Code4
 01     aaa     service       2017-02-11      0        2        3       5
 02     bbb     customerCare  2010-01-23      1        4        7       9
 03     ccc     Receptionist  2009-12-20      2        5        1       8

所有这些字段都是通过加入条件从不同的表中获取的。(我创建了一个选择存储过程来执行每个月的第一个工作日)

我的要求是,我想将所有代码字段替换为开始日期。例如,

如果我在2017年3月1日执行此存储过程,结果应如下所示

EmpID   FirstName Dept       JoinDate 2016-12-01 2017-01-01 2017-02-01 2017-03-01                   
 01     aaa       service       2017-02-11  0       2       3       5
 02     bbb       customerCare  2010-01-23  1       4       7       9
 03     ccc       Receptionist  2009-12-20  2       5       1       8

例2: 如果我在2017年4月1日执行此存储过程,结果应该如下所示

*

EmpID   FirstName Dept JoinDate      2017-01-01 2017-02-01 2017-03-01 2017-04-01
    01  aaa     service 2017-02-11          0       2         3         5
    02  bbb     customerCare    2010-01-23  1       4         7         9
    03  ccc     Receptionist    2009-12-20  2       5         1         8

*

那个me​​nas,

  • code4字段别名应为当月的第一天

  • code3字段别名应该是当前月份的第一天-1

  • code2字段别名应该是当前月份的第一天-2

  • code1字段别名应该是当前月份的第一天-3

我该怎么做?我正在使用SQL Server 2014。 我无法找到如何在sql server中连接多个表时使用日期对字段名称进行别名。 请帮我。感谢您花时间阅读我的问题并非常感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

将类似以下内容放入存储过程中。这将获得当前日期,查找本月的第一天和前3个月。只是在你自己的表中。您必须将其余查询放入Dynamic SQL @query字符串以及非代码列。如果您需要任何帮助,请告诉我。

DECLARE @code4 date; SET @code4 = CAST(DATEADD(DAY,(DATEPART(DAY, GETDATE()-1)) * -1, GETDATE()) AS date)
DECLARE @code3 date; SET @code3 = DATEADD(MM, -1, @code4)
DECLARE @code2 date; SET @code2 = DATEADD(MM, -2, @code4)
DECLARE @code1 date; SET @code1 = DATEADD(MM, -3, @code4)

DECLARE @query NVARCHAR(MAX);
DECLARE @selects NVARCHAR(MAX);
set @selects = 'code1 AS [' + CAST(@code1 as nvarchar) + '], code2 AS [' + CAST(@code2 as nvarchar) + '], code3 AS [' + CAST(@code3 as nvarchar) + '], code4 AS [' + CAST(@code4 as nvarchar) + ']'
set @query = 'SELECT ' + @selects + ' FROM [dbo].[myTable]'

EXEC(@query)

答案 1 :(得分:1)

IF Object_ID('tempdb..#temp') IS NOT NULL
Drop table #temp

;WITH cte(EmpID,FirstName, Dept,JoinDate,Code1, Code2,Code3,Code4)
AS
(
SELECT  01,  'aaa',  'service'       ,'2017-02-11',0,  2,  3, 5  union all
SELECT  02,  'bbb',  'customerCare' , '2010-01-23',1,  4,  7, 9  union all
SELECT  03,  'ccc',  'Receptionist' , '2009-12-20',2,  5,  1, 8
)
SELECT * INTO #temp FROM cte


DECLARE @Currentmonth DATE='2017-03-01',--Change date like 2017-04-01,2017-03-01
@AllDAtes VARCHAR(100),
@Lastmonth DATE,    
@LastBeforemonth DATE,
@LastBeforemonth2 DATE,
@query NVARCHAR(MAX),
@selects NVARCHAR(MAX);

DECLARE @Lastmonths TABLE (Currentmonth DATE,Lastmonth DATE,LastBeforemonth DATE,LastBeforemonth2 DATE)
INSERT INTO @Lastmonths

SELECT @Currentmonth AS Currentmonth,
    DATEADD(month, -1, DATEADD(day, 1 - day(@Currentmonth), @Currentmonth))AS Lastmonth,
    DATEADD(month, -2, DATEADD(day, 1 - day(@Currentmonth), @Currentmonth))AS LastBeforemonth,
    DATEADD(month, -3, DATEADD(day, 1 - day(@Currentmonth), @Currentmonth))AS LastBeforemonth2

SET @Currentmonth=@Currentmonth
SELECT @Lastmonth= Lastmonth ,@LastBeforemonth= LastBeforemonth,@LastBeforemonth2=LastBeforemonth2 FROM @Lastmonths

SET @selects = 'code1 AS [' + CAST(@LastBeforemonth2 as nvarchar) + '], code2 AS [' + CAST(@LastBeforemonth as nvarchar) + '], code3 AS [' + CAST(@Lastmonth as nvarchar) + '], code4 AS [' + CAST(@Currentmonth as nvarchar) + ']'

SET @query = 'SELECT EmpID,FirstName, Dept,JoinDate , ' + @selects + ' FROM #temp'

EXEC (@query)