Sunday, October 28, 2018

SQL Server - Subtract/Add hours from date


SELECT DATEADD(HOUR,-1,DateTimesColumn)

or

select dateadd(HOUR, -8/2 ,'07/01/2018')

SQL Server - How to extract only Year/Hour/Min from the date

Try running following

select year('01/07/2018')
select year(@date)
select year(getdate())
select year('20180101')

----------------- or use datepart.



select datepart(HOUR,ColumnWithDateTime)
select datepart(YEAR,ColumnWithDateTime)
select datepart(MI,ColumnWithDateTime)



datepartAbbreviations
yearyyyyyy
quarterqqq
monthmmm
dayofyeardyy
dayddd
weekwkww
weekdaydw
hourhh
minutemi, n
secondsss
millisecondms
microsecondmcs
nanosecondns
TZoffsettz
ISO_WEEKisowkisoww

Sunday, October 14, 2018

SQL Sever - Create a Copy of Table



SELECT * INTO BizDev.CurrCustomers FROM Sales.Customers


You can also create the new table from a specific subset of columns in the original table. In this case, you specify the names of the columns to copy after the SELECT keyword. Any columns not specified are excluded from the new table. The following example copies specific columns to a new table:

SELECT CustName, Address, Telephone, Email INTO BizDev.CurrCustomers
FROM Sales.Customers

Recently Executed queries

 SELECT     txt.TEXT AS [SQL Statement],     qs.EXECUTION_COUNT [No. Times Executed],     qs.LAST_EXECUTION_TIME AS [Last Time Executed],   ...