-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Azure-Samples/sqlextension
Adding the Teradata GETBIT function to the toolkit
- Loading branch information
Showing
4 changed files
with
44 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'getbit') | ||
DROP FUNCTION microsoft.getbit; | ||
GO | ||
|
||
CREATE FUNCTION microsoft.getbit(@value INT, @position INT) | ||
RETURNS BIT | ||
WITH SCHEMABINDING | ||
AS | ||
BEGIN | ||
|
||
-- Fast opt out for NULL values | ||
IF (@value IS NULL OR @position IS NULL) | ||
RETURN NULL; | ||
|
||
DECLARE @temp_value VARCHAR(1000) = ''; | ||
|
||
WHILE (@value != 0) | ||
BEGIN | ||
IF (@value % 2 = 0) | ||
SET @temp_value = '0' + @temp_value; | ||
ELSE | ||
SET @temp_value = '1' + @temp_value; | ||
|
||
SET @value = @value / 2; | ||
|
||
END; | ||
|
||
RETURN SUBSTRING(REVERSE(@temp_value), @position + 1, 1); | ||
|
||
END; | ||
GO |