Created
February 22, 2018 10:05
-
-
Save joacar/307541345b5b32d130dc29f1c1e12043 to your computer and use it in GitHub Desktop.
Import data into SQL from Google Spreadsheet or Microsoft Excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Split string Table-Valued function | |
CREATE FUNCTION SplitString | |
( | |
@Input NVARCHAR(MAX), | |
@Character CHAR(1) | |
) | |
RETURNS @Output TABLE ( | |
Item NVARCHAR(1000) | |
) | |
AS | |
BEGIN | |
DECLARE @StartIndex INT, @EndIndex INT | |
SET @StartIndex = 1 | |
IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character | |
BEGIN | |
SET @Input = @Input + @Character | |
END | |
WHILE CHARINDEX(@Character, @Input) > 0 | |
BEGIN | |
SET @EndIndex = CHARINDEX(@Character, @Input) | |
INSERT INTO @Output(Item) | |
SELECT SUBSTRING(@Input, @StartIndex, @EndIndex - 1) | |
SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input)) | |
END | |
RETURN | |
END | |
GO | |
-- Enclose each cell in single quote | |
-- =ARRAYFORMULA("'"&<range>&"'") | |
-- Transform column in Google Spreadsheet to row | |
-- =join(",";<range>) | |
CREATE TABLE #TempTable ([Name] NVARCHAR(max)) | |
INSERT INTO #TempTable | |
SELECT * FROM dbo.SplitString('string',',') -- string_split |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment