Converting DATE or DATETIME to INT (format YYYYMMDD) declare @dt datetime set @dt = '\09-22-2007 15:07:38.850\' select cast(convert(char(8),@dt,112) as int) Converting INT (format YYYYMMDD) to DATE declare @dti int = 20071022; select cast(@dti as datetime)
Example of date trimming to minutes and hours: declare @dt datetime set @dt = '09-22-2007 15:07:38.850' select dateadd(mi, datediff(mi, 0, @dt), 0) select dateadd(hour, datediff(hour, 0, @dt), 0)
How to add data to a table with a Primary Key (when you need to ignore this primary key) SET IDENTITY_INSERT tableA ON -- Now you can make a list of fields with Primary Key and add records like: INSERT Into tableA ([id], [c2], [c3], [c4], [c…
Example of rounding date to hour and minute declare @dt datetime set @dt = '09-22-2007 15:07:38.850' select dateadd(mi, datediff(mi, 0, @dt), 0) select dateadd(hour, datediff(hour, 0, @dt), 0)
Below is an example of correctly canceling a transaction with an error: update Params set Value=168 where Param = 'SessionHoursLimit' PRINT @@TRANCOUNT BEGIN TRY BEGIN TRAN PRINT @@TRANCOUNT update Params set Value=167 where Param = 'SessionHours…
The log cleanup process should be done regularly, and a professional administrator should be responsible for it. However, it may happen that on your small development project for testing, logs unexpectedly exceed the set size and the following problem arises:
Simple MERGE example for TSQL. The example assumes that we are working with two tables of the same structure (in real life, this condition is not necessary). The TargetTable and SourceTable tables consist of only two fields: Id - some identifier. Name - object name.
By default, the length of a string in MSSQL Manager queries is limited, and when the string size is large enough, only its part will be retrieved in the query, but there is a way to bypass this issue. To select a long string, it is enough to simply assign the name to the field with the name [XML_F52E2B61-18A1-11d1-B105-00805F49916B].
To select fields and all parameters by field from a table, you can use the SP_COLUMNS procedure. Example: exec sp_columns [Table Name]
This query returns a set of table fields with all characteristics. The method is also applicable and works great with views VIEW. SELECT ORDINAL_POSITION ,COLUMN_NAME ,DATA_TYPE ,CHARACTER_MAXIMUM_LENGTH ,IS_NULLABLE ,COLUMN_DEFAULT…