문제

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