Bentuk-bentuk Query Pada SQL 2000+
Sesi Pertama
SELECT TsMovement3Cass.IATACode, MsAirline.AirlineName, MsCustomerCass.AgentCode, TsMovement3Cass.AccNo, MsCustomerCass.AccName,
SUM(TsMovement3Cass.BillCurr15 + TsMovement3Cass.BillCurr30) AS Sum_Of_Bill,
SUM(TsMovement3Cass.PayBillCurr15 + TsMovement3Cass.PayBillCurr30) AS Sum_Of_Payment,
MsMonth.MonthName + ‘ ‘ + TsCalendarCass.Cycle + ‘ ‘ + CONVERT(varchar, TsCalendarCass.CalendarYear) AS PeriodName, MsMonth.MonthName,
TsCalendarCass.Cycle, TsCalendarCass.CalendarYear, MsCustomerCass.AgentCode AS Expr1
FROM TsMovement3Cass INNER JOIN
MsAirline ON TsMovement3Cass.IATACode = MsAirline.IATACode INNER JOIN
MsCustomerCass ON TsMovement3Cass.AccNo = MsCustomerCass.AccNo INNER JOIN
TsCalendarCass ON TsMovement3Cass.CalendarID = TsCalendarCass.CalendarID INNER JOIN
MsMonth ON TsCalendarCass.CalendarMonth = MsMonth.MonthID
WHERE (TsMovement3Cass.Status = ‘A’)
GROUP BY TsMovement3Cass.IATACode, MsAirline.AirlineName, TsMovement3Cass.AccNo, MsCustomerCass.AccName, MsMonth.MonthName,
TsCalendarCass.Cycle, MsMonth.MonthName, TsCalendarCass.Cycle, TsCalendarCass.CalendarYear, MsCustomerCass.AgentCode
Sesi Kedua PIVOT Table
SELECT A.CalendarID, A.CalendarYear, A.CalendarMonth,
A.MonthName, A.Cycle, A.PeriodName, A.TrxDate,
A.AccNo, A.AccName, A.AgentCode, A.CityAbrv,
A.CcyName,
SUM(CASE A.IATACode WHEN ‘020′
THEN A.BillCurr30 ELSE 0 END) AS ‘Bill_30_020′,
SUM(CASE A.IATACode WHEN ‘065′
THEN A.BillCurr30 ELSE 0 END) AS ‘Bill_30_065′,
SUM(CASE A.IATACode WHEN ‘020′
THEN A.PayBillCurr30 ELSE 0 END) AS ‘Pay_30_020′,
SUM(CASE A.IATACode WHEN ‘065′
THEN A.PayBillCurr30 ELSE 0 END) AS ‘Pay_30_065′,
SUM(CASE A.IATACode WHEN ‘020′
THEN A.BillPrev30 ELSE 0 END) AS ‘Bill_30_P_020′,
SUM(CASE A.IATACode WHEN ‘065′
THEN A.BillPrev30 ELSE 0 END) AS ‘Bill_30_P_065′,
SUM(CASE A.IATACode WHEN ‘020′
THEN A.PayBillPrev30 ELSE 0 END) AS ‘Pay_30_P_020′,
SUM(CASE A.IATACode WHEN ‘065′
THEN A.PayBillPrev30 ELSE 0 END) AS ‘Pay_30_P_065′
FROM (SELECT TsMovement3Cass.CalendarID,
TsCalendarCass.CalendarYear, TsCalendarCass.CalendarMonth,
MsMonth.MonthName, TsCalendarCass.Cycle,
MsMonth.MonthName + ‘ ‘ + TsCalendarCass.Cycle + ‘ - ‘ +
CONVERT(varchar, TsCalendarCass.CalendarYear)
AS PeriodName, TsMovement3Cass.TrxDate, TsMovement3Cass.AccNo,
MsCustomerCass.AccName, MsCustomerCass.AgentCode,
TsMovement3Cass.IATACode, MsCustomerCass.CityAbrv,
MsCcy.CcyName, TsMovement3Cass.OpenBal, BillCurr30, PayBillCurr30,
OutBillCurr30, BillPrev30, PayBillPrev30, CloseBalPrev30
FROM TsMovement3Cass
INNER JOIN MsCustomerCass
ON TsMovement3Cass.AccNo = MsCustomerCass.AccNo
INNER JOIN TsCalendarCass
ON TsMovement3Cass.CalendarID = TsCalendarCass.CalendarID
INNER JOIN MsMonth
ON TsCalendarCass.CalendarMonth = MsMonth.MonthID
INNER JOIN MsCcy ON MsCustomerCass.Ccy = MsCcy.Ccy) A
GROUP BY A.CalendarID, A.CalendarYear, A.CalendarMonth,
A.MonthName, A.Cycle, A.PeriodName, A.TrxDate,
A.AccNo, A.AccName, A.AgentCode, A.CityAbrv, A.CcyName
————————————————————————
SQL SERVER - 2005 - T-SQL Script to Attach and Detach Database
August 24, 2007 by pinaldave
Following script can be used to detach or attach database. If database is to be from one database to another database following script can be used detach from old server and attach to new server.
Process to move database :
—-Step 1 : Detach Database using following script
USE [master]
GO
EXEC master.dbo.sp_detach_db @dbname = N’AdventureWorks’,
@keepfulltextindexfile = N’true’
GO
—-Step 2 : Move Data files and Log files to new location
–—Step 3 : Attach Database using following script
USE [master]
GO
CREATE DATABASE [AdventureWorks] ON
( FILENAME = N’C:\Data\AdventureWorks_Data.mdf’ ),
( FILENAME = N’C:\Data\AdventureWorks_Log.ldf’ )
FOR ATTACH
GO
IF EXISTS ( SELECT name
FROM master.sys.databases sd
WHERE name = N’AdventureWorks’
AND SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() )
EXEC [AdventureWorks].dbo.sp_changedbowner @loginame=N’sa’,
@map=false
GO
Reference : Pinal Dave (http://www.SQLAuthority.com), SQL SERVER - 2005 Take Off Line or Detach Database
SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
February 6, 2008 by pinaldave
This is very common request recently - How to import CSV file into SQL Server? How to load CSV file into SQL Server Database Table? How to load comma delimited file into SQL Server? Let us see the solution in quick steps.
CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values.
Create TestTable
USE TestData
GO
CREATE TABLE CSVTest
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO
Create CSV file in drive C: with name csvtest.txt with following content. The location of the file is C:\csvtest.txt
1,James,Smith,19750101
2,Meggie,Smith,19790122
3,Robert,Smith,20071101
4,Alex,Smith,20040202
Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.
BULK
INSERT CSVTest
FROM ‘c:\csvtest.txt’
WITH
(
FIELDTERMINATOR = ‘,’,
ROWTERMINATOR = ‘\n’
)
GO
Check the content of the table.
SELECT *
FROM CSVTest
GO
Drop the table to clean up database.
SELECT *
FROM CSVTest
GO
Reference : Pinal Dave (http://www.SQLAuthority.com)
Posted in Author Pinal, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL Utility, T SQL, Technology | 62 Comments
62 Responses to “SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server”
Archive for the ‘SQL Function’ Category
SQL SERVER - Create a Comma Delimited List Using SELECT Clause From Table Column
Posted in Author Pinal, Database, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology on June 4, 2008 | 2 Comments »
I received following question in email :
How to create a comma delimited list using SELECT clause from table column?
Answer is to run following script.
USE AdventureWorks
GO
DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+‘,’ ,”) + Name
FROM Production.Product
SELECT @listStr
GO
I have previously written similar […]
SQL SERVER - Example of DISTINCT in Aggregate Functions
Posted in Author Pinal, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology on June 3, 2008 | No Comments »
Just a day ago, I was was asked this question in one of the teaching session to my team members. One of the member asked me if I can use DISTINCT in Aggregate Function and does it make any difference.
Of course! It does make difference. DISTINCT can be used to return unique rows from a […]
SQL SERVER - UNPIVOT Table Example
Posted in Author Pinal, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology on May 29, 2008 | No Comments »
My previous article SQL SERVER - PIVOT Table Example encouraged few of my readers to ask me question about UNPIVOT table. UNPIVOT table is reverse of PIVOT Table.
USE AdventureWorks
GO
CREATE TABLE #Pvt ([CA] INT NOT NULL,
[AZ] INT NOT NULL,
[TX] INT NOT NULL);
INSERT INTO #Pvt ([CA], [AZ], [TX])
SELECT [CA], [AZ], [TX]
FROM
(
SELECT sp.StateProvinceCode
FROM Person.Address a
INNER JOIN Person.StateProvince sp
ON a.StateProvinceID = sp.StateProvinceID
) p
PIVOT
(
COUNT (StateProvinceCode)
FOR StateProvinceCode
IN ([CA], [AZ], [TX])
) AS pvt;
SELECT StateProvinceCode, Customer_Count
FROM
(
SELECT [CA], [AZ], [TX]
FROM #Pvt
) t
UNPIVOT
(
Customer_Count
FOR StateProvinceCode
IN ([CA], [AZ], [TX])
) AS unpvt;
DROP TABLE #Pvt;
GO
Reference : Pinal Dave (http://www.SQLAuthority.com)
SQL SERVER - SQL SERVER - UDF - Get the Day of the Week Function - Part 3
Posted in Author Pinal, SQL, SQL Authority, SQL DateTime, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology on May 27, 2008 | No Comments »
Datetime functions and stored procedures always interests me. Nanda Kumar has suggested modification to previous written article about SQL SERVER - SQL SERVER - UDF - Get the Day of the Week Function - Part 2. He has improved on UDF.
CREATE FUNCTION dbo.udf_DayOfWeek(@dtDate DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @rtDayofWeek VARCHAR(10)
DECLARE @weekDay INT
—-Here I have subtracted 7 For keeping Sunday as the First day like wise for Monday we need to subtract 2 and so on
SET @weekDay=((DATEPART(dw,@dtDate)+@@DATEFIRST-7)%7)
SELECT @rtDayofWeek = CASE @weekDay
WHEN 1 THEN ‘Sunday’
WHEN 2 THEN ‘Monday’
WHEN 3 THEN ‘Tuesday’
WHEN 4 THEN ‘Wednesday’
WHEN 5 THEN ‘Thursday’
WHEN 6 THEN ‘Friday’
WHEN 0 THEN ‘Saturday’
END
RETURN (@rtDayofWeek)
END
GO
SELECT dbo.udf_dayofweek(GETDATE())
Reference : Pinal Dave (http://www.SQLAuthority.com), Nanda Kumar
Sesi Ketiga



