Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi Guise,
Can you pls suggest me how we can format date & time in MS SQl server.

Cheers
Everybody
Posted

select CONVERT(varchar(10),getdate(),101) RESULT : 09/23/2011
sql have some formats like 101,104,108....

1 101 U.S mm/dd/yyyy
2 102 ANSI yy.mm.dd
3 103 British / French dd/mm/yy
4 104 German dd.mm.yy
5 105 Italian dd-mm-yy
8 108 - hh:mi:ss
10 115 USA mm-dd-yy
11 111 Japan yy/mm/dd
12 112 ISO yymmdd

http://www.sql-server-helper.com/tips/date-formats.aspx
 
Share this answer
 
This overview[^] should help.
 
Share this answer
 
Comments
RaisKazi 23-Sep-11 7:15am    
Perfect link. 5!
André Kraak 23-Sep-11 7:23am    
Thanks.
Bhavikkumar Chaudhari 3-Dec-15 4:32am    
Good Link...!!!
select CONVERT(varchar,yourdate column,101) from table name ;

you can user 101,102,103,104,105,106,107,108,109.
 
Share this answer
 
Here am Giving a Function am using in my applications

Try this..

SQL
CREATE FUNCTION [dbo].[FormatDateTime](@strDATE varchar(30))
RETURNS datetime
WITH EXECUTE AS CALLER
AS
BEGIN

     RETURN (CAST(
       SUBSTRING(@strDATE,3,2) + '-' +
       SUBSTRING(@strDATE,1,2) + '-' +
       SUBSTRING(@strDATE,5,4) + ' ' +
       SUBSTRING(@strDATE,9,2) + ':' +
       SUBSTRING(@strDATE,11,2) + ':' +
       SUBSTRING(@strDATE,13,2) as datetime));
END;


@strDate = ddmmyyyyHHmiss

Thanks
 
Share this answer
 
This Link is very useful to you for all queries about date & time format in MS SQL Server Queries.

Please mark this if it's helpful to u.
 
Share this answer
 
Hi,

You want to pass datetime directly from sql query ha.

try this below line

SQL
 INSERT INTO TimeTab
                      (id, dtname)
VALUES     (4, '9/23/2011 3:27:51 PM')


Here you can pass in the formatt like

month/date/year

All the Best
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900