Tag: CSV

Using comma separated value CSV parameter strings in SQL IN clauses

Author: | Categories: Database No Comments
Below function when called with a CSV list returns a temporary table which can be used in “IN” of WHERE clause /****** Object: User Defined Function [dbo].[CSVTable] ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[CSVTable] (@InStr VARCHAR(MAX)) RETURNS @TempTab TABLE (id varchar(10) not null) AS BEGIN SET @InStr = REPLACE(@InStr + […]

SQL SERVER – Remove the last character in a string

Author: | Categories: Database No Comments
Sometimes it is required to remove last character usually a comma from SQL string, below are 3 way to achieve it. Option 1: Using LEFT function DECLARE @String as VARCHAR(50) SET @String='1,2,3,4,5,' SELECT @String As [String] ,LEFT(@String,LEN(@String)-1) As [Last Character removed from string] GO Option 2: Using STUFF function DECLARE @String as VARCHAR(50) SET @String='1,2,3,4,5,' […]

Datatable to CSV file

Author: | Categories: Database, Programming No Comments
C# DataTable to a CSV file C# DataTable to a CSV file with default options i.e. all columns with default column names public static string DataTableToCsvFile(DataTable TableData, string FileName) { try { // change this with any delimiter you want string Delimiter = ","; int i = 0; StreamWriter sw = null; sw = new […]