Format Date in SQL Server

It’s very common to show the current date (i.e., month, day, and year) in different formats. Although you can obtain the current date with Microsoft SQL Server’s GETDATE function, getting the returned date in the format you need can take a lot of effort and code. The FormatDate UDF (user-defined function) provides dates in various formats by using the following options: Continue reading

Order BY Not Working in SQL Server Functions | Quickie

Database

Ever created a function and needed to include the Order By clause. It would result in the following error being loaded:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

The problem is that to use, it requires a TOP clause or FOR XML. By if you specify TOP 100, you’ll only get the first 100 rows. You can specify a big number that you might think will never exceed. But what if you do? The best bet is to use TOP 100 PERCENT.

This will get you all the rows and works in Functions.

For Loop with Negative Step | Quickie

Cannot believe that I’m writing some helpful examples using VB.Net, but that is the language that we’re using at work, so have to adapt now. Next find how to make a loop with a negative step (decreasing step):

Imports System

Public Class MainClass
 Shared Sub Main()
 For intCount As Integer = 10 To 1 Step -1
 'Add the item to the list
 System.Console.WriteLine(intCount)
 Next
 End Sub
End Class

Solution | Unable to write output file ‘…pdb’; unspecified error

Visual Studio 2005 / 2008

During this weekend, I started having a problem when building a solution in Visual Studio. It would load the following error:

Unable to write to output file Solution_Name.pdb

This Visual Studio solution has 8 projects, and a lot of files and folders. I tried several solutions that I found on the net, which worked for others, but none worked for my case including:

  • Delete the .pdb file from the solution – Didn’t Work
  • Delete the Debug folder – Still Didn’t work
  • Delete the whole obj Folder – Nothing Continue reading

Get data from an SQLDataSource to a DataTable and Bind Grid

This week, I needed to bind a gridview to and SQLDataSource. The only problem is that I need to include extra rows. How to do this? First I removed the binding code to bind the grid directly from HTML (in gridview remove the DataSourceID attribute).

Then I wanted to get the data from the SQL Data source and convert to a data table to insert and update the table as needed. Next is the code to get the data… Continue reading

SQL Server | Reset Identity Column

When you create a table in SQL Server, you would also have an Identity Primary Key. This type of key would increase as new rows are added. But sometimes you would just need to delete all the rows and start all over again.

To restart the identity column, you could delete the column and re-add it again. This would normally work, but if you have relations with the primary key, you wouldn’t be allowed to delete it.

The next code that will make this possible, and restart the Primary Key from 0, thus the new row will be set as 1: Continue reading