Beginning of the Minute and Second of Datetime

  

create table test (StartDatetime datetime);


insert into  test values ('2017-10-03 00:11:01.000'),('2017-10-03 00:20:02.333'),
('2017-10-03 00:33:03.333'),
('2017-10-03 00:44:04.667'),
('2017-10-03 00:55:00.333'),
('2017-10-03 00:06:01.697')
 

 select  
  dateadd(mi, datediff(mi, 0, CURRENT_TIMESTAMP), 0) beginningOfTheMinute_current,
 dateadd(mi, datediff(mi, 0, StartDatetime), 0) beginningOfTheMinute1 
 , dateadd(minute,datediff(minute,Cast(Cast(StartDatetime as date) as datetime), StartDatetime)
 ,Cast(Cast(StartDatetime as date) as datetime)) beginningofminute2
 
  , dateadd(second,datediff(second,Cast(Cast(StartDatetime as date) as datetime), StartDatetime)
 ,Cast(Cast(StartDatetime as date) as datetime)) beginningofsecond

  from test


drop table test 

--https://jingyangli.wordpress.com/?s=beginning



 


 

Check Internal Version of a .MDF File

When you have a detached .MDF file, you want to check the version number without modifying it. You can use a powershell script as below.
If you want to use the same script for a live database file, you need to take the database offline first.

  
  
 
 
 get-content -Encoding Byte "E:\MSSQL\testcs.mdf" | select-object -skip 0x12064 -first 2
84
3

PS H:> 3*256+64
852
## SQL Server 2016 SP1
 
 
 

 

http://rusanu.com/2011/04/04/how-to-determine-the-database-version-of-an-mdf-file/


Open Firewall Ports with Powershell

  
  
 
netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80

netsh advfirewall firewall add rule name="Open Port 443" dir=in action=allow protocol=TCP localport=443


netsh advfirewall firewall add rule name="Open Port 1433" dir=in action=allow protocol=TCP localport=1433
netsh advfirewall firewall add rule name="Open Port 1434" dir=in action=allow protocol=TCP localport=1434



##  or Open ports for Domain, Private  with PowerShell in ONE command:

New-NetFirewallRule -DisplayName 'HTTP Inbound' -Profile @('Domain', 'Private') -Direction Inbound -Action Allow -Protocol TCP -LocalPort @('80', '443')

 
 

 

https://blogs.msdn.microsoft.com/timomta/2016/11/04/how-do-i-open-ports-with-powershell/


System Administrator Was Locked Out from SQL Server Instance After Installation

Installed SQL Server Database Engine with a service account (not the admin user) and after installation finished, you cannot login to the installed instance with windows authentication.

How to allow the user (system administrator role) to access the instance?
Here are the steps:
When your start SQL Server in single-user mode with either -m or -f options, any member of computer’s local admins can then connect to the instance as a member of the sysadmin fixed server role.

Option 1: Powershell to run SQLcmd to add the login to sysadmin role (tested):

  
  
 # stop database service if it is running
net stop mssqlserver
#start database service as single user mode (for default instance mssqlserve)

net start mssqlserver /m

# in single user mode, window admin group member can access the instance
 
 & sqlcmd -S "myservername" -E -Q "EXEC sp_addsrvrolemember 'myDomain\myID', 'sysadmin';"
 
##After add the user, restart service without single user mode;
net stop mssqlserver
net start mssqlserver
 ## start agent service default instance
net start "SQL Server Agent (MSSQLSERVER)"
 ## to drop a login, not need here.
    & sqlcmd -S "myservername" -E -q "DROP LOGIN [myDomain\OtherID];"

 

Option 2 (tested)
2.1 From UI:
Open SQL Server Configuration Manager (run as admin if asked)>> Select SQL Server Services>>Click on your instance to open Properties window>>From Startup Parameters>> Specify a startup parameter
Add -m ( this is a dash and a lower case letter m). >>Click OK and Restart.
Make suer SQL Server Agent Service stopped.
Restart SSMS by Run as administrator and you will be in as a sysadmin now. Create a new login for your account and add your login as sysadmin role from SSMS.
2.2 Or using T-SQL script:

  

  CREATE LOGIN [yourDomain\yourUsername] FROM WINDOWS;
ALTER SERVER ROLE sysadmin ADD MEMBER [yourDomain\yourUsername];


 

Close SSMS and restart SQL Server Configuration Manager and remove the startup parameter -m.
Restart all services now.

Launch SSMS under your user name (windows authentication), you should be connected now.

https://technet.microsoft.com/en-us/library/dd207004%28v=sql.110%29.aspx?f=255&MSPPError=-2147217396


T-SQL to Convert CYYMMDD to Standard Date

  

 --For date between 1900 and 2099
--CYYMMDD
--7 digits
select   Cast(stuff('1170226',1,1,'20') as date) 
-- or
,  Cast(stuff('1170226',1,1
,Case When right('1170226',1)=1 then '20' When right('1170226',1)=0 then '19' End) 
as date) 


 

Binary to Bigint Sample

  

 Declare @bStr varchar(16)='09EEDF7C10C41030'


  
 Select Convert(BIGINT,0x09EEDF7C10C41030) --715755114748710960

 --Use CONVERT function with style 2 to data type BIGINT

Select  Convert(bigint, CONVERT(VARBINARY(8),  @bStr,2))  

--check
Select   CONVERT(BINARY(8), CONVERT(BIGINT, 715755114748710960))  
--0x09EEDF7C10C41030

--Below returns wrong value
Select  Cast('0x'+@bStr as binary(8))  ---wrong



 

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/ef5cd089-71c9-4ded-af69-8682d77595ed/hexadecimal-to-number-table-values-functionsp?forum=transactsql

http://stackoverflow.com/questions/33021992/i-want-to-get-hexadecimal-value-off-a-binary-value-removing-0x-from-it


Winscp Script to Rename a File to Append Timestamp

Based on a link from Stackoverflow by WinSCP author Martin Prikryl:
The put command accepts an optional second argument to specify a target name.
put localfile remotefile

http://stackoverflow.com/questions/41465186/batch-script-to-rename-file-with-timestamp-on-ftp-server-after-upload

Here is an example I used:

winscp.StandardInput.WriteLine(“put E:\\myfolder\\myfile.csv myfile” + DateTime.Now.ToString(“_MMddyyyy_HHmm”) + “.csv”);
The local file name is myfile.csv and the remote file name changed to
myfile_03202017_1413.csv

//Based on WinSCP site forum, Martin suggested: mv command should work as well
//winscp.StandardInput.WriteLine(“mv myfile.csv myfile”+ DateTime.Now.ToString(“_MMddyyyy_HHmm”) + “.csv”);