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://learn.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
Related
I want a result 2000-02-05 with below query in snowsql.
alter session set TWO_DIGIT_CENTURY_START=2000;
select cast ('05-FEB-00' as date) from dual;
But I am getting 0001-02-05.
I am using existing script to load date in snowflake which works for oracle. I know I can get expected result using to_date function but I don't want to do so. If I have to then I have change many place in script which is hectic.
I want solution using cast function. Do anyone know what is happening here?
You first need to specify the non-default date format for your input data. In the case of the example above:
alter session set date_input_format = 'DD-MON-YY';
Then
alter session set TWO_DIGIT_CENTURY_START=2000;
select cast ('05-FEB-00' as date) from dual;
yields:
2000-02-05
Is it possible to change dateformat from DMY to YMD only for new connections?
I tried a LOGON trigger using SET DATEFORMAT YMD but always the default DMY is back.
So I want to keep the default DMY into database however for new sessions (connections) I want to change to YMD.
I believe it is tied to the language of your server which you can look up like this:
USE [master]
GO
SELECT * FROM [syslanguages]
then you need to run sp_config with the langid from the first query
EXEC sp_configure 'default language', 'langid'
GO;
RECONFIGURE
GO;
I had the same problem and I found the solution.
SQL management studio gets the data format from your local windows settings.
Click on the clock (windows)
Click on Change data and time settings...
Click on Change calendar settings
Go to Date tab
Change the Short date format to: yyyy-MM-dd
that is it.
I would like to know how I can translate this Oracle line into SQL Server code:
to_timestamp_tz('18/08/14 09:43:29,262000000 +02:00','DD/MM/RR HH24:MI:SSXFF TZR')
You can convert() to datetimeoffset() by replacing the comma with a period, and specifying a style that matches day first (if not already implicitly set by session settings and/or session language settings):
declare #str varchar(40) = '18/08/14 09:43:29,262000000 +02:00';
select convert(datetimeoffset(7),replace(#str,',','.'),4)
Or setting set dateformat dmy:
set dateformat dmy;
select convert(datetimeoffset(7),replace(#str,',','.'))
rextester demo: http://rextester.com/GSGL61143
Is it possible to change the datetime for a particular database on SQL Server?
Is it tied to the operating system's date/time?
We wish to simulate a future datetime for testing purposes i.e. so the GETDATE() returns a date in the future.
It's got to be in a semi-production (staging) environment so unfortunately changing the OS date / time isn't an option for us.
In an ideal world we'd spin up a virtual server, but also not really an option at the moment.
As stated, by others, No.
A really hacky workaround, would be be to write your own function to return the date you want and have it return GETDATE() when you're done testing, and call that function instead. There's probably some slight overhead in doing this, but it'll do what you need.
Unfortunately it is tied to the OS date and time. See here: http://msdn.microsoft.com/en-us/library/ms188383.aspx
This value is derived from the operating system of the computer on
which the instance of SQL Server is running.
You can always use this and adjust accordingly:
SELECT getutcdate()
Please see below for more information
StackOverflow Question
But there is no way to change the results from a GETDATE() without changing the server's date.
Added:
You could do a EXEC xp_cmdshell 'DATE 10/10/2011' if you wish... but it's not advised.
Another workaround I've had some success with is to add an INSTEAD OF trigger to any table where a GETDATE() value is being inserted and modify it there e.g.:
ALTER TRIGGER [dbo].[AccountsPayableReceivable_trg_i] ON [dbo].[AccountsPayableReceivable]
INSTEAD OF INSERT
AS
SET NOCOUNT ON
SELECT *
INTO #tmp_ins_AccountsPayableReceivable
FROM INSERTED
UPDATE #tmp_ins_AccountsPayableReceivable
SET dtPaymentMade = '01-Jan-1900'
WHERE dtPaymentMade between dateadd(ss, -5, getdate()) and dateadd(ss, +5, getdate())
INSERT INTO AccountsPayableReceivable
SELECT *
from #tmp_ins_AccountsPayableReceivable
(Incidentally, the where clause is there because my test script autogenerates these triggers, adding an update for every datetime column, so I only want to update those that look like they are being inserted with a GETDATE() value.)
I believe you can create a user function that would do the calculation for you and apply that.
http://msdn.microsoft.com/en-us/library/ms186755.aspx
Also, it can be used as the default value for a column.
Bind a column default value to a function in SQL 2005
I need to change the date format from US (mm/dd/YYYY) to UK (dd/mm/YYYY) on a single database on a SQL server machine.
How can this be done?
I've seen statements that do this for the whole system, and ones that do it for the session, but I can't change the code now as it will have to go through QA again, so I need a quick fix to change the date time format.
Update
I realize that the date time has nothing to do with how SQL Server stores the data, but it does have a lot to do with how it parses queries.
I'm chucking raw data from an XML file into a database. The dates in the XML file are in UK date format.
You could use SET DATEFORMAT, like in this example
declare #dates table (orig varchar(50) ,parsed datetime)
SET DATEFORMAT ydm;
insert into #dates
select '2008-09-01','2008-09-01'
SET DATEFORMAT ymd;
insert into #dates
select '2008-09-01','2008-09-01'
select * from #dates
You would need to specify the dateformat in the code when you parse your XML data
In order to avoid dealing with these very boring issues, I advise you to always parse your data with the standard and unique SQL/ISO date format which is YYYY-MM-DD. Your queries will then work internationally, no matter what the date parameters are on your main server or on the querying clients (where local date settings might be different than main server settings)!
You can only change the language on the whole server, not individual databases. However if you need to support the UK you can run the following command before all inputs and outputs:
set language 'british english'
Or if you are having issues entering datatimes from your application you might want to consider a universal input type such as
1-Dec-2008
Although you can not set the default date format for a single database, you can change the default language for a login which is used to access this database:
ALTER LOGIN your_login WITH DEFAULT_LANGUAGE=British
In some cases it helps.
If this really is a QA issue and you can't change the code. Setup a new server instance on the machine and setup the language as "British English"
Use:
select * from mytest
EXEC sp_rename 'mytest.eid', 'id', 'COLUMN'
alter table mytest add id int not null identity(1,1)
update mytset set eid=id
ALTER TABLE mytest DROP COLUMN eid
ALTER TABLE [dbo].[yourtablename] ADD DEFAULT (getdate()) FOR [yourfieldname]
It's working 100%.
You do realize that format has nothing to do with how SQL Server stores datetime, right?
You can use set dateformat for each session. There is no setting for database only.
If you use parameters for data insert or update or where filtering you won't have any problems with that.
For SQL Server 2008 run:
EXEC sp_defaultlanguage 'username', 'british'