mssql
converting datetimeoffset to datetime
To preserve time zone information, use CONVERT(datetime, @yourDateTimeOffset, 1). This particular usage of 1 in what is otherwise called the style parameter doesn’t seem to be documented for datetimeoffset > datetime, but does work.
sqldeclare @createdon datetimeoffset set @createdon = '2008-12-19 10:00:00 +10:00' select CONVERT(datetime, @createdon) --Output: 2008-12-19 10:00:00.000 select CONVERT(datetime, @createdon, 1) --Output: 2008-12-19 00:00:00.000
drop connections and databases
plaintextALTER DATABASE MyDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO DROP DATABASE MyDatabase GO
list databases & tables
List databases
sqlSELECT name, database_id, create_date FROM sys.databases;
List tables
sqlselect * from sysobjects where xtype='U'
login audit
Current audit level;
sqlexecute xp_loginconfig 'audit level';
Audit level. Possible values are none, success, failure, and all. Audits are written to the error log and to the Windows Event Viewer.
xp_loginconfig cannot be used to set configuration values. To set the login mode and audit level, use SQL Server Management Studio.
SSMS
Database instance > Right-click > Properties > Security
Seeing failed logins
sqlEXEC sp_readerrorlog 0, 1, 'Login failed'
where 0 is the log number - increment to keep going back in time.
Going back further
debug messages in sql profiler
In profiler enable event UserConfigurable:0, then:
plaintextEXEC sp_trace_generateevent 82, N'My message'
trailing spaces
Trailing spaces are a bit whacky in mssql, check this.