Saturday, August 12, 2017

Hands-on learning in Honolulu: COM2037 - Introduction to Structured Query Language (SQL)

The next session will be starting October 12, 2017. Register early.





Explore funding opportunity here:










Sunday, August 6, 2017

Encrypting email password in SQL Server

There is a simple but not most secure way of encrypting a password in SQL Server. It uses the T-SQL function EncryptByPassPhrase().

This function takes two arguments: PassPhrase and ClearText. You provide both the PassPhrase and the ClearText(this is your email password that needs to be encrypted using this function). The PassPhrase generates a TripleDes algorithm with a 128 Key bit length.

ClearText can be a variable of the following types:
nvarchar,
char,
varchar,
binary,
varbinary,
or nchar


PassPhrase can be of the following types:
nvarchar,
char,
varchar,
binary,
varbinary,
or nchar


The following code uses the following variables:
PassPhrase -> "Happy birthday to you"
ClearText->   "$#MyPassword#$"
---------
CREATE TABLE ENCRPTPSWD (EncryptedText varbinary(200))
go

declare @pswrd nvarchar(15)
set @pswrd = '$#MyPassword#$'--ClearText
INSERT INTO ENCRPTPSWD (EncryptedText)
values (EncryptByPassPhrase('Happy birthday to you', @pswrd))
go


The column ENCRPTPSWD contains the result of encrypting and is therefore of type varbinary. The phrase to de-encrypt is the phrase 'Happy birth day to you'.

Now if we access that column we see the encrypted value as shown.





SQL Server and two-digit year cut-off

Remember the millenium bug fiasco. It happenned in Year 2000.
This is what chronicled in Wikipedia:

"The Year 2000 problem is also known as the Y2K problem, the Millennium bug, the Y2K bug, or Y2K. Problems resulted because people, including programmers, reduced the four-digit year to two digits. This made the year 2000 indistinguishable from 1900"

Now we have a better interpretation for people using two digit years as far as SQL Server is concerned.

The default time span for two digit years in SQL Server is 1950-2049.

Two-digit year 49 is 2049, but two digit year 50 is back to 1950.

Probably you will start seeing it in your credit cards (may be?)

You may configure it in SQL Server 2017 using,

Exec Sp_Configure 'two digit year cutoff', 2038

In SQL Server 2012 Express it is enabled by default:



Read more here:
https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-the-two-digit-year-cutoff-server-configuration-option


Friday, August 4, 2017

Adding Excel Application to SQL Server Management Studio

I am using SQL Server Management Studio Version 17.1. You may user another version.

Launch SSMS 17.1

Click Tools | External Tools...

External Tools window is displayed as shown.



Replace the Title with a name of your own, herein Excel Application. The menu contents also
changes to the one you provided, herein Excel Application.

You need to provide the location of the executable in the Command box. In the present case the
executable is here:

"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"

Now you External Tools window should appear as shown.


Click OK.

Now you will find 'Excel Application' as shown in the Tools menu.


Now when you click this menu item, Excel Application is launched.

Tuesday, August 1, 2017

Usage of math operator Modulo in SQL Server

x%y is defined as the remainder of the division x/y. You can use modulo to find the minutes and hours in so many minutes.

Let us say we have a cconversion exercise to convert 123490 minutes into minutes and hours.
By long hand we would do this:

123490/60 and find the remainder
We get 2058.1666666667 with 0.1666666667 hours converts to 10
That is 2058 hours and 10 minutes.

We can do the same using modulo as shown.


OR

You can use a much shorter code as shown here: (note input data has changed to 1234901)



Monday, July 31, 2017

SQL Server 2012 (x86) and SQL Server 2016 Developer on the same x64 bit computer - Part 2

Earlier in Part 1 of this post we saw that the SQL Server Management Studio 17.1 easily allows you to work with SQL Server 2012 (x32) Express, the SQL Server Management Studio that installs with SQL Server 2012 Express Advanced has many GUI related problems. The problems were neither related to Display resolution nor to resizing of windows.

Laptop: Windows 10 Pro
SQL Server 2012: and SQL Server Express (x32) 2012

The following images require no explanation.











Difference between dbo and db_owner

This question pops up now and then.

dbo is a user and db_owner is a database role. They both are in the Security node in the Object Browser. The SQL Server Management Studio (SSMS) makes it abundantly clear.

dbo


dbo.png

db_owner

db_owner.png

It will be instructive to review their properties as shown here.

dbo - Properties


dboProps.png

db_owner - Properties


db_ownerProps.png