Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SERVER DATETIME FORMAT

Studying SQL Server there is something I am not sure of:

A datetime field with the value: 2012-02-26 09:34:00.000

If I select out of the table using:

CAST(dob2 AS VARCHAR(12) ) AS d1

It formats it as: Feb 26 2012

What I am unsure of his how or why SQL Server formats DateTime like that. If you use datetime2 it does not - anyone know why?

like image 900
Ian Avatar asked Feb 08 '13 09:02

Ian


People also ask

How do I change the default datetime format in SQL Server?

The default date format of SQL is mdy(U.S English). Now to change sql server default date format from “mdy”(mm/dd/yyyy) to “dmy”(dd/mm/yyyy),we have to use SET DATEFORMAT command. Before changing the default date time format of sql server lets go through the way to know what format of date time is currently in use.


2 Answers

The default date format depends on the language setting for the database server. You can also change it per session, like:

set language french
select cast(getdate() as varchar(50))
-->
févr 8 2013 9:45AM
like image 152
Andomar Avatar answered Sep 26 '22 06:09

Andomar


try this:

select convert(varchar, dob2, 101)
select convert(varchar, dob2, 102)
select convert(varchar, dob2, 103)
select convert(varchar, dob2, 104)
select convert(varchar, dob2, 105)
select convert(varchar, dob2, 106)
select convert(varchar, dob2, 107)
select convert(varchar, dob2, 108)
select convert(varchar, dob2, 109)
select convert(varchar, dob2, 110)
select convert(varchar, dob2, 111)
select convert(varchar, dob2, 112)
select convert(varchar, dob2, 113)

refernces: http://msdn.microsoft.com/en-us/library/ms187928.aspx

http://www.w3schools.com/sql/func_convert.asp

like image 39
SQLGuru Avatar answered Sep 23 '22 06:09

SQLGuru