Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Date format into DD/MMM/YYYY format in SQL Server

I have a query in SQL, I have to get a date in a format of dd/mm/yy

Example: 25/jun/2013.

How can I convert it for SQL server?

like image 807
Krish KvR Avatar asked Jun 20 '13 04:06

Krish KvR


People also ask

How do I change the date format in SQL Server?

You can specify the format of the dates in your statements using CONVERT and FORMAT. For example: select convert(varchar(max), DateColumn, 13), format(DateColumn, 'dd-MMM-yyyy')

How convert dd mm yyyy to dd Ye yyyy in SQL?

select date_format(columnname ,'%d-%b-%Y') as format_date from tableName will work for MYSQL.


2 Answers

There are already multiple answers and formatting types for SQL server 2008. But this method somewhat ambiguous and it would be difficult for you to remember the number with respect to Specific Date Format. That's why in next versions of SQL server there is better option.

If you are using SQL Server 2012 or above versions, you should use Format() function

FORMAT ( value, format [, culture ] ) 

With culture option, you can specify date as per your viewers.

DECLARE @d DATETIME = '10/01/2011'; SELECT FORMAT ( @d, 'd', 'en-US' ) AS 'US English Result'       ,FORMAT ( @d, 'd', 'en-gb' ) AS 'Great Britain English Result'       ,FORMAT ( @d, 'd', 'de-de' ) AS 'German Result'       ,FORMAT ( @d, 'd', 'zh-cn' ) AS 'Simplified Chinese (PRC) Result';     SELECT FORMAT ( @d, 'D', 'en-US' ) AS 'US English Result'       ,FORMAT ( @d, 'D', 'en-gb' ) AS 'Great Britain English Result'       ,FORMAT ( @d, 'D', 'de-de' ) AS 'German Result'       ,FORMAT ( @d, 'D', 'zh-cn' ) AS 'Chinese (Simplified PRC) Result';  US English Result Great Britain English Result  German Result Simplified Chinese (PRC) Result ----------------  ----------------------------- ------------- ------------------------------------- 10/1/2011         01/10/2011                    01.10.2011    2011/10/1  US English Result            Great Britain English Result  German Result                    Chinese (Simplified PRC) Result ---------------------------- ----------------------------- -----------------------------  --------------------------------------- Saturday, October 01, 2011   01 October 2011               Samstag, 1. Oktober 2011        2011年10月1日     

For OP's solution, we can use following format, which is already mentioned by @Martin Smith:

FORMAT(GETDATE(), 'dd/MMM/yyyy', 'en-us') 

Some sample date formats:

enter image description here

If you want more date formats of SQL server, you should visit:

  1. Custom Date and Time Format
  2. Standard Date and Time Format
like image 22
Somnath Muluk Avatar answered Oct 19 '22 23:10

Somnath Muluk


I'm not sure there is an exact match for the format you want. But you can get close with convert() and style 106. Then, replace the spaces:

SELECT replace(convert(NVARCHAR, getdate(), 106), ' ', '/') 
like image 123
Jonathan Lonowski Avatar answered Oct 19 '22 23:10

Jonathan Lonowski