MSSQL Notes
- January 21st, 2020
- Posted in Documentation
- Write comment
To determine a view definition:
SELECT DEFINITION FROM sys.sql_modules WHERE object_id = OBJECT_ID(‘viewschema.viewname‘)
To list all the tables in a database:
SELECT * FROM INFORMATION_SCHEMA.TABLES
To list all the columns and tables in a database:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
OUTER JOIN example:
SELECT xx.column1, yy.column2
FROM table1 xx
LEFT OUTER JOIN DB2.schema1.table2 yy ON xx.column1 = yy.column2
Change the time format:
Some CONVERT statements to the time format from DATETIME to something else:
DECLARE @SOMETIME DATETIME
SET @SOMETIME = ‘2020-01-31 11:00:00 AM’
SELECT CONVERT(VARCHAR(20), @SOMETIME, 22)
01/31/20 11:00:00 AM
This one has the four digit year:
DECLARE @SOMETIME DATETIME
SET @SOMETIME = ‘2020-01-31 11:00:00 AM’
SELECT CONVERT(VARCHAR(20), DATEADD(Hour, -8, @RECEIPTDATE), 101) + ‘ ‘ + CONVERT(VARCHAR(20), DATEADD(Hour, -8, @RECEIPTDATE), 8)
01/31/2020 11:00:00
Selecting records based on time example:
This will returns records (in this case just a bunch of time variables), if the the @SOMEDATETIME variable is between now and one hour ago:
DECLARE @SOMEDATETIME DATETIME
DECLARE @CURRENTDATETIME DATETIME
DECLARE @CURRENTDATETIMEMINUS DATETIME
SET @SOMEDATETIME = ‘2020-01-31 15:30:00.000’
SET @CURRENTDATETIME = CONVERT(VARCHAR(20), GETDATE(), 101) + ‘ ‘ + CONVERT(VARCHAR(20), GETDATE(), 8)
SET @CURRENTDATETIMEMINUS = CONVERT(VARCHAR(20), DATEADD(Hour, -1, @CURRENTDATETIME), 101) + ‘ ‘ + CONVERT(VARCHAR(20), DATEADD(Hour, -1, @CURRENTDATETIME), 8)SELECT @SOMEDATETIME AS SOMEDATETIME
, @CURRENTDATETIMEMINUS AS CURRENTDATETIMEMINUS
, @CURRENTDATETIME AS CURRENTDATETIME
WHERE @SOMEDATETIME
BETWEEN @CURRENTDATETIMEMINUS AND @CURRENTDATETIME
No comments yet.