Fresh spaghetti code
The Daily WTF just cracks me up sometimes . Todays code sniplet, Yet Another "Hex To Ascii", is a perfect example of how a simple task can be made complex with goto. It is difficult to be the worst though as this example shows: look at me, I know how to write loops.
One of the worst sql statements I have seen in a production environment does a simple date filter:
CREATE VIEW dbo.V_News_Details
AS
SELECT TOP 100 PERCENT dbo.Details.idDetail, dbo.Details.sTitle, dbo.Details.sText, dbo.Details.dValidFrom,
dbo.Details.dValidTo, dbo.Details.dNewsHour, dbo.Details.dValidHour, dbo.Details.idService,
dbo.Details.lPos, dbo.Details.idImage, b.Status
FROM dbo.Details LEFT OUTER JOIN
dbo.Images b ON b.Id = dbo.Details.idImage
WHERE (CONVERT(char(10), GETDATE(), 111) BETWEEN CONVERT(char(10), dbo.Details.dValidFrom, 111) AND CONVERT(char(10),
dbo.Details.dValidTo, 111)) AND (dbo.Details.dValidHour IS NULL) AND (dbo.Details.dNewsHour IS NULL) OR
(dbo.Details.dValidFrom IS NULL) AND (dbo.Details.dValidTo IS NULL) OR
(CONVERT(char(10), GETDATE(), 111) <= CONVERT(char(10), dbo.Details.dValidTo, 111)) AND (dbo.Details.dValidHour IS NULL)
AND (dbo.Details.dNewsHour IS NULL) AND (dbo.Details.dValidFrom IS NULL) OR
(CONVERT(char(10), GETDATE(), 111) >= CONVERT(char(10), dbo.Details.dValidFrom, 111)) AND (dbo.Details.dValidHour IS NULL)
AND (dbo.Details.dNewsHour IS NULL) AND (dbo.Details.dValidTo IS NULL) OR
(CONVERT(char(10), GETDATE(), 111) > CONVERT(char(10), dbo.Details.dValidFrom, 111)) AND (CONVERT(char(10), GETDATE(), 111)
< CONVERT(char(10), dbo.Details.dValidTo, 111)) OR
(CONVERT(char(10), GETDATE(), 111) > CONVERT(char(10), dbo.Details.dValidFrom, 111)) AND (dbo.Details.dValidTo IS NULL) OR
(CONVERT(char(10), GETDATE(), 111) < CONVERT(char(10), dbo.Details.dValidTo, 111)) AND (dbo.Details.dValidFrom IS NULL) OR
(dbo.Details.dValidHour IS NOT NULL) AND (dbo.Details.dNewsHour IS NOT NULL) AND
(dbo.Details.dValidFrom IS NOT NULL) AND (dbo.Details.dValidTo IS NOT NULL) AND (CONVERT(datetime, CONVERT(char(4),
DATEPART(yyyy, GETDATE())) + '/' + CONVERT(char(2), DATEPART(mm, GETDATE())) + '/' + CONVERT(char(2), DATEPART(dd, GETDATE()))
+ ' ' + CONVERT(char(2), DATEPART(hh, GETDATE())) + ':' + CONVERT(char(2), DATEPART(mi, GETDATE()))) BETWEEN CONVERT(datetime,
CONVERT(char(4), DATEPART(yyyy, dbo.Details.dValidFrom)) + '/' + CONVERT(char(2), DATEPART(mm, dbo.Details.dValidFrom))
+ '/' + CONVERT(char(2), DATEPART(dd, dbo.Details.dValidFrom)) + ' ' + CONVERT(char(2), DATEPART(hh, dbo.Details.dNewsHour))
+ ':' + CONVERT(char(2), DATEPART(mi, dbo.Details.dNewsHour))) AND CONVERT(datetime, CONVERT(char(4), DATEPART(yyyy,
dbo.Details.dValidTo)) + '/' + CONVERT(char(2), DATEPART(mm, dbo.Details.dValidTo)) + '/' + CONVERT(char(2), DATEPART(dd,
dbo.Details.dValidTo)) + ' ' + CONVERT(char(2), DATEPART(hh, dbo.Details.dValidHour)) + ':' + CONVERT(char(2), DATEPART(mi,
dbo.Details.dValidHour)))) OR
(dbo.Details.dValidHour IS NULL) AND (dbo.Details.dNewsHour IS NOT NULL) AND
(dbo.Details.dValidFrom IS NOT NULL) AND (dbo.Details.dValidTo IS NOT NULL) AND (CONVERT(datetime, CONVERT(char(4),
DATEPART(yyyy, GETDATE())) + '/' + CONVERT(char(2), DATEPART(mm, GETDATE())) + '/' + CONVERT(char(2), DATEPART(dd, GETDATE()))
+ ' ' + CONVERT(char(2), DATEPART(hh, GETDATE())) + ':' + CONVERT(char(2), DATEPART(mi, GETDATE()))) BETWEEN CONVERT(datetime,
CONVERT(char(4), DATEPART(yyyy, dbo.Details.dValidFrom)) + '/' + CONVERT(char(2), DATEPART(mm, dbo.Details.dValidFrom))
+ '/' + CONVERT(char(2), DATEPART(dd, dbo.Details.dValidFrom)) + ' ' + CONVERT(char(2), DATEPART(hh, dbo.Details.dNewsHour))
+ ':' + CONVERT(char(2), DATEPART(mi, dbo.Details.dNewsHour))) AND CONVERT(datetime, CONVERT(char(4), DATEPART(yyyy,
dbo.Details.dValidTo)) + '/' + CONVERT(char(2), DATEPART(mm, dbo.Details.dValidTo)) + '/' + CONVERT(char(2), DATEPART(dd,
dbo.Details.dValidTo)) + ' ' + '23:59')) OR
(dbo.Details.dValidHour IS NOT NULL) AND (dbo.Details.dNewsHour IS NULL) AND
(dbo.Details.dValidFrom IS NOT NULL) AND (dbo.Details.dValidTo IS NOT NULL) AND (CONVERT(datetime, CONVERT(char(4),
DATEPART(yyyy, GETDATE())) + '/' + CONVERT(char(2), DATEPART(mm, GETDATE())) + '/' + CONVERT(char(2), DATEPART(dd, GETDATE()))
+ ' ' + CONVERT(char(2), DATEPART(hh, GETDATE())) + ':' + CONVERT(char(2), DATEPART(mi, GETDATE()))) BETWEEN CONVERT(datetime,
CONVERT(char(4), DATEPART(yyyy, dbo.Details.dValidFrom)) + '/' + CONVERT(char(2), DATEPART(mm, dbo.Details.dValidFrom))
+ '/' + CONVERT(char(2), DATEPART(dd, dbo.Details.dValidFrom)) + ' ' + '00:00') AND CONVERT(datetime, CONVERT(char(4), DATEPART(yyyy,
dbo.Details.dValidTo)) + '/' + CONVERT(char(2), DATEPART(mm, dbo.Details.dValidTo)) + '/' + CONVERT(char(2), DATEPART(dd,
dbo.Details.dValidTo)) + ' ' + CONVERT(char(2), DATEPART(hh, dbo.Details.dValidHour)) + ':' + CONVERT(char(2), DATEPART(mi,
dbo.Details.dValidHour)))) OR
(dbo.Details.dValidHour IS NULL) AND (dbo.Details.dNewsHour IS NOT NULL) AND
(dbo.Details.dValidFrom IS NOT NULL) AND (dbo.Details.dValidTo IS NULL) AND (CONVERT(datetime, CONVERT(char(4),
DATEPART(yyyy, GETDATE())) + '/' + CONVERT(char(2), DATEPART(mm, GETDATE())) + '/' + CONVERT(char(2), DATEPART(dd, GETDATE()))
+ ' ' + CONVERT(char(2), DATEPART(hh, GETDATE())) + ':' + CONVERT(char(2), DATEPART(mi, GETDATE()))) BETWEEN CONVERT(datetime,
CONVERT(char(4), DATEPART(yyyy, dbo.Details.dValidFrom)) + '/' + CONVERT(char(2), DATEPART(mm, dbo.Details.dValidFrom))
+ '/' + CONVERT(char(2), DATEPART(dd, dbo.Details.dValidFrom)) + ' ' + CONVERT(char(2), DATEPART(hh, dbo.Details.dNewsHour))
+ ':' + CONVERT(char(2), DATEPART(mi, dbo.Details.dNewsHour))) AND '2100/12/31' + ' ' + '23:59') OR
(dbo.Details.dValidHour IS NOT NULL) AND (dbo.Details.dNewsHour IS NULL) AND (dbo.Details.dValidFrom IS NULL)
AND (dbo.Details.dValidTo IS NOT NULL) AND (CONVERT(datetime, CONVERT(char(4), DATEPART(yyyy, GETDATE())) + '/' + CONVERT(char(2),
DATEPART(mm, GETDATE())) + '/' + CONVERT(char(2), DATEPART(dd, GETDATE())) + ' ' + CONVERT(char(2), DATEPART(hh, GETDATE()))
+ ':' + CONVERT(char(2), DATEPART(mi, GETDATE()))) BETWEEN '1800/01/01' + ' 00:00' AND CONVERT(datetime, CONVERT(char(4), DATEPART(yyyy,
dbo.Details.dValidTo)) + '/' + CONVERT(char(2), DATEPART(mm, dbo.Details.dValidTo)) + '/' + CONVERT(char(2), DATEPART(dd,
dbo.Details.dValidTo)) + ' ' + CONVERT(char(2), DATEPART(hh, dbo.Details.dValidHour)) + ':' + CONVERT(char(2), DATEPART(mi,
dbo.Details.dValidHour))))
ORDER BY dbo.Details.lPos, dbo.Details.dValidFrom DESC, dbo.Details.dNewsHour DESC,
dbo.Details.idDetail DESC
The developers are so scared by the monster that the code is still used as the base view for most selects...
KISS people!