Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
crazyco
Starting Member
30 Posts |
Posted - 2012-03-20 : 09:42:36
|
I have a function which I need to change. At the moment it returns a date in the format '12/02/2008' but I would like it to return a date as '2012-02-01'.ALTER FUNCTION [dbo].[anonDOB](-- Add the parameters for the function here@DOB varchar(10))RETURNS varchar(10)ASBEGIN-- Declare the return variable hereDECLARE @anonDOB varchar(10), @month varchar(2), @slashes VARCHAR(1)IF(LEN(@DOB) = 10)BEGINSET @DOB = REPLACE(@DOB, '/','')SET @slashes = 'Y'ENDSET @month = SUBSTRING(@DOB,3,2)IF @month IN ('01','02','03','04','05','06','07','08','09','10','11','12')-- Add the T-SQL statements to compute the return value hereIF @month IN ('01','02','03')SET @anonDOB = '0102' + RIGHT(@DOB,len(@DOB) - 4)ELSE IF @month IN ('04','05','06')SET @anonDOB = '0105' + RIGHT(@DOB,len(@DOB) - 4)ELSE IF @month IN ('07','08','09')SET @anonDOB = '0108' + RIGHT(@DOB,len(@DOB) - 4)ELSE IF @month IN ('10','11','12')SET @anonDOB = '0111' + RIGHT(@DOB,len(@DOB) - 4)ELSE SET @anonDOB = NULLELSE SET @anonDOB = NULLIF @slashes = 'Y'SET @anonDOB = LEFT(@anonDOB, 2) + '/' + SUBSTRING(@anonDOB, 3, 2) + '/' + RIGHT(@anonDOB, 4)-- Return the result of the functionRETURN @anonDOB |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
crazyco
Starting Member
30 Posts |
Posted - 2012-03-20 : 09:56:17
|
| Could be either or depending on the format we receive the data in. The expected format is yyyymmdd or yyyy-mm-dd as dashes are removed prior to re-formatting.Slashes are retained, so if yyyy-mm-dd is supplied, then yyyy-dmm-dd is returned.Likewise, if yyyy-mm-dd is supplied, yyyymmdd is returned. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
crazyco
Starting Member
30 Posts |
Posted - 2012-03-21 : 05:08:29
|
| I figured it out myself:ALTER FUNCTION [dbo].[anonDOB_date](-- Add the parameters for the function here@DOB varchar(10))RETURNS varchar(10)ASBEGIN-- Declare the return variable hereDECLARE @anonDOB varchar(10), @month varchar(2), @dashes VARCHAR(1)IF(LEN(@DOB) = 10)BEGINSET @DOB = REPLACE(@DOB, '-','')SET @dashes = 'Y'ENDSET @month = SUBSTRING(@DOB,5,2)IF @month IN ('01','02','03','04','05','06','07','08','09','10','11','12')-- Add the T-SQL statements to compute the return value hereIF @month IN ('01','02','03')SET @anonDOB = LEFT(@DOB, 4) + '0201'ELSE IF @month IN ('04','05','06')SET @anonDOB = LEFT(@DOB, 4) + '0501' ELSE IF @month IN ('07','08','09')SET @anonDOB = LEFT(@DOB, 4) + '0801' ELSE IF @month IN ('10','11','12')SET @anonDOB = LEFT(@DOB, 4) + '1101' ELSE SET @anonDOB = NULLELSE SET @anonDOB = NULLIF @dashes = 'Y' SET @anonDOB = LEFT(@anonDOB, 4) + '-' + SUBSTRING(@anonDOB, 5, 2) + '-' + RIGHT(@anonDOB, 2)-- Return the result of the functionRETURN @anonDOB |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-21 : 10:17:26
|
| i didnt understand why you want to return dates as varchar and then worry on format? why not return it as date themselves?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|