sql-server - 使用 SQL Server 打印一个句子说一年中的哪一天
问题描述
我正在尝试使用 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.')
解决方案
另一种方法是:
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.'
推荐阅读
- c# - 以编程方式访问 dotnet 核心项目中的 HTTP 端点
- serialization - 为什么kafka希望数据在主题中是二进制的(字节数组)?
- haskell - 为什么函数是 Haskell 中的第一个参数
- c - 如何创建矩阵的静态数组
- here-api - 为什么我会收到“签名不匹配。授权签名或事物凭证错误”?
- python - 尽管python中的输入数量很多,如何处理多个键盘输入而没有错误?
- jupyter-notebook - 有没有办法启动一个类似服务器的笔记本?
- python - 如何在groupby之后创建变量
- django - 如何在多对多关系中获得独特的孩子?
- php - 如何终止旧请求并在 php 中发起新的 API 调用?