Вопрос

If you want to set the dateformat to some style say, ddMMyyyy, in sql server we can use the following statement:

SET DATEFORMAT dmy

My question is how to know before hand that this is the format set?

The program I am writing needs to determine, if the above is the actual datetimeformat, else set it and continue with rest of execution.

How is this possible? Else is my only approach, to set it to my desired format and continue with execution?

I hope setting this won't affect other sessions (connections)?

Это было полезно?

Решение

To check the date format use

DBCC useroptions

SET DATEFORMAT will only effect the current session

MSDN Link

Другие советы

the below code, get the current session dateformat, sets it and test if it is working

-- get the current session date_format
select date_format
from sys.dm_exec_sessions
where session_id = @@spid

-- set the dateformat for the current session
set dateformat ymd

-- this should work
select cast('2017-08-13 16:31:31'  as datetime)

Starting with SQL 2008, the current date format setting can be determined by SPID/session_id within the dm_exec_requests dynamic management view:

SELECT r.date_format
FROM master.sys.dm_exec_requests r
WHERE r.session_id = @@SPID;

Permissions:
If the user has VIEW SERVER STATE permission on the server, the user is able to see all executing sessions on the instance of SQL Server; otherwise, the user will see only the current session.

It seems there is not a global @@ variable to display DATEFORMAT. (?)

The list of @@ variables is documented here: https://docs.microsoft.com/en-us/sql/t-sql/functions/configuration-functions-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15

As per other answers here, Can use:

DBCC useroptions

and

select date_format
from sys.dm_exec_sessions
where session_id = @@spid

You should convert datetime to desirable format by convert function,

Try this:

SELECT convert(varchar(10),getdate(),103)

OR

SELECT replace(convert(varchar(10),getdate(),103),'/','')

yo can get more information on: http://msdn.microsoft.com/en-us/library/ms187928.aspx

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top