edrz01
asked on
convert Date() to CCYYMMDD format in SQL
I have to convert Date() to CCYYMMDD format using MSSQL. i.e. if today's date yields 01/12/2001 CC date should converted respectively. I have a formula that works in MS Access in a module:
Public Function FormatCCDate(inputdate As Date)
'reformats a date in mm/dd/yyyy format to cyymmdd
Dim lngyear As Long
Dim lngday As Long
Dim lngmonth As Long
lngyear = DatePart("yyyy", inputdate)
lngyear = lngyear - 1900
lngday = DatePart("d", inputdate)
lngmonth = DatePart("m", inputdate)
FormatCCDate = (lngyear * 10000) + (lngmonth * 100) + (lngday)
End Function
This works fine in Access within a query, but I would like to do this in MS SQL 2000.
Public Function FormatCCDate(inputdate As Date)
'reformats a date in mm/dd/yyyy format to cyymmdd
Dim lngyear As Long
Dim lngday As Long
Dim lngmonth As Long
lngyear = DatePart("yyyy", inputdate)
lngyear = lngyear - 1900
lngday = DatePart("d", inputdate)
lngmonth = DatePart("m", inputdate)
FormatCCDate = (lngyear * 10000) + (lngmonth * 100) + (lngday)
End Function
This works fine in Access within a query, but I would like to do this in MS SQL 2000.
Use the function like this
select dbo.FormatCCDate(yourcolum n) from yourtable
select dbo.FormatCCDate(yourcolum
create Function dbo.FormatCCDate(@inputdate Datetime)
returns bigint
as
begin
'reformats a date in mm/dd/yyyy format to cyymmdd
declare @lngyear bigint
declare @lngday bigint
declare @lngmonth bigint
set @lngyear = year(inputdate)
set @lngyear = lngyear - 1900
set @lngday = day(inputdate)
set @lngmonth = month(inputdate)
return( (@lngyear * 10000) + (@lngmonth * 100) + (@lngday))
end;
ASKER
I am new to creating functions in SQL, do I put the code into the SQL statement above the Select statement?
To create the function, just run the code in a query window. Be sure to set the database context first (the dropdown at the top for which database you are in).
In the attached file containing the function, there is a section called "Usage" which gives 3 examples on how to use the function.
Yes, you will use the function in a SELECT statement.
In the attached file containing the function, there is a section called "Usage" which gives 3 examples on how to use the function.
Yes, you will use the function in a SELECT statement.
Yes, run the function code first and then use it in the select as I indicated
Now if you just want the date for instance 01/12/2010 to look like this: 20100112
just do a select like this:
select convert(varchar, yourdatecolumn, 112) from yourtable
No need to create any function
just do a select like this:
select convert(varchar, yourdatecolumn, 112) from yourtable
No need to create any function
ASKER
The results I received is: 20100112
However, if I'm not understanding the users request, i.e. CCYYMMDD is Century, Year, Month, Day. When I use the MS Access code, which is very similar to 'ralmada' the results yield: 1100112
And ralmada, I attempted to create your function...but it errors on 'year', 'month', 'day'
however, I did REM the noteline from ' to /* */ and removed the ; at the end.
However, if I'm not understanding the users request, i.e. CCYYMMDD is Century, Year, Month, Day. When I use the MS Access code, which is very similar to 'ralmada' the results yield: 1100112
And ralmada, I attempted to create your function...but it errors on 'year', 'month', 'day'
however, I did REM the noteline from ' to /* */ and removed the ; at the end.
Sorry missed '@' there
create Function dbo.FormatCCDate(@inputdate Datetime)
returns bigint
as
begin
--reformats a date in mm/dd/yyyy format to cyymmdd
declare @lngyear bigint
declare @lngday bigint
declare @lngmonth bigint
set @lngyear = year(@inputdate)
set @lngyear = lngyear - 1900
set @lngday = day(@inputdate)
set @lngmonth = month(@inputdate)
return( (@lngyear * 10000) + (@lngmonth * 100) + (@lngday))
end;
Still missed one @! In SQL Variables starts with '@'
create Function dbo.FormatCCDate(@inputdate Datetime)
returns bigint
as
begin
--reformats a date in mm/dd/yyyy format to cyymmdd
declare @lngyear bigint
declare @lngday bigint
declare @lngmonth bigint
set @lngyear = year(@inputdate)
set @lngyear = @lngyear - 1900
set @lngday = day(@inputdate)
set @lngmonth = month(@inputdate)
return( (@lngyear * 10000) + (@lngmonth * 100) + (@lngday))
end;
ASKER
ramalda - yes I caught that one too. Your formula does yeild the format that is requested. And once again I applaud you.
ASKER
I am working on the last piece of the puzzle, adding the current Time Stamp, HHMMSS
Current time? or the inputvalue one?
create Function dbo.FormatCCDate(@inputdate Datetime)
returns varchar
as
begin
--reformats a date in mm/dd/yyyy format to cyymmdd
declare @lngyear bigint
declare @lngday bigint
declare @lngmonth bigint
set @lngyear = year(@inputdate)
set @lngyear = @lngyear - 1900
set @lngday = day(@inputdate)
set @lngmonth = month(@inputdate)
return( cast(((@lngyear * 10000) + (@lngmonth * 100) + (@lngday)) as varchar) + convert(varchar, @inputdate, 108) )
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The requirement is to have a separate CCYYMMDD column and Time column of the current time.
ASKER
the time must be HHMMSS
oh so you just do like this, no need to create any function
select replace(convert(varchar, yourdatetimecolumn, 108), ':', '') from yourtable
select replace(convert(varchar, yourdatetimecolumn, 108), ':', '') from yourtable
ASKER
Once again the experts on this team is outstanding. Your solutions have been right on. Thank you once again.!
if you want the current time use the getdate() function
select replace(convert(varchar, getdate(), 108), ':', '') from yourtable
it's accepted already but have a look at this one, no function way to get CCYYMMDD
select convert(varchar(10),conver t(int,year (getdate() )/100 )+1) + right(convert(varchar(10), getdate(), 112),6)
select convert(varchar(10),conver
SELECT LEFT(CONVERT(varchar, GetDate(), 120), 4)
+ Substring(CONVERT(varchar,
+ Substring(CONVERT(varchar,
Or encapsulate this login into a function. See attached script.
fnConvertToCCYYMMDD.txt