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.
Related
I am using Sybase DB and using dblib interface( C++ Interface) to connect and pass commands to Sybase DB.
I have one stored procedure added to Sybase DB. Below is the signature of stored procedure:
create procedure process_write #id varchar(35), #pTime datetime,#status tinyint
and I am calling this stored procedure with below :
process_write '000000100', '22/9/2022 10:18:37', 1
Now when I run my code I do not see error on console and the stored procedure executes successfully, But when I run this in isql command prompt, I get below error:
Msg 247, Level 16, State 1:
Server 'ABCXYZ', Procedure 'process_write':
Arithmetic overflow during implicit conversion of VARCHAR value '22/9/2022 10:18:37' to a DATETIME field .
(return status = -6)
I am not able to figure out how this is working in code but failing in isql console?
tl;dr
ASE defaults to processing strings of the format X/Y/Z as M/D/Y and this likely explains why your isql session is generating an error (ie, your isql session is running with mdy as its default dateformat). My guess is that somewhere in your dblib/C++ code you've either modified the dateformat or the language used by the dblib/C++ session which in turn insures your proc call works (ie, no conversion errors are generated).
I don't work with dblib/C++ so I don't know if you need to modify a db connection attribute or if you just issue a T-SQL command upon successful connection, eg:
set dateformat 'dmy'
In Sybase ASE when processing strings as dates it's necessary to tell ASE the ordering of the date components in strings like X/Y/Z.
From a strictly T-SQL point of view there are two set options that can tell ASE how to interpret X/Y/Z as a date:
set dateformat <format> - where <format> is one of 'mdy', 'myd', 'ymd', 'ydm', 'dmy' and 'dym'; default is 'mdy'
set languange <language> - where <language> is going to be based on what languages you've loaded into ASE; default is 'us_english' which causes the dateformat to default to 'mdy' [I don't have details on if/how individual languages may modify the dateformat so you would need to run some tests in your environment]
NOTES:
see T-SQL Users Guide: Date Formats and Reference Manual: Commands: set for more details.
to see the current dateformat in use for your session: select get_appcontext('SYS_SESSION','dateformat')
Demonstrating the use of set dateformat with a stored proc:
create proc testp
#pTime datetime
as
select #pTime
go
-------------
select get_appcontext('SYS_SESSION','dateformat')
go
---
mdy <<<--- ASE default
exec testp '22/9/2022 10:18:37'
go
Msg 247, Level 16, State 1:
Server 'ASE400', Procedure 'testp':
Arithmetic overflow during implicit conversion of VARCHAR value '22/9/2022 10:18:37' to a DATETIME field .
----------------
set dateformat 'dmy'
go
select get_appcontext('SYS_SESSION','dateformat')
go
---
dmy
exec testp '22/9/2022 10:18:37'
go
-------------------
Sep 22 2022 10:18AM
Weird situation during .csv file import via SSIS into a SQL Server table.
CSV date 25-May-46 is imported into a NVARCHAR column.
When selecting that column on
REPLACE(CONVERT(NVARCHAR(10), CAST(date_of_birth AS DATE), 111), '/', '-')
I get '2046-05-25'
When converting that column to a date in Excel, I get '1946-05-25'
Does anyone have an idea what may be causing this?
You should check the "Two Digit Year Cutoff" parameter at the SSMS - rightclick on your SQL Server - Server Properties - Advanced Settings.
Default value is 2049, and you have expected result for this setting.
Set it to 2045, to get '1946-05-25' on SQL Server.
Also you can check/set this value like that:
EXEC sys.sp_configure N'two digit year cutoff' --to check
EXEC sys.sp_configure N'two digit year cutoff', N'2045' --to set
GO
RECONFIGURE WITH OVERRIDE
GO
Hope this helps.
It could be a mismatch in the date format in the csv vs your machines localization. I reckon you should review the date format and check that it matches with you machines settings
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
I'm having a lot of difficulty with locale's in a particular instance of SQL Server 2008 R2 Express.
I'm in the UK and the following fails:
SELECT CAST('2012-12-31' AS DATETIME)
Error message:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The Windows server locale is British English. My login locale is British English. Collation 'if that matters' is Latin1_General_CI_AS.
The database 'language' is English (United States) but then this is the same as another instance on a different server and the above SQL doesnt fail.
Any thoughts?
For the user making the database connection -- the SQL user -- set the language to English.
This is a setting specific to the SQL user of the connection issuing the query
One way to check if this is a problem... Run this in Management Studio and login as the SQL user who issues the query
SET LANGUAGE English
SELECT CAST('2012-12-31' AS DATETIME)
If this works, set the default language of the SQL user appropriately
Don't use YYYY-MM-DD for date literals, always use YYYYMMDD. This will never fail, regardless of locale, dateformat settings, language settings, regional settings, etc:
SELECT CAST('20121231' AS DATETIME);
A worthwhile read perhaps:
Bad habits to kick : mis-handling date / range queries
You should explicitly define the date format on your convert, in this case is 120:
SELECT CONVERT(DATETIME,'2012-12-31',120)
You can take a look at this page to see more date formats:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
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'