【问题标题】:Print a sentence saying the day of the year using SQL Server使用 SQL Server 打印一个句子说一年中的哪一天
【发布时间】:2019-07-04 21:15:00
【问题描述】:

我正在尝试使用 SQL SERVER 编写此代码:

“嗨!今天是……这是一年中的……天数。除夕在……天”。

我的代码运行没有问题,但我也无法打印。我究竟做错了什么?我还没有完成整个短语,因为我需要在进入最后一部分之前解决问题。

DECLARE
    @currentDate DateTime
        SET @currentDate = GETDATE();

DECLARE @dayofyear datetime
SET @dayofyear=DATEDIFF(day,STR(YEAR(@dayofyear),4)+'0101',@dayofyear)+1
-- SELECT Numbertoday = DATEDIFF(day,STR(YEAR(@dayofyear),4)+'0101',@dayofyear)+1

print('Hi! Today is '+ CONVERT(VARCHAR(10), @currentDate , 111) + '. ' + 'This is the day number '+ ' ' +  CONVERT (VARCHAR(10), @dayofyear) + of the year.')   

【问题讨论】:

  • 哪个数据库? SQL server 从外观上看呢?
  • 是的,SQL Server。

标签: sql-server tsql date


【解决方案1】:

另一种方法是:

    declare @today varchar(11) = convert(varchar(11), getdate(), 1);

    declare @dayOfTheYear int = datepart(DAYOFYEAR, getdate());

    declare @untilNewYearsEve int = datepart(dayofyear, datefromparts(year(getdate()), 12, 31)) - @dayOfTheYear

   -- if you use print, you should see the result under the 'messages' tab not in the 'results' tab in SSMS
    print 'Hi! Today is '+ @today + '. ' + 'This is the day number '+ cast( @dayOfTheYear as varchar(3)) +  '. New year''s eve is in '+ 
    cast (@untilNewYearsEve as varchar(3)) + ' days.'

【讨论】:

    【解决方案2】:
    This should work perfectly fine. 
    while seeting the day of year you need to put @current date so it will work because you are trying to get a day from a @currentdate
    
        DECLARE
            @currentDate DateTime
                SET @currentDate = GETDATE();
    
        DECLARE @dayofyear datetime
        SET @dayofyear=DATEDIFF(day,STR(YEAR(@currentDate),4)+'0101',@currentDate)+1
    
    
        print('Hi! Today is '+ CONVERT(VARCHAR(10), @currentDate , 111) + '. ' + 'This is the day number '+ ' ' +  CONVERT (VARCHAR(10), @dayofyear) + 'of the year.')  
    

    【讨论】:

      【解决方案3】:

      只是为了好玩,你可以使用concat()

      示例

      Print concat('Hi! Today is '
                 ,format(GetDate(),'dddd, MMM d, yyyy.')
                 ,'  This is the day number '
                 ,DateDiff(DAY,datename(YEAR,getdate())+'0101',getdate()) + 1
                 ,' of the year.'
                 )   
      

      结果

      Hi! Today is Thursday, Jul 4, 2019.  This is the day number 185 of the year.
      

      【讨论】:

        【解决方案4】:

        如果使用 SQL 2008 及更高版本,您可以使用 DATEPART 函数中的 DAYOFYEAR 参数稍微简化一些事情。而且你真的不需要为日期声明一个变量。

        示例代码:

        print('Hi! Today is '+ convert(varchar(10), getdate() , 111) + '. ' +  'This is the day number' + ' ' + convert(varchar, datepart(dayofyear, getdate()))) + ' of the year.'
        

        结果:

        Hi! Today is 2019/07/05. This is the day number 186 of the year.
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多