Skip to main content

Posts

Function that Splits string in SQL Server / Table Valued Function

CREATE FUNCTION [dbo] . [SplitString] ( @SplitStr nvarchar ( 1000 ), @SplitChar nvarchar ( 5 ) ) RETURNS @RtnValue table ( Data nvarchar ( 50 ) ) AS BEGIN Declare @Count int Set @Count = 1 While ( Charindex ( @SplitChar , @SplitStr )> 0 ) Begin Insert Into @RtnValue ( Data ) Select Data = ltrim ( rtrim ( Substring ( @SplitStr , 1 , Charindex ( @SplitChar , @SplitStr )- 1 ))) Set @SplitStr = Substring ( @SplitStr , Charindex ( @SplitChar , @SplitStr )+ 1 , len ( @SplitStr )) Set @Count = @Count + 1 End Insert Into @RtnValue ( Data ) Select Data = ltrim ( rtrim ( @SplitStr )) Return END To Execute above Function use the statement as follows select * from dbo . SplitString ( ',abc,defg,hij,klm,nopq,Test,123,' , ',' )

sql inda all country in database

CREATE TABLE country ( id int NOT NULL , country_code varchar(5) NOT NULL default '', country_name varchar(100) NOT NULL default '', ) -- INSERT INTO country VALUES (1, 'US', 'United States'); INSERT INTO country VALUES (2, 'CA', 'Canada'); INSERT INTO country VALUES (3, 'AF', 'Afghanistan'); INSERT INTO country VALUES (4, 'AL', 'Albania'); INSERT INTO country VALUES (5, 'DZ', 'Algeria'); INSERT INTO country VALUES (6, 'DS', 'American Samoa'); INSERT INTO country VALUES (7, 'AD', 'Andorra'); INSERT INTO country VALUES (8, 'AO', 'Angola'); INSERT INTO country VALUES (9, 'AI', 'Anguilla'); INSERT INTO country VALUES (10, 'AQ', 'Antarctica'); INSERT INTO country VALUES (11, 'AG', 'Antigua and/or Barbuda'); INSERT INTO country VALUES (12, 'AR', 'Argentina'); INSERT INTO country VALUES ...

SQL NEW INSERT STORE PROCEDURE DYNAMICALLY

IF EXISTS (         SELECT *         FROM dbo.sysobjects         WHERE id = object_id(N'[dbo].[test_create_procedure]')             AND OBJECTPROPERTY(id, N'IsProcedure') = 1         )     DROP PROCEDURE [dbo].[test_create_procedure] GO SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS OFF GO ALTER PROCEDURE create_procedure @table VARCHAR(200),     @DeveloperName VARCHAR(200),     @Createtable VARCHAR(20)     --create_procedure 'Products','VISHAL','Products' AS SET NOCOUNT ON DECLARE @testTable VARCHAR(8000) DECLARE @testTable2 VARCHAR(8000) DECLARE @testTable3 VARCHAR(8000) DECLARE @opration VARCHAR(8000) DECLARE @testTable_UPDATE VARCHAR(8000) DECLARE @final VARCHAR(8000) DECLARE @OP VARCHAR(100) SET @testTable = '' SET @testTable2 = '' SET @final = '' SET @testTable3 =...

DYNAMIC SQL INSERT STATEMENT

create table #tmp ( SQLText varchar(8000) ) create table #tmp2 ( Id int identity, SQLText varchar(8000) ) set nocount on delete #tmp delete #tmp2 declare @vsSQL varchar(8000), @vsCols varchar(8000), @vsTableName varchar(40) declare csrTables cursor for select name from sysobjects where type in ('u') and name in ('Customers') order by name open csrTables fetch next from csrTables into @vsTableName while (@@fetch_status = 0) begin select @vsSQL = '', @vsCols = '' select @vsSQL = @vsSQL + CASE when sc.type in (39,47,61,111) then '''''''''+' + 'isnull(rtrim(replace('+ sc.name + ','''''''','''''''''''')),'''')' + '+'''''',''+' when sc.type = 35 then '''''''''+...

datatable group by

private DataTable GetGroupedBy(DataTable dt, string columnNamesInDt, string groupByColumnNames, string typeOfCalculation) { //Return its own if the column names are empty if (columnNamesInDt == string.Empty || groupByColumnNames == string.Empty) { return dt; } //Once the columns are added find the distinct rows and group it bu the numbet DataTable _dt = dt.DefaultView.ToTable(true, groupByColumnNames); //The column names in data table string[] _columnNamesInDt = columnNamesInDt.Split(','); for (int i = 0; i < _columnNamesInDt.Length; i = i + 1) { if (_columnNamesInDt[i] != groupByColumnNames) { _dt.Columns.Add(_columnNamesInDt[i]); } } //Gets the collection and send it back for (int i = 0; i < _dt.Rows.Count; i = i + 1) { for (int j = 0; j < _columnNamesInDt.Length; j = j + 1) { if (_columnNamesInDt[j] != groupByColumnNames) { _dt.Rows[i][j] = dt.Compute(typeOfCalculation + "(" + _columnNamesInDt[j] + ")", groupByColumnName...

Building a Database Driven Hierarchical Menu using ASP.NET

Download AspSooperFish

ASP.NET DATE FORMAT CUSTOME

string SSS = DateTime.Now.ToString("dddd, d MMMM, yyyy", CultureInfo.CreateSpecificCulture("en-US")); DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15); Console.WriteLine(date1.ToString( "ddd d MMM" , CultureInfo.CreateSpecificCulture( "en-US" ))); // Displays Fri 29 Aug Console.WriteLine(date1.ToString( "ddd d MMM" , CultureInfo.CreateSpecificCulture( "fr-FR" ))); // Displays ven. 29 août      DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18); CultureInfo ci = CultureInfo.InvariantCulture; Console.WriteLine(date1.ToString( "hh:mm:ss.f" , ci)); // Displays 07:27:15.0 Console.WriteLine(date1.ToString( "hh:mm:ss.F" , ci)); // Displays 07:27:15 Console.WriteLine(date1.ToString( "hh:mm:ss.ff" , ci)); // Displays 07:27:15.01 Console.WriteLine(date1.ToString( "hh:mm:ss.FF" , ci)); // Displays 07:27:15.01 Console.WriteLine(date1....