473,378 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

DateTime Automatic Formatting

I want to write some SQL which results in an automatic conversion of a
datetime to a string in a format suitable for the Language of the
connection (either by explicitly setting the Language in the
connection string, or by setting the default language in for the user
used for the connection.)

The casting from string to datetime uses the language setting:

Data Source=localhost\sqlexpress;Initial Catalog=master;Persist
Security Info=True; Language =BRITISH ENGLISH; Trusted_Connection=yes;
Application Name = Test Application;

select cast('13/01/2008' as datetime) --WORKS as expected
select cast('01/13/2008' as datetime) --FAILS as expected

and

Data Source=localhost\sqlexpress;Initial Catalog=master;Persist
Security Info=True; Language =ENGLISH; Trusted_Connection=yes;
Application Name = Test Application;

select cast('13/01/2008' as datetime) --FAILS as expected
select cast('01/13/2008' as datetime) --WORKS as expected

but implicit casting the other way ignores the "Language setting" (ie
the format is the same for both):

BRITISH ENGLISH
select cast(cast('12/01/2008' as datetime) as nvarchar(max)) --Jan 12
2008 12:00AM
select convert(nvarchar(max),cast('12/01/2008' as datetime)) --Jan 12
2008 12:00AM

ENGLISH
select cast(cast('12/01/2008' as datetime) as nvarchar(max)) --Dec 1
2008 12:00AM
select convert(nvarchar(max),cast('12/01/2008' as datetime)) -- Dec 1
2008 12:00AM

Is it possible to tell SQL Server "For language w convert datetimes to
strings using format x, but for language y use format z?"

Regards,

Dan




Mar 12 '08 #1
2 3308
Seems to work just fine to me when the correct language is set:

SET LANGUAGE british
SELECT CAST(CAST('12/01/2008' AS DATETIME) AS NVARCHAR(30))
Result:
Jan 12 2008 12:00AM

SELECT CONVERT(NVARCHAR(30), CAST('12/01/2008' AS DATETIME))
Result:
Jan 12 2008 12:00AM
SET LANGUAGE us_english
SELECT CAST(CAST('12/01/2008' AS DATETIME) AS NVARCHAR(30))
Result:
Dec 1 2008 12:00AM

SELECT CONVERT(NVARCHAR(30), CAST('12/01/2008' AS DATETIME))
Result:
Dec 1 2008 12:00AM
I am not sure if there is a difference when setting the language via the
connection string.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Mar 12 '08 #2
(br****@hotmail.com) writes:
I want to write some SQL which results in an automatic conversion of a
datetime to a string in a format suitable for the Language of the
connection (either by explicitly setting the Language in the
connection string, or by setting the default language in for the user
used for the connection.)
I'm sorry, but why on Earth would you do that? Return datetime data as
binary values to the client and do the formatting there.

The datetime and language settings in SQL Server applies only for how
strings are interpreted. They do not affect output.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ed | last post by:
Hi there, My problem is the following: When I assign a custom formatted Date & Time to a Date variable it loses it’s formatting. Ex. 2005-06-07 15:46 now when I assign this to a variable of...
15
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" ...
26
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a miniute to it.How can i do that? by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004 12:00 AM'...
1
by: Russell Mangel | last post by:
I am using VS2005. When I paste source code into the code window, VS2005 formats like the following: public MsgRecipientReader() : base() { }
0
by: H5N1 | last post by:
Hi there I know that a problem of different datetime strings formatting between asp.net and ms sql has been covered here widely, but what I couldn't find is the 100% safe way of getting datetime...
5
by: cpnet | last post by:
I'm able to pass string parameters with no problems to a LocalReport being displayed by the ASP.NET ReportViewer control. However, my report also has a DateTime parameter. ...
1
by: Untitled | last post by:
I have an application that stores all DateTime inforamtion in UTC. However, this application will be accessed from arround the world. I would like my users to be able to specify their prefered...
3
by: =?ISO-8859-2?Q?W=B3adys=B3aw_Bodzek?= | last post by:
Hi, I want to change 2 default behaviors of ms-sql server within a connection. 1. All varchar (etc.) fields should be returned as UTF-8. I know about field attribute collation and convert...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.