J'essaie de convertir un champ DATETIME
au format dd-mm-yyyy
dans ma requête.Date hors de portée, mais seulement dans la procédure stockée appelez
Lorsque j'exécute l'une des lignes suivantes dans SSMS, la requête s'exécute correctement et ma date est au format correct.
CONVERT(VARCHAR(30), CONVERT(DATETIME, f.Created, 101), 103) as [Created]
CONVERT(VARCHAR, f.created, 105) as [Created]
f.Created
est une colonne de DATETIME
Cependant, si je tente de l'exécuter dans le cadre d'une requête dans une procédure stockée je reçois une erreur:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Dans la procédure stockée , J'ai essayé de définir la langue à British
et aussi le format de la date à dmy
mais je reçois toujours une erreur hors plage. Si je supprime ces lignes ou sélectionne simplement le champ f.Created
normalement, cela fonctionne.
Qu'est-ce qui ne va pas?
Edit:
requête pour exécuter 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
données typiques dans la colonne 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
résultat escompté pour le f. Colonne créée
05-11-2002
15-12-2003
05-11-2002
05-11-2002
06-11-2002
Je serais heureux avec une variation de/jj-mm-aa aaaa
Quel type est 'f.created'? Pourquoi convertissez-vous deux fois? Pouvez-vous fournir des exemples de données et les résultats attendus? –
DATETIME. J'ai mis à jour mon message. –