format getdate() like 2010.04.14


I want to format getdate() like 2010.04.14.



Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


select convert(varchar,getdate(),102)
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.
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Marten RuneSQL Expert/Infrastructure ArchitectCommented:
'SYSDATETIME' is not a recognized built-in function in SQL 2005, look at Zone

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
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
select * from #temp
drop table #Temp

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

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 :)
Marten RuneSQL Expert/Infrastructure ArchitectCommented:

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

It is already approved. :)
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 :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.