Blog Archives

Convert rows to column using pivot

DECLARE @t AS TABLE (key1 varchar(25), [Soucecolumn Name] varchar(25), ColumnValue varchar(25)) INSERT INTO @t VALUES (‘8308862’, ‘Name’, ‘1a234’), (‘8308862’, ‘GrossTons’, ‘1a235’), (‘8308862’, ‘CallSign’, ‘1a236’), (‘8308862’, ‘YearofBuild’,’1a237′), (‘8308862′ ,’Type’,’1a238′), (‘8308862′,’Date’,’1a239′), (‘8308862’, ‘NetTons’,’1a245′), (‘8308862′,’OfficialNumber’,’1a247′), (‘8308862′,’PlaceofBuild’,’1a278′), –For other key (‘8308863’, ‘Name’, ‘1a234’), (‘8308863’,

Tagged with:
Posted in SQL Server

TSQL Cursor Syntext

declare cursor cust_cursor fast_forward for select custid from tblCustomer Open cust_cursor fetch next from cust_cursor into @custId while @@fetch_status = 0 begin fetch next from cust_cursor into @custId end close cust_cursor deallocate cust_cursor

Tagged with: ,
Posted in SQL Server

Difference between Date Datatypes in SQL Server 2012

What is basic difference between different date data types in sql server 2012. In SQL 2012, there are new date datatypes have been included such as Datetime2, DateTimeOffset, smalldatetime, Time, Date. Major difference between each is date time precision and

Tagged with: , , ,
Posted in SQL Server

Import and Export data from csv in TSQL

There are common task which you have to perform as DBA or as programmer to import/export data in databbase. it’s really easy to import/export data from SQL Server if you know the correct commands to use it. here are easiest

Tagged with: , , , , ,
Posted in SQL Server

Group by concat string of Column value in TSQL

how to get following query by using just single statement id Name Value 1 A 4 1 B 8 2 C 9 to id Column 1 A:4, B:8 2 C:9 and query is  CREATE TABLE #YourTable ([ID] INT, [Name] CHAR(1),

Tagged with: , ,
Posted in SQL Server

TSQL function return string with Capital initial character

CREATE FUNCTION [dbo].[fnInitCap] ( @InputString varchar(max) ) RETURNS VARCHAR(max) AS BEGIN DECLARE @Index INT DECLARE @Char CHAR(1) DECLARE @PrevChar CHAR(1) DECLARE @OutputString VARCHAR(max) SET @OutputString = LOWER(@InputString) SET @Index = 1 WHILE @Index <= LEN(@InputString) BEGIN SET @Char = SUBSTRING(@InputString,

Tagged with:
Posted in SQL Server

Select one row from detail records based on condition.

I often come across the issue to return one row from detail table like invoice items or order details for one customer for specific condition. i.e. show me the max order product for each customer and order that kind of

Tagged with: ,
Posted in SQL Server
What people say about post?
seo hizmeti on Awesome wordpress ecommerce…

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 38 other followers