use zipcode
go
If Exists(Select * From Information_Schema.Tables Where Table_Name = 'ZipCodes' And Table_Type = 'Base Table')
Drop Table ZipCodes
CREATE TABLE ZipCodes(
Country varchar(2) NULL,
ZipCode varchar(5) NULL,
City varchar(200) NULL,
state varchar(50) NULL,
StateAbbreviation varchar(2) NULL,
County varchar(50) NULL,
Unused1 varchar(5) NULL,
Unused2 varchar(1) NULL,
Latitude Decimal(8,5) NULL,
Longitude Decimal(8,5) NULL,
Unused3 varchar(1) NULL
)
Alter Table ZipCodes Drop Column Unused1, Unused2, Unused3
Declare @bulk_cmd varchar(1000)
Set @bulk_cmd = 'BULK INSERT ZipCodes
FROM ''c:\YourFolder\correct_database.csv''
WITH (FIELDTERMINATOR='';'', ROWTERMINATOR = ''\n'', LASTROW = 45234)'
EXEC(@bulk_cmd)
select * from zipcodes
Create Function dbo.LongitudePlusDistance
(@StartLongitude Float,
@StartLatitude Float,
@Distance Float)
Returns Float
AS
Begin
Return (Select @StartLongitude + Sqrt(@Distance * @Distance / (4784.39411916406 * Cos(2 * @StartLatitude / 114.591559026165) * Cos(2 * @StartLatitude / 114.591559026165))))
End
Create Function dbo.LatitudePlusDistance(@StartLatitude Float, @Distance Float) Returns Float
As
Begin
Return (Select @StartLatitude + Sqrt(@Distance * @Distance / 4766.8999155991))
End
Create Function dbo.CalculateDistance(@Longitude1 Float, @Latitude1 Float, @Longitude2 Float, @Latitude2 Float)
Returns Float
AS
Begin
Return Sqrt(Power(@Longitude2 - @Longitude1, 2) * Power(Cos((@Latitude1 + @Latitude2) / 114.591559026164646), 2) * 12391523883.7963953387686623629457364 + Power(@Latitude2 - @Latitude1,2) * 12346214104.563361) / 1609.344
End
CREATE PROCEDURE cst_Find_Duplicates_In_Table(
@Table VARCHAR(25),
@Field VARCHAR(100))
AS
--DECLARE @Table AS VARCHAR(25)
--DECLARE @Field AS VARCHAR(100)
--SET @Table = 'Table'
--SET @Field = 'Field' --Name of Field that will determine if you have a duplicate
DECLARE @Sql AS VARCHAR(8000)
SET @Sql = 'SELECT ' + @Field + ', COUNT(' + @Field + ') AS NumCount ' +
' FROM ' + @Table +
' GROUP BY ' + @Field +
' HAVING (COUNT(' + @Field + ') > 1) ' +
' ORDER BY ' + @Field + ';'
EXEC(@Sql);