How to catch specific MS-SQL SQLExceptions in C# and VB.NET

To catch specific SQLExceptions using Microsoft.NET and Microsoft Sequel Server you can use the snippet below. You also need to look for the specific error numbers/codes you want to catch. To do this you can either execute the following query in the SQL Management Studio or you can use this
List of SQLException Numbers/Codes (Direct-DL around 15mb).

Samples

T-SQL Sample

C# Sample

VB.NET Sample

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

Please rate this snippet

How to get the current Database and Log Filesize using MSSQL

To get the current Database and Log Filesize using MSSQL you can use the snippet below. Tested using SQL Server 2012.

Sample MSSQL

with database_files_CTE as
(
    select
        name, type_desc, physical_name, size_mb = convert(decimal(11, 2), size * 8.0 / 1024),
        space_used_mb = convert(decimal(11, 2), fileproperty(name, 'spaceused') * 8.0 / 1024) from sys.database_files
)
select name,type_desc as [type],physical_name as [path],size_mb as [Filesize],space_used_mb as [Filesize used],
case size_mb when 0 then 0 else convert(decimal(5, 2), space_used_mb / size_mb * 100) end as [Filesize used %]  from database_files_CTE;

Sample Output

Query Results Filesizes

Please rate this snippet

How to drop Primary Key of a Table using MSSQL

To drop Primary Key of a Table using MSSQL you can use the snippet below.

Sample MSSQL

ALTER TABLE tblTest
DROP CONSTRAINT PK_tblTest
GO

Please rate this snippet

How to search stored procedures using MS-SQL

To search stored procedures using MS-SQL you can use the snippet below.

Sample MSSQL

SELECT Name FROM sys.procedures WHERE name LIKE '%StoredProcName%'

Please rate this snippet

How to use IF ELSE in an SQL Select query

In MSSQL IF ELSE is not supported inside the SQL-Query.
For a similiar behaviour you should use CASE WHEN keyword.

Sample Query

SELECT CASE 
            WHEN [Productnumber] LIKE '112%' THEN 'SOFTWARE'
			WHEN [Productnumber] LIKE '005%' THEN 'HARDWARE'
		    ELSE 'MISC' 
       END as [ProductType] 
FROM dbo.[Products]

Please rate this snippet

How to list all Databases in MSSQL

To list all Databases in MSSQL you can use the following snippet.

Sample MSSQL

SELECT name FROM sys.databases

SQL Server Version newer than MS SQL 2000 also support these StoredProcedures.

EXEC sp_databases

or

EXEC sp_helpdb

Please rate this snippet

How to TRIM a string in MSSQL

To TRIM a string in MSSQL you can use the following Userdefinedfunction Snippet.

Sample MSSQL

CREATE FUNCTION dbo.TRIM(@inputString VARCHAR(MAX))
RETURNS VARCHAR(MAX)
BEGIN
RETURN LTRIM(RTRIM(@inputString))
END

Please rate this snippet

How to check if a column exists in MSSQL

To check if a column exists in MSSQL you can use the following snippet.

Sample MSSQL

SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('Pricetable') AND name='Price'

Please rate this snippet

How to select date from datetime in MSSQL

To select date from datetime in MSSQL you can use the following snippet.

Sample MSSQL

SELECT CONVERT(VARCHAR(10),Testdatecolumn,104) FROM dbo.tblTest

The 104 is for tt.mm.jjjj, a list of formats can be found at the MSDN CAST and CONVERT (Transact-SQL)

Please rate this snippet

How to use the Space() function in MSSQL

To learn how to use the Space() function in MSSQL you can take a look at the following snippet.

 

Let’s say you have a table (tblTest) like this:

ID Testcolumn
13 X000013
14 X000014
15 X000015

 

You want to select the next “X” number for Testcolumn. The problem is that when adding something to 000015 you will loose the 0 characters. So after getting the information that 16 is the next highest number you need to fill the gap between X and 16 with 0. And if the length of the X number is not fixed, you need to do that generic. The Space() function can help you alot. What the space function does is, it just creates as much spaces as you need. We will use this to replace the Spaces then with 0.

Sample MSSQL

SELECT TOP 1 Testcolumn AS [Currently Highest Field Value],

CONCAT(
	LEFT(Testcolumn,1), -- select X
	REPLACE( --Replace *
	SPACE(LEN(Testcolumn) - 1 - LEN(CONVERT(BIGINT,SUBSTRING(Testcolumn,2,LEN(Testcolumn))))) -- adds the missing characters as Space
	,' ','0') -- * each created SPACE() with a 0
	, 
	CONVERT(BIGINT,SUBSTRING(Testcolumn,2,LEN(Testcolumn))) --select 15
	+1 -- adds 1
	) AS  VALUE
    
FROM tblTest 
ORDER BY SUBSTRING(testcolumn,2,LEN(testcolumn)) DESC

The result will be

X000016

Please rate this snippet