Tuesday, January 19, 2016

Multiple valid OR conditions in select statement SQL Server

If you have a query that has OR condition,  you will not get multiple rows just because there are multiple conditions satisfied the result.
However many rows exist in table that many rows will be returned.

Also all the operators are evaluated before the query result is returned.
This can result in multiple rows to be returned.

Lets work with an example:

Create table #person (name varchar(50), age int)

-- insert one row
insert into #person values ('tom', 40)

-- select person whose name is either 'tom' or age is 40.
select * from #person 
where name = 'tom' or age = 40

One row will be returned.

-- insert second row 
insert into #person values ('monica', 41)

-- Now select with multiple conditions 
select * from #person 
where (name = 'tom' or age = 40 or age > 40)

2 rows will be returned which tells all the ORs are evaluated and based on filters, rows will be returned.

HTH

Thursday, January 14, 2016

Using star * in SQL Server Views

Learned a lesson about using star * in views.

If you add new cols in the table that is referenced in the view as * (along with other cols using join), the view will only show same number of columns that it is originally compiled with. 
View definition doesn’t get update right if * is not used in the end.
It keeps the same number of columns and moves the data to wrong columns while trying to return same number of cols.

Never use *.

HTH

Sunday, January 10, 2016

Return no result set with Min / Max

Hi,

Just in case if you wondering how not to return NULL rows when aggregate functions couldnt find value, this is how you can do it!


If Object_Id('tempdb..#myTable') Is Not Null 
Drop Table #myTable;
Go

Create Table #myTable(col1 Integer Null, col2 Integer Null)
Insert #myTable(col1) values (1)

;With t_max(tmax, tmin) As
       (Select Max(col2), Min(col2)
       From #myTable)
Select tmax, tmin
From t_max
Where tmax Is Not Null And tmin Is Not Null




HTH

Monday, December 21, 2015

Error accessing the system registry

If you get above error, make sure you open your Visual Studio 6.0 with "Run as Administrator" option.

You can also set this option permanently by going to the file location of Visual Basic 6.0 shortcut.
On the file, right click and go to Properties.
In Properties, select Compatibility tab.
In this tab, you will see "Run this program as an administrator" check box.
Check it and click OK.

This should do the trick permanently and now you don't need to open project by Run as Administrator option anymore.

HTH.

Tuesday, December 15, 2015

Parse Date in VB6

VB6 is not smart to understand some usual date formats that we use daily.
So for example, if you were to put yyymmdd in date type, VB6 will give Type mismatch error.


You would need to parse it to store in date type field as below:


Dim strDate as string
Dim dBusinessDate as date


strDate = "20151211"
dBusinessDate = Mid(strDate, 5, 2) & "/" & Right(strDate, 2) & "/" & Left(strDate, 4)

For more info: See Dates on MS page




HTH

Friday, November 13, 2015

Windows 10 Search with - in file name

If you have "-" in the beginning of word that you want to use to search file, Windows 10 wont be able to search.
To use search, you would replace "-" with "*".



Example:
If file name is: Hello-saqib-7526.txt, and you want to search files with "saqib", you would use *saqib or *7526.


HTH

Wednesday, November 4, 2015

Cast to Int16 - Type Conversion

In VB.net, if you need to convert number/string (assuming its number) to Int16, there isn't any function as Cint16 as there is for CInt.

Instead you would write :
Convert.Int16(value)
or
CType(value, Int16)