我在SQL请求中使用关键字UNION很困难, 我需要按SELECT,
显示的列对结果进行排序 DECLARE @search0 varchar(30);
SET @search0 = 'joll'
SELECT c.CdCnd AS n, FORMAT(c.ModifieLe, 'd', 'fr-FR') AS Modifié, c.Nom+' '+c.prenom AS 'Nom Complet', c.TelMobile AS 'Tel. Mob', c.SuiviPar AS 'Suivi par', a.LibAction AS 'à faire', c.Remuneration AS Rémunération, p.LibPrio AS Priorité, c.disponibilite AS Disponibilité, c.MotCleTech AS MCTech, c.MotCleFct AS MCFonc, c.MotCleEnt AS MCEnt, c.Details AS 'Détails', c.DateDispo AS DTDispo, c.Mobilite AS Mobilité
FROM Candidat c
LEFT JOIN TypAction a
ON c.CdAction = a.CdAction
LEFT JOIN TypPriorite p
ON c.CdPrio = p.CdPrio
WHERE ( ( Nom LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR prenom LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR MotCleTech LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR MotCleFct LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR MotCleEnt LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI ) )
UNION
SELECT c.CdCnd AS n, FORMAT(c.ModifieLe, 'd', 'fr-FR') AS Modifié, c.Nom+' '+c.prenom AS 'Nom Complet', c.TelMobile AS 'Tel. Mob', c.SuiviPar AS 'Suivi par', a.LibAction AS 'à faire', c.Remuneration AS Rémunération, p.LibPrio AS Priorité, c.disponibilite AS Disponibilité, c.MotCleTech AS MCTech, c.MotCleFct AS MCFonc, c.MotCleEnt AS MCEnt, c.Details AS 'Détails', c.DateDispo AS DTDispo, c.Mobilite AS Mobilité
FROM Candidat c
LEFT JOIN TypAction a
ON c.CdAction = a.CdAction
LEFT JOIN TypPriorite p
ON c.CdPrio = p.CdPrio
WHERE ( 1 = 2 OR c.CdCnd = '3' OR c.CdCnd = '48' OR c.CdCnd = '16' )
ORDER BY Modifié DESC
在这里,我需要使用列c.ModifieLe对DateTime的所有结果进行排序, 但是,由于UNION限制,我可以使用此列而不显示它,我需要在'fr-FR'格式化我的日期,
所以,我使用“Modifié”对我的结果进行排序,但这类似于varchar类型......
我需要在法语文化中格式化我的日期时间并保持日期类型。
感谢您的帮助。
答案 0 :(得分:2)
你的ORDER BY应该是
ORDER BY CONVERT(DATETIME,Modifié,103) DESC
答案 1 :(得分:2)
首先应用UNION,然后对它们进行排序:
;WITH T AS
(
SELECT
c.CdCnd AS n,
c.ModifieLe,
c.Nom+' '+c.prenom AS 'Nom Complet',
c.TelMobile AS 'Tel. Mob',
c.SuiviPar AS 'Suivi par',
a.LibAction AS 'à faire',
c.Remuneration AS Rémunération,
p.LibPrio AS Priorité,
c.disponibilite AS Disponibilité,
c.MotCleTech AS MCTech,
c.MotCleFct AS MCFonc,
c.MotCleEnt AS MCEnt,
c.Details AS 'Détails',
c.DateDispo AS DTDispo,
c.Mobilite AS Mobilité
FROM Candidat c
LEFT JOIN TypAction a
ON c.CdAction = a.CdAction
LEFT JOIN TypPriorite p
ON c.CdPrio = p.CdPrio
WHERE ( ( Nom LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR prenom LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR MotCleTech LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR MotCleFct LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR MotCleEnt LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI ) )
UNION
SELECT
c.CdCnd AS n,
c.ModifieLe,
c.Nom+' '+c.prenom AS 'Nom Complet',
c.TelMobile AS 'Tel. Mob',
c.SuiviPar AS 'Suivi par',
a.LibAction AS 'à faire',
c.Remuneration AS Rémunération,
p.LibPrio AS Priorité,
c.disponibilite AS Disponibilité,
c.MotCleTech AS MCTech,
c.MotCleFct AS MCFonc,
c.MotCleEnt AS MCEnt,
c.Details AS 'Détails',
c.DateDispo AS DTDispo,
c.Mobilite AS Mobilité
FROM Candidat c
LEFT JOIN TypAction a
ON c.CdAction = a.CdAction
LEFT JOIN TypPriorite p
ON c.CdPrio = p.CdPrio
WHERE ( 1 = 2 OR c.CdCnd = '3' OR c.CdCnd = '48' OR c.CdCnd = '16' )
)
SELECT
[n],
FORMAT([ModifieLe], 'd', 'fr-FR') AS Modifié,
[Nom Complet],
[Tel. Mob],
[Suivi par],
[à faire],
[Rémunération],
[Priorité],
[Disponibilité],
[MCTech],
[MCFonc],
[MCEnt],
[Détails],
[DTDispo],
[Mobilité]
FROM T ORDER BY CAST(ModifieLe AS DATETIME) DESC
答案 2 :(得分:0)
您可以尝试
select * from(
SELECT c.CdCnd AS n, FORMAT(c.ModifieLe, 'd', 'fr-FR') AS Modifié, c.Nom+' '+c.prenom AS 'Nom Complet', c.TelMobile AS 'Tel. Mob', c.SuiviPar AS 'Suivi par', a.LibAction AS 'à faire', c.Remuneration AS Rémunération, p.LibPrio AS Priorité, c.disponibilite AS Disponibilité, c.MotCleTech AS MCTech, c.MotCleFct AS MCFonc, c.MotCleEnt AS MCEnt, c.Details AS 'Détails', c.DateDispo AS DTDispo, c.Mobilite AS Mobilité
FROM Candidat c
LEFT JOIN TypAction a
ON c.CdAction = a.CdAction
LEFT JOIN TypPriorite p
ON c.CdPrio = p.CdPrio
WHERE ( ( Nom LIKE '%%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR prenom LIKE '%%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR MotCleTech LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR MotCleFct LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI OR MotCleEnt LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI ) )
UNION
SELECT c.CdCnd AS n, FORMAT(c.ModifieLe, 'd', 'fr-FR') AS Modifié, c.Nom+' '+c.prenom AS 'Nom Complet', c.TelMobile AS 'Tel. Mob', c.SuiviPar AS 'Suivi par', a.LibAction AS 'à faire', c.Remuneration AS Rémunération, p.LibPrio AS Priorité, c.disponibilite AS Disponibilité, c.MotCleTech AS MCTech, c.MotCleFct AS MCFonc, c.MotCleEnt AS MCEnt, c.Details AS 'Détails', c.DateDispo AS DTDispo, c.Mobilite AS Mobilité
FROM Candidat c
LEFT JOIN TypAction a
ON c.CdAction = a.CdAction
LEFT JOIN TypPriorite p
ON c.CdPrio = p.CdPrio
WHERE ( 1 = 2 OR c.CdCnd = '3' OR c.CdCnd = '48' OR c.CdCnd = '16' )
)
ORDER BY Modifié DESC
答案 3 :(得分:0)
您可以像这样设置{SET DATEFORMAT { format_column | varchar_column }}
对于排序,您可以在where条件中使用having子句。您可以在First Query中的where子句中使用OR条件而不是UNION
。