ALTER FUNCTION [dbo].[ConvertToClientLocalTime] ( -- Add the parameters for the function here @Time DATETIME ,@TimeZone NVARCHAR(100) ) RETURNS DATETIME AS BEGIN -- Declare the return variable here DECLARE @LocalDate DATETIME SELECT @LocalDate = CASE WHEN @Time IS NULL OR @Time = '' THEN NULL ELSE dbo.ConvertToClientTime(@Time, @TimeZone) END -- Return the result of the function RETURN @LocalDate; END ------------------------------Below function called inside above----- ALTER FUNCTION [dbo].[ConvertToClientTime](@time [datetime], @timeZone [nvarchar](100)) RETURNS [datetime] --WITH EXECUTE AS CALLER ...
Posts
Showing posts from February, 2015
StringToNumeric
- Get link
- X
- Other Apps
ALTER FUNCTION [dbo].[ConvertStringToNumeric] (@Value text) RETURNS NUMERIC(12,2) AS BEGIN declare @string varchar(max) SET @string = CONVERT(VARCHAR(MAX), @Value) --set @string='abc93.3ad4' DECLARE @IncorrectCharLoc SMALLINT SET @IncorrectCharLoc = PATINDEX('%[^0-9.-]%', @string) WHILE @IncorrectCharLoc > 0 BEGIN SET @string = STUFF(@string, @IncorrectCharLoc, 1, '') SET @IncorrectCharLoc = PATINDEX('%[^0-9.-]%', @string) ...
ConvertLocalToUTC
- Get link
- X
- Other Apps
ALTER FUNCTION [dbo].[ConvertLocalToUTC](@time [datetime],@timeZoneAdj Int, @IsDayLightSavings Bit) RETURNS [datetime] --WITH EXECUTE AS CALLER AS BEGIN --EXTERNAL NAME [Mmt.Cyramed.Cryptography].[Mmt.Cyramed.Cryptography.Cryptography].[ConvertLocalToUTC] RETURN dbo.[GetPracticeUTCDateTime](@time,@timeZoneAdj,@IsDayLightSavings) END --------------Below function is called inside above----------- ALTER FUNCTION [dbo].[GetPracticeUTCDateTime] ( @InLocaleDateTime DATETIME, @TimeZoneAdj INT, @IsDayLightSaving BIT ) RETURNS DateTime AS BEGIN DECLARE @OutLocalDateTime DATETIME set @outLocalDateTime = dateadd(MINUTE , -1 * @TimeZoneAdj, @inLocaleDateTime) if (@IsDayLightSaving = 1) -- figure in daylight savings time BEGIN declare @yearT...
SplitWordList
- Get link
- X
- Other Apps
ALTER function [dbo].[SplitWordList] ( @list varchar(MAX) ) returns @t table ( Word varchar(50) not null, Position int identity(1,1) not null ) as begin declare @pos int, @lpos int, @item varchar(100), @ignore varchar(100), @dl int, @a1 int, @a2 int, @z1 int, @z2 int, @n1 int, @n2 int, @n3 int, @c varchar(1), @a smallint select @a1 = ascii('a'), @a2 = ascii('A'), @z1 = ascii('z'), @z2 = ascii('Z'), @n1 = ascii('0'), @n2 = ascii('9'), @n3 = ascii('-') set @ignore = '''"' set @pos ...
StringToTable
- Get link
- X
- Other Apps
ALTER FUNCTION [dbo].[udf_StringToTable] ( @list NVARCHAR(MAX) , @delimiter NCHAR(1) = ',' --Defaults to CSV ) RETURNS @tableList TABLE ( value nvarchar(MAX) ) AS BEGIN DECLARE @value NVARCHAR(MAX) DECLARE @position INT SET @list = LTRIM(RTRIM(@list))+ ',' SET @position = CHARINDEX(@delimiter, @list, 1) IF REPLACE(@list, @delimiter, '') <> '' BEGIN WHILE @position > 0 BEGIN SET @value = LTRIM(RTRIM(LEFT(@list, @position - 1))) IF @value <> '' BEGIN INSERT INTO @tableList (value) ...
splitstring
- Get link
- X
- Other Apps
ALTER FUNCTION [dbo].[splitstring] (@stringToSplit VARCHAR(MAX)) RETURNS @returnList TABLE ([Name] [nvarchar](500)) AS BEGIN DECLARE @name NVARCHAR(255) DECLARE @pos INT WHILE CHARINDEX(',', @stringToSplit) > 0 BEGIN SELECT @pos = CHARINDEX(',', @stringToSplit) SELECT @name = SUBSTRING(@stringToSplit, 1, @pos - 1) INSERT INTO @returnList SELECT @name SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos + 1, LEN(@stringToSplit) - @pos) END INSERT INTO @returnList SELECT @stringToSplit RETURN END
GetPhoneSSNFormat
- Get link
- X
- Other Apps
ALTER FUNCTION [dbo].[udf_GetPhoneSSNFormat] ( -- Add the parameters for the function here @TypeId INT, @DataValue NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @FormattedValue NVARCHAR(MAX) SET @FormattedValue = '' -- Will Remove all white spaces, brackets, hyphens DECLARE @OriginalValue NVARCHAR(MAX) SET @OriginalValue = ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(@DataValue,'(',''),')',''),'-',''),' ',''),'') -- Type Id : 1 For Phone # -- Type Id : 2 For SSN # IF @TypeId = 1 BEGIN SELECT @FormattedValue = ...
SQL Calculate Age
- Get link
- X
- Other Apps
ALTER FUNCTION [dbo].[ufn_CalculateAge] ( @BirthDate DATETIME, @CurrentDate DATETIME ) RETURNS NUMERIC(18,0) AS BEGIN RETURN ( ( --Age Years Apart i.e. quotient CASE WHEN @BirthDate > @CurrentDate THEN NULL WHEN DATEPART(day, @BirthDate) > DATEPART(day, @CurrentDate) THEN DATEDIFF(month, @BirthDate, @CurrentDate) - 1 ...
NumberToWords SQL
- Get link
- X
- Other Apps
ALTER FUNCTION [dbo].[udf_NumberToWords] ( @Number Numeric (38, 0) -- Input number with as many as 18 digits ) RETURNS VARCHAR(8000) /* * Converts a integer number as large as 34 digits into the * equivalent words. The first letter is capitalized. * Example: select dbo.udf_NumberToWords (1234567890) + CHAR(10) + dbo.udf_NumberToWords (0) + CHAR(10) + dbo.udf_NumberToWords (123) + CHAR(10) select dbo.udf_NumberToWords(76543210987654321098765432109876543210) DECLARE @i numeric (38,0) SET @i = 0 WHILE @I <= 1000 BEGIN PRINT convert (char(5), @i) + convert(varchar(255), dbo.udf_NumberToWords(@i)) SET @I = @i + 1 END * * Published as the T-SQL UDF of the Week Vol 2 #9 2/17/03 ****************************************************************/ AS BEGIN ...