문제

I need to get the exact date time to SSRS, I noticed when I use a date field the milliseconds get cut off in SSRS so I was thinking about formatting a string. I could not find a convert option that match the output I needed

mm/dd/yyyy hh:mm:ss.nnn AM/PM

So I came up with this long formula

-- Test Data
DECLARE @DATETIMESTAMP DATETIME 
SET @DATETIMESTAMP = GETDATE()
------------
SELECT DISTINCT 
    RIGHT('0'+CONVERT(VARCHAR(2), MONTH(@DateTimeStamp)), 2) + '/' +
    RIGHT('0'+CONVERT(VARCHAR(2), DAY(@DateTimeStamp)), 2) + '/' +
    CONVERT(VARCHAR(4), YEAR(@DateTimeStamp)) + ' ' +
    CASE 
        WHEN DATEPART(HOUR, @DateTimeStamp) > 12 THEN RIGHT('0'+CONVERT(VARCHAR(2), DATEPART(HOUR, @DateTimeStamp)-12), 2)
        WHEN DATEPART(HOUR, @DateTimeStamp) = 0 THEN '12'
        ELSE RIGHT('0'+CONVERT(VARCHAR(2), DATEPART(HOUR, @DateTimeStamp)), 2)
    END + ':' +
    RIGHT('0'+CONVERT(VARCHAR(4), DATEPART(MINUTE, @DateTimeStamp)), 2) + ':' +
    RIGHT('0'+CONVERT(VARCHAR(4), DATEPART(SECOND, @DateTimeStamp)), 2) + '.' +
    RIGHT('000'+CONVERT(VARCHAR(4), DATEPART(MILLISECOND, @DateTimeStamp)), 3) + ' ' +
    CASE 
        WHEN DATEPART(HOUR, @DateTimeStamp) > 11 THEN 'PM'
        ELSE 'AM'
    END 
    ,@DateTimeStamp

Can anyone suggest a better way?

Here is a picture of the report: This is using my code enter image description here

If I just pass back @DATETIMESTAMP I would get just 11/20/2013 02:23:19 PM

Then when running the query no records would match because 11/20/2013 02:23:19.000 PM to 11/20/2013 02:23:19.083 PM

도움이 되었습니까?

해결책

Geez, that's a lot of busy work that should probably be handled by SSRS. You can do it in T-SQL, but there is a lot of overhead in formatting strings there instead of just returning the native data. Here is one way that yields 11/25/2013 3:03:12.567PM:

SELECT CONVERT(CHAR(11), GETDATE(), 101)
  + LTRIM(RIGHT(CONVERT(CHAR(26), GETDATE(), 9), 14));

If you really need the space between the milliseconds and AM/PM, then you can get there with a little more work:

SELECT CONVERT(CHAR(11), GETDATE(), 101)
  + LTRIM(STUFF(RIGHT(CONVERT(CHAR(26), GETDATE(), 9), 14),13,0,' '));

And if you really need the leading 0, it gets even more fun:

SELECT CONVERT(CHAR(11), GETDATE(), 101)
  + RIGHT('0' + LTRIM(STUFF(RIGHT(CONVERT(CHAR(26), GETDATE(), 9), 14),13,0,' ')),15);

In SQL Server 2012, you'll be able to say:

SELECT FORMAT(GETDATE(), 'MM/dd/yyyy hh:mm:ss.fff tt');

But again, these methods of formatting strings in SQL Server force a lot of extra CPU overhead on the database that is much better handled by the presentation layer - which has to treat every row independently anyway. SQL Server only has to treat a set as a bunch of independent rows when you force it to, by applying formatting and other things, and this can slow the query down significantly.

Never mind that mm/dd/yyyy is very irresponsible output. Are you sure your entire audience will always understand that 06/09/2013 is June 9th and not September 6th?

다른 팁

Use the following format in SSRS:

MM/dd/yyyy hh:mm:ss.fff tt

This returns:

11/25/2013 11:41:51.067 AM

Keep the data type as DATETIME in your source query. You don't need to convert the output to string. Use the application layer, in this case SSRS, to control the format.

DECLARE @Date DATETIME;
SET  @Date  = GETDATE()

SELECT CONVERT(VARCHAR(10), @Date, 101) + ' '
            +  RIGHT(CONVERT(VARCHAR(26), @Date, 109), 14)

Result

11/25/2013 7:44:23:330PM
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top