format getdate() like 2010.04.14

Hello,

I want to format getdate() like 2010.04.14.

Thanks

Regards

Bibi
bibi92Asked:
Who is Participating?
 
Marten RuneSQL Expert/Infrastructure ArchitectCommented:
Bawer:
'SYSDATETIME' is not a recognized built-in function in SQL 2005, look at Zone

RiteshShah:
Nice peace of code. Did a small modification, added a column showing the code to create each example, and added three common situations in a second select statement.
1. How to get today at 00:00:000 time
2. How to get today at any time, example uses 18:00 hours
3. How to get next hour on the hour

Hope your'e not offended that I post the code here. If you like my modifications be my guest to use it on your blog.

Sincerely Marten
set nocount on
DECLARE @Date int
create table #temp
(
Type int,
date varchar(50),
Code Varchar(50)
)
set @Date=131
While @Date>=0
begin
if((@Date>=0 and @date<15) or (@Date>=20 and @date<26) or (@Date>=100 and @date<=114) or @Date in (120,121,126,130,131))
insert into #temp
select convert(varchar,@date),convert(varchar,getdate(),@Date),'SELECT convert(varchar,getdate(),' + cast(@Date as varchar) + ')'
set @Date=@Date-1
end
select * from #temp
drop table #Temp

SELECT
Cast(CONVERT(VARCHAR,getdate(),110) as datetime) AS 'Today 00:00' --No Formatting
,convert(varchar,dateadd(hh,18,Cast(CONVERT(VARCHAR,getdate(),110) as datetime)),20) AS 'Today 18:00' -- 20 = format YYYY-MM-DD HH:NN:SS
,convert(varchar,dateadd(hh,datepart(hh,getdate())+1,convert(varchar,getdate(),10)),20) as 'Now next hour on the hour'

Open in new window

0
 
Marten RuneSQL Expert/Infrastructure ArchitectCommented:
print convert(varchar,getdate(),102)

or

select convert(varchar,getdate(),102)
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
RiteshShahCommented:
SQL Server is really very rich with so many different kind of date/time format. You have to use format # 102 for the date you want. here in my blog, you can find all different kind of format you can use.

http://www.sqlhub.com/2009/04/list-of-all-available-datetime-format.html
0
 
RiteshShahCommented:
martenrune,

indeed it is good that you, yourself place this code on my blog with due credit to you. you have suggested the modification so credit should go to you only, not me :)
0
 
Marten RuneSQL Expert/Infrastructure ArchitectCommented:
RiteshShah:

It's up on your blog, after due aproval ofcourse.
I'll bookmark your blog and will come back at a convienient time.

//Marten
0
 
RiteshShahCommented:
It is already approved. :)
0
 
RiteshShahCommented:
script you selected as an answer was originally written by me so I was at least expecting assisted points. seems like martenrune is luckier than me :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.