Convert Date format into DD/MMM/YYYY format in SQL Server
Asked Answered
R

12

65

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?

Raphaelraphaela answered 20/6, 2013 at 4:44 Comment(3)
possible duplicate of #15017800Johniejohnna
sql-server-helper.com/tips/date-formats.aspxRaphaelraphaela
i got the format as 25/06/2013 but what i need is 25/Jun/2013Raphaelraphaela
C
70

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), ' ', '/')
Cistern answered 20/6, 2013 at 4:50 Comment(1)
Just to mention, code 103 directly gives requested date format with just numbers.Nellnella
B
50

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
Bigener answered 23/8, 2016 at 9:22 Comment(0)
C
31

we can convert date into many formats like

SELECT convert(varchar, getdate(), 106)

This returns dd mon yyyy

More Here This may help you

Commensurable answered 20/6, 2013 at 4:51 Comment(0)
S
11

The accepted answer already gives the best solution using built in formatting methods in 2008.

It should be noted that the results returned is dependent on the language of the login however.

SET language Russian

SELECT replace(CONVERT(NVARCHAR, getdate(), 106), ' ', '/') 

Returns

06/апр/2015

at the time of writing.

For people coming across this question on more recent versions of SQL Server a method that avoids this issue - and the need to REPLACE is

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

On 2005+ it would be possible to write a CLR UDF that accepted a DateTime, Formatting Pattern and Culture to simulate the same.

Soule answered 6/4, 2015 at 15:40 Comment(0)
A
10

Try using the below query.

SELECT REPLACE(CONVERT(VARCHAR(11),GETDATE(),6), ' ','/');  

Result: 20/Jun/13

SELECT REPLACE(CONVERT(VARCHAR(11),GETDATE(),106), ' ','/');  

Result: 20/Jun/2013

Aurlie answered 20/6, 2013 at 4:51 Comment(0)
L
2

Try this

select convert(varchar,getdate(),100)

third parameter is format, range is from 100 to 114, any one should work for you.

If you need date in dd/mmm/yyyy use this:

replace(convert(char(11),getdate(),113),' ','-')

Replace getdate() with your column name. This worked for me.

Laxation answered 14/9, 2013 at 5:8 Comment(0)
K
2

You can use this query-

SELECT FORMAT (getdate(), 'dd/MMM/yy') as date

Hope, this query helps you.

Thanks!!

Kirkendall answered 20/6, 2019 at 13:9 Comment(0)
W
1

Try this :

select replace ( convert(varchar,getdate(),106),' ','/')
Withdraw answered 4/12, 2015 at 11:23 Comment(0)
M
0

Anyone trying to manually enter the date to sql server 'date type' variable use this format while entering :

'yyyy-mm-dd'

Misogamy answered 7/9, 2017 at 9:59 Comment(0)
M
0

Just format after convert to a date (tested in SQL Server v.2017)

dd/mon/yyyy
Select Format(cast('25/jun/2013' as date),'dd/MMM/yyyy')

dd/mm/yyyy
Select Format(cast('25/jun/2013' as date),'dd/MM/yyyy')

dd/Month/yyyy
Select Format(cast('25/jun/2013' as date),'dd/MMMM/yyyy')
Marniemaro answered 30/7, 2020 at 5:53 Comment(0)
P
0

Simply get date and convert

Declare @Date as Date =Getdate()

Select Format(@Date,'dd/MM/yyyy') as [dd/MM/yyyy] // output: 22/10/2020
Select Format(@Date,'dd-MM-yyyy') as [dd-MM-yyyy] // output: 22-10-2020

//string date
Select Format(cast('25/jun/2013' as date),'dd/MM/yyyy') as StringtoDate // output: 25/06/2013

Source: SQL server date format and converting it (Various examples)

Pronunciamento answered 22/10, 2020 at 11:4 Comment(0)
P
0

There are already very good answers available like:

select replace(convert(varchar,getdate(),106),' ','/')

But, I want to add a comprehensive list of codes for all the other formats:

enter image description here


Here is the the link:

https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/

Pusillanimity answered 19/6, 2023 at 6:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.