Advertisement
Guest User

Testing Format vs Convert

a guest
Mar 24th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.88 KB | None | 0 0
  1.  
  2. SELECT TOP (1000) d = DATEADD(DAY, CHECKSUM(NEWID())%1000, o.create_date)
  3.   INTO dbo.dtTest
  4.   FROM sys.all_objects AS o
  5.   ORDER BY NEWID();
  6. GO
  7. CREATE CLUSTERED INDEX d ON dbo.dtTest(d);
  8.  
  9. -- prime the cache
  10. SELECT d,
  11.   convert(char(10),d,105)+' '+convert(char(5),d,108),
  12.   format(d,'dd-mm-yyyy hh:mm')
  13. FROM dbo.dtTest;
  14. GO
  15.  
  16. -- select all 1,000 rows
  17. GO
  18. SELECT d FROM dbo.dtTest ORDER BY d;
  19. GO 5
  20. SELECT d = convert(char(10),d,105)+' '+convert(char(5),d,108) FROM dbo.dtTest ORDER BY d;
  21. GO 5
  22. SELECT d = format(d,'dd-mm-yyyy hh:mm') FROM dbo.dtTest ORDER BY d;
  23. GO 5
  24.  
  25. -- select top 1
  26. GO
  27. SELECT TOP (1) d FROM dbo.dtTest ORDER BY d;
  28. GO 5
  29. SELECT TOP (1) convert(char(10),d,105)+' '+convert(char(5),d,108) FROM dbo.dtTest ORDER BY d;
  30. GO 5
  31. SELECT TOP (1) format(d,'dd-mm-yyyy hh:mm') FROM dbo.dtTest ORDER BY d;
  32. GO 5
  33.  
  34. -- force scan but leave SSMS mostly out of it
  35. GO
  36. DECLARE @d DATE;
  37. SELECT @d = d FROM dbo.dtTest ORDER BY d;
  38. GO 5
  39. DECLARE @d CHAR(16);
  40. SELECT @d = convert(char(10),d,105)+' '+convert(char(5),d,108) FROM dbo.dtTest ORDER BY d;
  41. GO 5
  42. DECLARE @d CHAR(16);
  43. SELECT @d = format(d,'dd-mm-yyyy hh:mm') FROM dbo.dtTest ORDER BY d;
  44. GO 5
  45.  
  46. SELECT
  47.    
  48.    case when t.text like '%format(%' then 'format' when t.text like '%convert(%' then 'convert' else '' end as func,
  49.   s.execution_count,
  50.   s.total_elapsed_time,
  51.   avg_elapsed_time = CONVERT(DECIMAL(12,2),s.total_elapsed_time / 5.0),
  52.   s.total_worker_time,
  53.   avg_worker_time = CONVERT(DECIMAL(12,2),s.total_worker_time / 5.0),
  54.   s.total_clr_time
  55.   , [t] = replace(CONVERT(varchar(255), t.[text]),char(13)+char(10),'')
  56.  
  57. FROM sys.dm_exec_query_stats AS s
  58. CROSS APPLY sys.dm_exec_sql_text(s.[sql_handle]) AS t
  59. WHERE 1=1
  60.   AND t.[text] LIKE N'%dbo.dtTest%'
  61.   and s.execution_count > 1
  62.   and (t.text like '%format(%'
  63.   or  t.text like '%convert(%')
  64. ORDER BY s.last_execution_time;
  65. GO
  66.  
  67. drop table dbo.dtTest;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement