Wednesday, August 14, 2013

How to find all Saturday and Sunday of the Current Month in MS SQL

I have seen many people asking the question “How to show all Saturdays and Sundays of the current month” in forums, so I thought I will try which is the best and easiest solution for this requirement.

Here I am going to use common table expression (CTE) to show the week ends with the date of the month. Below is the actual query you can use to find all the weekends of the current month with date.

 
WITH CTE(DATE, DayOfTheMonth)
AS
(
SELECT DATEADD(DAY, -DAY(GETDATE()-1), GETDATE()), 1
UNION ALL
SELECT DATE+1, DayOfTheMonth+1 FROM CTE WHERE DayOfTheMonth < (SELECT DAY(DATEADD(DAY, -DAY(DATEADD(MM, 1, GETDATE())), DATEADD(MM, 1, GETDATE()))))
)
 
SELECT DATENAME(WEEKDAY, DATE) AS WeekDay_Name, DayOfTheMonth INTO #TempDateTable FROM CTE OPTION(MAXRECURSION 30)
 
SELECT * FROM #TempDateTable WHERE WeekDay_Name IN('Saturday', 'Sunday')
 
DROP table #TempDateTable
 
 

When you execute above query you will get the out put as

WeekDay_Name DayOfTheMonth
Saturday 3
Sunday 4
Saturday 10
Sunday 11
Saturday 17
Sunday 18
Saturday 24
Sunday 25
Saturday 31

Depends on the Month your DayOfTheMonth value will change. Since I am executing this in August 2013 it shows the records for that month.

image

List all tables in the SQL Database without Primary Key

Last week I was facing some issues with the application which I have been developing. Query was not working properly when somebody loaded huge test data in a table. when I checked the table I found that primary key was not created for that table even though it was not mentioned the database schema we have prepared. So I thought I will run a query to find out all the tables in the database which doesn’t have primary key.

Since I already created the query to find out all the tables without primary key in MS SQL database of course I have to share that with you also…

SELECT [schema_id] AS [Schema Name],[name] AS [Table Name], [type_desc] as [Table Type]
FROM sys.tables
WHERE OBJECTPROPERTY(OBJECT_ID,'TableHasPrimaryKey') = 0
 

Output of above query will be,

Table Names without primary key

Monday, August 5, 2013

How to enable Script debugging in web browser?

In this article I am going to explain you how to debug JavaScript or how to enable Script Debugging.

I have seen many people posting question in forum asking I am getting “Java script rut time error” and how to resolve this issue. Interestingly they post whole JavaScript also for the solution. With my experience it is bit hard to find out the actual issue in JavaScript if you are not able to find the exact line you are getting this exception.

Many people still not aware that we can debug JavaScript and find out the exact line where we are getting the script error.

To find the exact line where the exception is showing first we will enable the Script debugging in web browser. In this example I am using Internet Explorer.

To enable the JavaScript debugging in Internet Explorer follow below steps.
Open IE-->Go to Tools-->Internet Options—>Select Advanced Tab—>

Scroll down to Browsing section-->Un Check "Disable Script debugging (Internet Explorer)" and Uncheck "Disable script debugging (Other)" and Check "Display a notification about every script error".

Script Debugging

Click Ok to save the changes.

Now close your browser and open your web page and see the error message as pop up.

Script Debugging1

Click yes to open the script debugging and see in which line you are getting this error. Exact line you are getting this exception will be highlighted.

Script Debugging2

Now it will be very easy for you locate the line your script from your .aspx page.