Home » excel » sql server – create_date / modify_date not accurate when export to EXCEL

sql server – create_date / modify_date not accurate when export to EXCEL

Posted by: admin April 23, 2020 Leave a comment

Questions:

I run a query for getting a list of all DBs and Users. The query runs fine.

After selecting all and copying and pasting the query results into Excel, I have multiple columns properly displayed. Then only that are not correct are create_date and modify_date. The year month and date do not appear. See the image below:

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

What could be done?
Thanks.

The query:

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
How to&Answers:

Format the cells in excel to show date and time for the cells that you want to show date and time.

Using a date format:
enter image description here

Using a custom format:
enter image description here

<a href="https://exceptionshub.com/category/sql-server”>More Answers>