Monday, 2 March 2009

SQL Inline statements

Thought I'd post a quicky on SQL inline statements. Before I would create a variable and then assign a value depending on a CASE statement. But after a brief try I solved a problem with dates using an inline case statement. Converting the minute datepart to a string means that 5 past the hour becomes just 5 and doesn't look write in date strings. So i wanted any single numbers to be prefixed with zero. Here it is:

CONVERT(nVARCHAR,CreatedDate,103) + ' @ ' + CONVERT(nVARCHAR,DATEPART(Hh, CreatedDate)) + ':' + CASE WHEN LEN(CONVERT(nVARCHAR,DATEPART(Mi, CreatedDate))) = 1 THEN '0' + CONVERT(nVARCHAR,DATEPART(Mi, CreatedDate)) ELSE CONVERT(nVARCHAR,DATEPART(Mi, CreatedDate)) END AS CreatedDate

No comments:

Post a Comment