Date format in sql server 2008 - c#

I have date in format 2013-08-05 12:45:56. But I want to store this in format 2013-08-05 00:00:00 in database.
The thing I want is, I don't want to save the time in database.
I am using:
cmd.Parameters.AddWithValue("#VoucherDate", VoucherDate.Date);
But still it is saving the time. Any suggestions?

If you're using SQL 2008 or above, then you can use the Date data type
http://msdn.microsoft.com/en-us/library/bb675168.aspx
If you want to get the Date part only in C#, then you can use the Date property e.g.
var fullDate = DateTime.Now;
var dateOnly = fullDate.Date;
But, if you're only looking to store the date portion, then consider changing the SQL datatype.

Use the date column type instead of datetime
http://technet.microsoft.com/en-us/library/bb630352.aspx

Even you have Date, or DateTime column in database, when you read data back to your program you can format the display date by giving correct DataTime format string.
like dt.ToString("yyyy-MM-dd) , then you will not get any time as output

Once I too had a similar problem, with the help of this answer I fixed it.
Your C# code:
cmd.Parameters.AddWithValue("VoucherDate", VoucherDate.Date);
Tune your SQL query like below mentioned,
DATEADD(dd, 0, DATEDIFF(dd, 0, #VoucherDate))

Try using:
VoucherDate.Date.ToShortDateString();

Related

Formatting sql datetime to MM/dd/yyyy using C#

I have a date stored in SQL. When I get the value back I want to format as MM/dd/yyyy, but I'm getting values like 2020-09-01 instead.
AccountHolderRenewalDate = a.AccountRenewalDate.ToString("MM/dd/yyyy")
Both AccountHolderRenewalDate and a.AccountRenewalDate are strings. a.AccountRenewalDate holds the date I'm after.
What would I be doing wrong here?
This should do the trick - I'm assuming that your DB is a Date column so I didn't use DateTime.TryParse:
DateTime.Parse(AccountHolderRenewalDate).ToString("d");
If you want 09/01 instead of 9/1 use
.ToString("MM/dd/yyyy")
instead

What's the correct SQL query to retrieve date field without time? C# MS Access

I have a Microsoft Access Database with a Date column listing dates in the format MM/DD/YYYY. I'm using the query :
SELECT Date FROM Table
This returns the date in C# in the format MM/DD/YYYY HH:MM:SS.
What would be the correct query to retrieve just the date and not the time?
You can just change it in C# by doing
yourDateTimeVariable.ToShortDateString();
To format date in MS Access you can use FORMAT function. In your case it will look like this:
SELECT FORMAT(Date, 'Short Date') FROM Table
Edit: Note that above example returns date in short date format as it is set up in system settings. To be more specific you can also use custom format like FORMAT(Date, 'yyyy/mm/dd').
I'm not quite sure what you mean here with C#. But if you want the query to return it in that format, you could do something like
SELECT CONVERT(VARCHAR(10), t.Date, 101) FROM Table t
read https://msdn.microsoft.com/en-us/library/ms187928.aspx about convert and it's formats.
If you want to have c# do it, you can use the DateTime.Format() see https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
In C# (since this question is tagged C#)
You have Date property of DateTime which returns Date.
var newdate = dateobject.Date;
or you can create new Date by passing date, month, year values to DateTime constructor.
var newdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
Please note, in both cases what you get is DateTime object with time set to 12:00 AM. in C# there is nothing like Date only, it is always associated with time.

Convert date type in c# to match sql server 2008 date type

I have retrieved a date from an application and stored it in a DateTime Variable. The format of date is dd/mm/yyyy.
I now want to update a column (with datatype date (yyyy/mm/dd)) in a sql server 2008 database with this date
I have tried the below code, but it's giving me an exception "string was not recognized as valid datetime". Please help to solve this problem.
DateTime date = calExpirydate.SelectedDate;
DateTime date1 = DateTime.ParseExact(date.ToString(), "YYYY/MM/DD", CultureInfo.InvariantCulture);
You don't need to convert it at all if you use parameters (and you should be).
A rough example:
DateTime date = DateTime.Now;
SqlCommand command = new SqlCommand();
command.CommandText = "INSERT INTO table (date) VALUES (#date)";
command.Parameters.Add("#date",SqlDbType.DateTime).Value = date;
I'm using SQL Server here, however the concept is similar across most ADO.NET providers.
Your DateTime variable in the framework is stored in one basic format. The way it appears is just formatting off of the .ToString. It's saying give me your date bit make it look like this. Sql server is similar, it understands the date time variable regardless of how it appears.
If you pass your DateTime as exactly how it is in the framework it will save it correctly. The date and time isn't changing just how it's displayed. Your try parse isn't working though because it's not able to recognize the partial string you're giving it.
You don't even need a new date time variable to see it the way you want. Even if you're successful you will have identical date time variables.
yyyy/MM/dd should be correct
DateTime string format

Cannot pass date into stored procedure

Trying to pass date into a stored procedure as follows:
#dateRegistered = '28/04/2012'
But it keeps telling me:
Error converting data type varchar to date.
It works if I do it as:
#dateRegistered = '04/28/2012'
But this is not the format I want to use.
I have run the following query to set the format:
SET DATEFORMAT dmy
Why isn't it working?
Edit: Here is how I did it:
In my stored procedure, I put:
SET DATEFORMAT dmy
Then from code, I pass it as:
myCommand.Parameters.Add("#dateRegistered", SqlDbType.Date);
myCommand.Parameters["#dateRegistered"].Value = DateTime.Now.ToString("dd/mm/yyyy");
I passed it as string to ensure that even if my computer (or the server) has a different date format, it will always use dmy.
Edit Edit:
I passed it as datetime.now. It seems to transform it to the default format when I send it, and then transform it back to my computer format when I read it. Unsure how exactly this happens, but it seems to be working fine.
It looks like the parameter is a date (in the database), while you try to pass a string (which corresponds to varchar in the database). If you instead pass a .NET DateTime object it should work as expected.
Save yourself the headache and use an unambiguous date format - 20120428 (YYYYMMDD) or 2012-04-28.
As an aside: you mention you're using C# (based on the tags) - how are you using ADO.NET? Or is it Linq to SQL / EF / some other ORM?
Edit:
Ok, use the CONVERT function with an appropriate parameter indicating the format of your date string
EG:
declare #D datetime
set #D = CONVERT(datetime, '04/28/2012', 101)
print #D
101 is US standard format mm/dd/yyyy.
Using convert will ensure it ALWAYS works regardless of any environment settings such as the date order.
If you call set dateformat dmy, that only affects the current connection. You'd have to set it every time right before you convert the string to a date.
The best solution is probably the ISO format, like #IanYates suggests.
dear your code is working after using
* SET DATEFORMAT dmy
because you are passing date as "dd/MM/yyyy"
like "DateTime.Now.ToString("dd/mm/yyyy")"
check the date format in your database by following command :-
dbcc useroptions
and check "dateformat : mdy" by default. So passing your date in "MM/dd/yyyy" format will also work fine.
Happy Coding :)

How to get datetime records in "dd/mm/yyyy h:m:s" format FROM SQL server 2005?

I want to get records from sql server 2005 in datetime format like this "dd/mm/yyyy h:m:s"
My database date is default datetime format
AttLogId int
...
DownloadLog datetime
I am trying to retrieve the datetime like this:
SELECT AttLogId ,convert(varchar,DownloadLogDate,103) FROM AttLog
ORDER BY DownloadLogDate
but I only get the date and not the time.
I suggest you don't try to get the values in a particular string format. Fetch them as DateTime values and then format them in the .NET code using DateTime.ToString("dd/MM/yyyy H:m:s").
It's almost always worth keeping data in its "natural" data type (date/time here) for as long as possible, only converting to text when you really need to. So your SQL should just be:
SELECT AttLogId, DownloadLogDate FROM AttLog
ORDER BY DownloadLogDate
How you then retrieve the data will depend on how you're talking to SQL (e.g. LINQ to SQL, using SqlDbReader etc). But you should be able to get it as a DateTime and then format it locally. This will make it easier to test, easier to debug, give you more control over cultural aspects (date separators, possibly specifying a standard specifier instead of a custom one, etc).
it is because you are using 103 , so it will give only date .
if you want more format check this :
http://msdn.microsoft.com/en-us/library/ms187928.aspx
According to need, u can try this
SELECT AttLogId , convert(varchar(10),DownloadLogDate,103) +' '+ convert(varchar(8),DownloadLogDate,108) FROM AttLog ORDER BY DownloadLogDate
Note : firstone is for date and second one is for time in H:M:S
hope this help u..

Categories