How do I read a date from SQL Server to a datetime variable in C# ?
DateTime sDate = (read["notDate"]);
I get error
Cannot convert object to a system.datetime [are you missing a cast?]
Edit: the SQL Server uses a Date datatype for this column.
Then how will format the sDate to UK format - I was looking at
format("dd/MM/yyyy")
Using ASP.NET, C#, SQL Server Express.
Thanks for your help.
you need to cast it into a date.. this should do the job
DateTime sDate = (DateTime)read["notDate"];
To then format it into a dd/MM/yyyy you can do
string formattedDate = sDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Related
I have problem. I have MySQL database and I use DATE type for my date column. I want to save date without time only.
In database date is saved like this 06/02/1999 for example.
But when I try to take it with dapper
var test = connection.QueryFirstAsync<string>(#"SELECT BirthDate FROM Students");
Then it returns 06/02/1999 00:00:00
How can I fix that? I want string without that time that shouldn't even be there in first place.
Thank you very much for answers
Simple (maybe naive) solution is this one:
string date = "06/02/1999 00:00:00";
DateTime dateTime = DateTime.Parse(date);
DateOnly dateOnly = DateOnly.FromDateTime(dateTime);
Note: DateOnly applies to .NET 6, .NET 7 Preview 6
I am uploading data into BigQuery Cloud by my c# Application. I have column in BigQuery table 'ForDate' with datatype 'Date'.
But in c# datatype is 'datetime'(As date datatype not supported in c#)
I am getting Below error on uploading data to bigquery:
{Invalid date: '2017-01-02T00:00:00' Field: ForDate; Value: 2017-01-02T00:00:00}
What could be the workaround to upload date with date part only and ignore time part from it?
This is working for me:
string currentdatetime = DateTime.Now.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
From:
How can I format DateTime to web UTC format?
Import said column as a string, parse in BigQuery to a timestamp value later.
SELECT TIMESTAMP('2017-01-02T00:00:00')
The date field in the BigQuery schema should be marked as a TimeStamp not as a DateTime field. Then you can simply insert the row with:
bqr.Add(attribute, date.ToUniversalTime());
I know its been a while since this question was asked, but for me none of the answers was correct. It seems that the BigQuery NuGet doesn't like date times with specified kind (e.g UTC), and ToUniversalTime() produces an UTC kind.
The only trick that worked for me was to create a new instance of DateTime with none (Unspecified) kind.
var now = DateTime.UtcNow;
var dt = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond);
You can use:
BigQueryDateTimeExtensions.AsBigQueryDate(DateTime.UtcNow)
From: Class BigQueryDateTimeExtensions
For DateTime use:
DateTime.UtcNow.ToString("s")
SQL:
select *
from tvideoconference
where del = 'false'
and iduserpatient = 0 and startdate >= N'18.02.2013 20:37:07'
order by startdate
Error:
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
I got this error, when I tried to use method below in de-DE session culture. What is more, in pl-PL and en-US culture, this methods works perfect.
public static DataSet getSpecialistConfs(int iduserspecialist)
{
DateTime? today = DateTime.Now;
today = today.Value.AddHours(-today.Value.Hour).AddMinutes(-(today.Value.Minute + 1));
string sql = "select * from tvideoconference where del='false' and startdate >=N'" + today.Value + "' and iduserspecialist=" + iduserspecialist;
sql += " order by startdate ";
return Tools.SQLTools.getDataSet(sql);
}
How can I resolve it? I tried with many solutions (substrings, date format), with the same effect..
How can I resolve it?
Use parameterized SQL in the first place. Don't include the values in your query itself; use parameters and set the values of those parameters.
It's unclear what Tools.SqlTools is, but you really, really should use parameterized SQL. That way you won't be converting the DateTime value into a string to start with, so cultures and formatting can't get in the way. Additionally, you won't be vulnerable to SQL injection attacks...
(Additionally, it's not at all clear why you're using DateTime? instead of DateTime - it's not like DateTime.Now can be null... and you should consider using DateTime.Today instead... or DateTime.UtcNow.Date.)
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 date format 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.
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.
i have sqlCE DataBase, i have Tdate field (datetime)
i notice that his format is: MM/DD/YYYY
in my C# program i work with date in: DD/MM/YYYY format.
how i can insert to my sqlCE data base my C# format ?
i have Tdate field (datetime) i notice
that his format is: MM/DD/YYYY
No, the datetime field doesn't have any format. The format is determined when the datetime value is converted into a string after reading it from the database.
The same works when you insert a datetime value in the database, you use a datetime value, not a string. If you supply a string value to the database, it will try to parse it into a datetime value. Whatever format you use when you insert the datetime value, that doesn't affect how the value is returned when you read it, as only the value is stored in the database not the format.
When you insert the value you should use a parametererized query so that you supply the date as a DateTime value, not as a formatted string in the query.
When you read the data from the database, you get a DateTime value. You can either set the culture of the application to control how the default conversion from DateTime to string is done, or use a specific culture or format when you convert it.
Maybe there's some function, i'm not sure. My variant is:
string DateString = "25/10/2009";
DateTime dateTime = DateTime.Parse(DateString);
string sqlString = datetime.Month+"/"+dateTime.Day+"/"+dateTime.Year;
I think you don't need to worry about the Date Format.