October 17, 2012

Date Functions – SQL Server vs MySQL

Continuing my series on how same things can be done differently in SQL Server and MySQL, in this post, we will see some Date Functions in SQL Server and MySQL. I have put them in a table format so that it becomes easier for you to follow:
SNo Purpose SQL Server MySQL
1 Find out current date and time Getdate() now()
2 Find out day name of current date datename(weekday ,getdate()) Dayname(now())
3 Find out day of month datename(day,getdate()) dayofmonth(now())
4 Find out day of week datepart(weekday,getdate()) dayofweek(now())
5 Find out day of year datepart(weekday,getdate()) dayofyear(now())
6 Find out year value year(getdate()) year(now())
7 Find out month value month(getdate()) month(now())
8 Find out day value day(getdate()) day(now())
9 Find out last day of month eomonth(getdate()) last_day(now())
10 Find out month name datename(month,getdate()) monthname(now())
11 Find out Time value from seconds cast(dateadd(second,seconds_value,0) as time) sec_to_time(seconds_value)
12 Subtract days from date dateadd(day,-day_value,getdate()) date_sub(now(), interval day_value day)
13 Add days to date dateadd(day,day_value,getdate()) adddate(now(), interval day_value day)
14 Format a date (dd-mm-yyyy) format(getdate(),'d-MM-yyyy') DATE_FORMAT(now(), '%d-%c-%Y');

About The Author

Madhivanan,an MSc computer Science graduate from Chennai-India, works as a works as a Lead Subject Matter Expert at a company that simplifies BIG data. He started his career as a developer working with Visual Basic 6.0, SQL Server 2000 and Crystal Report 8. As years went by, he started working more on writing queries in SQL Server. He now has good level of knowledge in SQLServer, Oracle, MySQL and PostgreSQL as well. He is also one of the leading posters at www.sqlteam.com and a moderator at www.sql-server-performance.com. His T-sql blog is at http://beyondrelational.com/blogs/madhivanan

No comments: