I am reading date from the sql table. In sql table date time in the format how client set in regional format. Because of this reading date and splitting that and taking to a format what we want is becoming difficult. Is there any way to restrict sql date format from regional settings date format (means sql not suppose to take regional settings date format).
Now i got the answer:
string Date = Convert.ToDateTime(dateTimeString).ToString("yyyy-MM-dd");
string time = Convert.ToDateTime(dateTimeString).ToString("hh:mm:ss");
//dateTimeString--->my dateTime value from sql database
If you write datetime fields using ISO 8601 there should be no problem.
That way, dates get formatted like "2011-09-26T12:04:00", so there's no misunderstanding possible.
Related
I have a weird problem with sql server 2008. I am trying to save date with dd/MM/yyyy format into sql server 2008, But after inserting date it automatically get converted into MM/dd/yyyy.
Note : my computer clock format is dd/MM/yyyy
My report viewer date text box properties
enter image description here
Date from my database table
enter image description here
my c# code
lbldate.Text = DateTime.Today.ToShortDateString();
date on report
05/04/2017
SQL Server is the Data Layer and as such there is no formatting available; it stores a date as a 4 byte number which is relative to days with 0 = 01/01/1900.
The Application Layer DateTime type is generally an ODBC Canonical representation which basically looks like a class with integer properties for each component (year, month, date, hours, minutes, seconds, milliseconds).
The Presentation Layer is what you actually see, and that is where you should be concerned. When your application calls the ToShortDateString() method, it is calling the display format from the threads current culture, which may or may not reflect the systems settings for Region & Language or Date & Time.
Solution number one is to set the threads current culture, but this would just go to that particular cultures standard display
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Solution number 2 is to just use a custom DateTime format string
lbldate.Text = DateTime.Today.ToString("dd/MM/yyyy");
I would not say this is a "problem" so to speak. This is how SQL handles dates. Your computer clock format is not relevant. To change the format, use CONVERT in your queries. Ex:
SELECT CONVERT(varchar, GETDATE(), 103)
Results: 04/05/2017
SELECT CONVERT(varchar, GETDATE(), 101)
Results: 05/04/2017
The codepage arguments are listed here: https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
edit per your new update: Your C# should look something like this:
DateTime.Now.ToString("dd/mm/yyyy")
What you're seeing is how the query tool presents results and has nothing to do with how Sql Server stored the data. Sql Server actually stores dates in a binary (not readable) format.
This is a good thing. Let Sql Server store dates (and other data) how it wants. You only need to worry about how you show the data to users after you retrieve it, and most of the time the best place to worry about that formatting isn't even in the server at all, but in your client language.
I have a SQL Server 2012 Express database that has been installed on a server in Germany. I created a database and have now realised the date formats are incorrect. I need to show each date as dd/mm/yyyy.
When I run dbcc useroptions (after making some changes to the server), I get the following -
language dateformat
-------------------------
British dmy
When I run select GetDate() in a new query, it shows the date as follows -
2014-08-28 13:53:10.550
The bottom line issue is when I enter 28/08/2014 in to a textbox on a web forms project, it errors
String was not recognized as a valid DateTime.
selP.StartDate = Convert.ToDateTime(tbStartDate.Text);
Any ideas why this is happening? I have created a new user since changing the settings to British and dmy, which still produces the same error.
SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value.
The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.
There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.
The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.
The ISO-8601 format is supported by SQL Server comes in two flavors:
YYYYMMDD for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!
or:
YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME.
This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME!), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.
Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.
The recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible
The proper way to achieve that is to set the culture of your web application.
How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization
IN SQL, you need to cast that string as a DATETIME before inserting or updating the database.
CAST('28/08/2014' AS DATETIME)
You can change the format a date is displayed in using the CONVERT. For example, the following will display the date in UK short format (dd/mm/yyyy)
SELECT CONVERT(varchar(50), GetDate(), 103)
If you are doing this in C#, you need to detect the local culture and cast the string to a datetime as approporate.
//You can set the culture on the current thread:
Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture; // or new CultureInfo("en-GB"); //dd/MM/yyyy
//or you can pass it to the DateTime.Parse method. Something like this:
DateTime startDate = DateTime.Parse(tbStartDate.Text, CultureInfo.InstalledUICulture);
DateTime startDate = DateTime.ParseExact(tbStartDate.Text,"dd/MM/yyyy",System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat)
Actually the GETDATE() function returns the system date and time in the format 'yyyy-mm-dd hh:mi:ss.mmm' irrespective of the Dateformat.Date format for SQL server is in U.S. date format MM/DD/YY, unless a localized version of SQL Server is installed which seems to be the case here.
Use
SET DATEFORMAT <format>
which sets the order of the date parts (month/day/year) for entering datetime or smalldatetime data. Valid parameters include mdy, dmy, ymd, ydm, myd, and dym. The U.S. English default is mdy.
This method allows you use a date format for dates sent to SQL Server of d/m/y, but it is connection dependent. If a new connection is made to SQL Server or if the server is stopped and restarted, the date format will go back to dmy which is default in your case.
selP.StartDate=tbStartDate.Text.ToString("dd/MM/yyyy")
SELECT CONVERT(varchar(50), GetDate(), 103)
SELECT CONVERT(varchar(50), JoiningDate, 103)from Employee where EmpId='1001'
I am reading excel file in my C# .net web application and storing that value in Database. Ony of field in Excel is DateTime. I am storing it in string
string tran_time = Convert.ToString(odr[5]); //tran_time is "03-11-2012 16:08:43"
and then convert it in in DateTime and store it in Database (SQL Server 2008)
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime dateVal = DateTime.ParseExact(tran_time, "dd-MM-yyyy HH:mm:ss", culture);
But the Value Being stored in Database is in format
2012-05-11 13:40:23.000 (yyyy-mm-dd)
and the value in Excel is 05-11-2012 13:40:23 (dd-mm-yyyy)
Date & Month is get replaced.
My Question is How can i store it in Database in Format (YYYY-MM-DD HH:MM:SS:FFF)
But the Value Being stored in Database is in format
No it isn't (assuming that you are using a DATETIME column type). This is just what the SQL tools show you.
A DateTime instance, either in a database or in C# does not have an associated format. It only gets formatted when displayed to the user.
Or use yyyy-MM-dd format. Works like a charm:
string myString = dateVal.ToString("yyyy-MM-dd");
Use dd-mm-yyyy instead of dd-MM-yyyy.
C# code is reading data from database through dynamic queries.
Select ID, TransDate from Table_01
Business logic is processing the data and finally putting back the date into the database again.
INSERT INTO Table_02
( ID,ClosingDate) VALUES
( 1,Convert(DateTime, '27/07/2011 12:00:00 AM',120))
Since date format, i am inserting, is dd/MM/yyyy. Sql Server does not like it and .net throwing this error:-
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value
If i change it to MM/dd/yyyy or yyyyMMdd then it works.
But system regional date time settings can be changed any time by any user, so I am looking for some concrete solution.
What is the best way to handle it?
The converstion style 120 is: yyyy-mm-dd hh:mi:ss(24h). Try using 103 (dd/mm/yyyy) instead.
INSERT INTO Table_02
( ID,ClosingDate) VALUES
( 1,Convert(DateTime, '27/07/2011 12:00:00 AM',103))
You can find the documentation for the styles here.
IMO the best way would be to use parameterized query - then the driver / server takes care of converting data into correct format.
Try setting the dateformat and using Cast:
Set DateFormat DMY
GO
INSERT INTO Table_02( ID,ClosingDate)
Select 1, Cast('27/07/2011 12:00:00 AM' As DateTime)
Stick to yyyyMMdd it will always work regardless of regional settings and date format settings. Beware of yyyy-MM-dd, it will not work when date format is DMY. http://www.sommarskog.se/wishlist.html#YYYYMMDD
I send the registration date parameter to mysql database like "22-12-2010". But my sql date date type is in another format how can I change the date format like "2010-12-22" also I have to insert this into table.
Give code in C#,asp.net code behind either sql query statement!
Use this comprehensive MSDN pages as your guide: Standard Date and Time Format Strings and Custom Date and Time Format Strings.
There are many examples on those pages on how to reformat a date string in C#, and they also provide a good clear explanation on how date formatting works in the DateTime class.
Once you've reformatted your date string in C#, you should be able to pass it on down without needing to use SQL to reformat it.