我試圖在我的查詢中將DATETIME
字段轉換爲dd-mm-yyyy
格式。日期超出範圍但僅在存儲過程中調用
當我在SSMS中運行以下任一行時,查詢將成功執行,並以正確的格式獲取我的日期。
CONVERT(VARCHAR(30), CONVERT(DATETIME, f.Created, 101), 103) as [Created]
CONVERT(VARCHAR, f.created, 105) as [Created]
f.Created
是一個DATETIME列
但是,如果我嘗試運行它作爲一個存儲過程中的查詢的一部分,我得到一個錯誤:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
在存儲過程,我試過將語言設置爲British
,日期格式爲dmy
,但我仍然遇到超出範圍的錯誤。如果我刪除這些行或通常只選擇f.Created
字段,它就可以工作。
什麼問題?
編輯:
查詢以運行SP
DECLARE @html nvarchar(MAX);
EXEC spQueryToHtmlTable @html = @html OUTPUT, @query = N'
SELECT top 100 * from
(
select
c.clno + ''.'' + f.fileno as [Number]
,c.clName as [Client Name]
,f.fileDesc as [File name]
,CONVERT(VARCHAR(255), f.created, 105) as Created_ddmmyyyy
--Or either of these:
--CONVERT(VARCHAR(30), CONVERT(DATETIME, f.Created, 101), 103) as [Created]
--CONVERT(VARCHAR, f.created, 105) as [Created]
from config.dbfile f
join config.dbclient c on c.clid = f.clid
) x
where x.Department = ''Import'' and Type = ''Import''
and
x.Created_ddmmyyyy Between DATEADD(m, -2, GETDATE()) and GETDATE()
', @orderBy = '';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Outlook',
@recipients = '[email protected];',
@subject = 'subject of email',
@body = @html,
@body_format = 'HTML',
@query_no_truncate = 1,
@attach_query_result_as_file = 0;
SP:
/****** Object: StoredProcedure [dbo].[spQueryToHtmlTable] Script Date: 10/16/2017 11:47:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Description: Turns a query into a formatted HTML table. Useful for emails.
-- Any ORDER BY clause needs to be passed in the separate ORDER BY parameter.
-- =============================================
CREATE PROC [dbo].[spQueryToHtmlTable]
(
@query nvarchar(MAX), --A query to turn into HTML format. It should not include an ORDER BY clause.
@orderBy nvarchar(MAX) = NULL, --An optional ORDER BY clause. It should contain the words 'ORDER BY'.
@html nvarchar(MAX) = NULL OUTPUT --The HTML output of the procedure.
)
AS
BEGIN
SET NOCOUNT ON;
IF @orderBy IS NULL BEGIN
SET @orderBy = ''
END
SET @orderBy = REPLACE(@orderBy, '''', '''''');
DECLARE @realQuery nvarchar(MAX) = '
DECLARE @headerRow nvarchar(MAX);
DECLARE @cols nvarchar(MAX);
SELECT * INTO #dynSql FROM (' + @query + ') sub;
SELECT @cols = COALESCE(@cols + '', '''''''', '', '''') + ''['' + name + ''] AS ''''td''''''
FROM tempdb.sys.columns
WHERE object_id = object_id(''tempdb..#dynSql'')
ORDER BY column_id;
SET @cols = ''SET @html = CAST((SELECT '' + @cols + '' FROM #dynSql ' + @orderBy + ' FOR XML PATH(''''tr''''), ELEMENTS XSINIL) AS nvarchar(max))''
EXEC sys.sp_executesql @cols, N''@html nvarchar(MAX) OUTPUT'', @[email protected] OUTPUT
SELECT @headerRow = COALESCE(@headerRow + '''', '''') + ''<th>'' + name + ''</th>''
FROM tempdb.sys.columns
WHERE object_id = object_id(''tempdb..#dynSql'')
ORDER BY column_id;
SET @headerRow = ''<tr>'' + @headerRow + ''</tr>'';
SET @html = ''<table border="1">'' + @headerRow + @html + ''</table>'';
';
EXEC sys.sp_executesql @realQuery, N'@html nvarchar(MAX) OUTPUT', @[email protected] OUTPUT
END
GO
典型數據在f.created柱
2002-11-05 00:00:00.000
2003-12-15 00:00:00.000
2002-11-05 00:00:00.000
2002-11-05 00:00:00.000
2002-11-06 00:00:00.000
爲F預期結果。創建了列
05-11-2002
15-12-2003
05-11-2002
05-11-2002
06-11-2002
我將很高興與DD-MM-YY/YYYY的任何變化
是什麼類型'f.created'?你爲什麼要把它轉換兩次?你能提供一些樣本數據和預期結果嗎? –
DATETIME。我已更新我的帖子。 –