Posts

How to get the word counts in sql server

CREATE FUNCTION [dbo].[WordCount] ( @InputString VARCHAR(max) ) RETURNS INT AS BEGIN DECLARE @Index INT DECLARE @Char CHAR(1) DECLARE @PrevChar CHAR(1) DECLARE @WordCount INT SET @Index = 1 SET @WordCount = 0 WHILE @Index <=LEN(@InputString) BEGIN SET @Char = SUBSTRING(@InputString, @Index, 1) SET @PrevChar = CASE WHEN @Index = 1 THEN ' ' ELSE SUBSTRING(@InputString, @Index - 1, 1) END IF @PrevChar = ' ' AND @Char != ' ' SET @WordCount = @WordCount + 1 SET @Index = @Index + 1 END RETURN @WordCount END

Get Immediate and Ultimate Parents with levels

WITH r ( id , parent , ultimate_parent , name , ultimate_parent_name , lvl ) as ( SELECT id , parent_id AS parent , parent_id AS ultimate_parent , name , name as ultimate_parent_name , 0 lvl FROM mytable WHERE parent_id = id -- identifies a root UNION ALL SELECT m . id , r . id , ultimate_parent , m . name , r . ultimate_parent_name , r . lvl + 1 FROM r join mytable m on m . parent_id = r . id -- joins child with parent WHERE m . parent_id <> m . id -- to avoid cycles ) SELECT * FROM r ;   The first part of the subquery fetches the roots and the second part connects  the children.   Parent is the immediate parent and ultimate_parent , the ultimate parent.

How to Read TOC Headings from word document using C#

Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application(); object paramMissing = Type .Missing; wordApplication.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF; bool paramOpenAfterExport = false ; WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint; WdExportRange paramExportRange = WdExportRange.wdExportAllDocument; int paramStartPage = 0; int paramEndPage = 0; WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent; bool paramIncludeDocProps = true ; bool paramKeepIRM = true ; WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks; bool paramDocStructureTags = true ; bool paramBitmapMissingFonts = true ; bool paramUseISO19005_1 = false ; try { // Open the source document. wordDocument = wordApplication.Documents.Open( ...

Creating XML File Using SQL Server

SELECT ( SELECT 'White' AS Color1, 'Blue' AS Color2, 'Black' AS Color3, 'Light' AS 'Color4/@Special' , 'Green' AS Color4, 'Red' AS Color5 FOR XML PATH('Colors'), TYPE ), ( SELECT 'Apple' AS Fruits1, 'Pineapple' AS Fruits2, 'Grapes' AS Fruits3, 'Melon' AS Fruits4 FOR XML PATH('Fruits'), TYPE ) FOR XML PATH(''), ROOT('SampleXML') /*Out Put*/ <SampleXML>   <Colors>     <Color1>White</Color1>     <Color2>Blue</Color2>     <Color3>Black</Color3>     <Color4 Special="Light">Green</Color4>     <Color5>Red</Color5>   </Colors>   <Fruits>     <Fruits1>Apple</Fruits1>     <Fruits2>Pineapple</Fruits2>     <Fruits3>Grapes</Fruits3>     <Fruits4>Melon</Fruits4>   </Fruits> ...

Delete Duplicate Rows in Sql Server

/* Create Table with 7 entries - 3 are duplicate entries */ CREATE TABLE DuplicateRcordTable (Col1 INT, Col2 INT) INSERT INTO DuplicateRcordTable SELECT 1, 1 UNION ALL SELECT 1, 1 --duplicate UNION ALL SELECT 1, 1 --duplicate UNION ALL SELECT 1, 2 UNION ALL SELECT 1, 2 --duplicate UNION ALL SELECT 1, 3 UNION ALL SELECT 1, 4 SELECT * FROM DuplicateRcordTable /* Delete Duplicate records */ WITH CTE (COl1,Col2, DuplicateCount) AS ( SELECT COl1,Col2, ROW_NUMBER() OVER(PARTITION BY COl1,Col2 ORDER BY Col1) AS DuplicateCount FROM DuplicateRcordTable ) DELETE FROM CTE WHERE DuplicateCount > 1

How to Zip the folder using C#

public static void AddFolderToZip(string sourcePath,string zipFullName)         {             DirectoryInfo CurrentFolder = new DirectoryInfo(sourcePath);             DirectoryInfo RootFolder = CurrentFolder.Parent;             DirectoryInfo[] SubFolders = RootFolder.GetDirectories(CurrentFolder.Name);                                     if (SubFolders.LongLength > 0)             {                 //Add all subfolders to the zip if any         ...

How to Convert Word Document to PDF using C#

  Document wordDocument = null;             string Sourcepath = ConfigurationSettings.AppSettings["SourcePath"];             string DestinationPath = ConfigurationSettings.AppSettings["DestinationPath"];             DirectoryInfo dirInfo = new DirectoryInfo(Sourcepath);             FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");             foreach (FileInfo wordFile in wordFiles)             {                 object paramSourceDocPath = (Object)wordFile.FullName;                 //(Object)wordFile.Na...