导出到EXCEL时,create_date / modify_date不准确

时间:2017-03-13 15:26:51

标签: sql-server excel

我运行查询以获取所有数据库和用户的列表。查询运行正常。

选择all并将查询结果复制并粘贴到Excel后,我正确显示了多个列。然后,只有那些不正确的是create_datemodify_date。年份月份和日期不会出现。见下图:

https://i.stack.imgur.com/t9nxn.jpg

可以做些什么? 感谢。

查询:

DECLARE @DB_USers TABLE
(DBName sysname, UserName sysname, LoginType sysname, AssociatedRole varchar(max),create_date datetime,modify_date datetime)

INSERT @DB_USers
EXEC sp_MSforeachdb

'
use [?]
SELECT ''?'' AS DB_Name,
case prin.name when ''dbo'' then prin.name + '' (''+ (select SUSER_SNAME(owner_sid) from master.sys.databases where name =''?'') + '')'' else prin.name end AS UserName,
prin.type_desc AS LoginType,
isnull(USER_NAME(mem.role_principal_id),'''') AS AssociatedRole ,create_date,modify_date
FROM sys.database_principals prin
LEFT OUTER JOIN sys.database_role_members mem ON prin.principal_id=mem.member_principal_id
WHERE prin.sid IS NOT NULL and prin.sid NOT IN (0x00) and
prin.is_fixed_role <> 1 AND prin.name NOT LIKE ''##%'''

SELECT 
dbname,username ,logintype ,create_date ,modify_date ,
 STUFF(
(
 SELECT ',' + CONVERT(VARCHAR(500),associatedrole)
 FROM @DB_USers user2
 WHERE
 user1.DBName=user2.DBName AND user1.UserName=user2.UserName
 FOR XML PATH('')
 )
 ,1,1,'') AS Permissions_user
 FROM @DB_USers user1
 GROUP BY
 dbname,username ,logintype ,create_date ,modify_date
 ORDER BY DBName,username

1 个答案:

答案 0 :(得分:2)

格式化excel中的单元格以显示要显示日期和时间的单元格的日期和时间。

使用日期格式: enter image description here

使用自定义格式: enter image description here