database field with type timestamp is not updating - database

Duplicate of database timestamp value is not
updating
I have a variable which contains the date and time in 0000-00-00 00:00:00 format but when i put this variable in database field with type timestamp .. it is still 0000-00-00 00:00:00

Make sure that you are using the right format of the datetime. Mysql default datetime format is YYYY-MM-DD H:i:s. But, it seems that you are trying to insert the datetime value with the format of (YYYY:MM-DD H:i:s). It does not allow to insert the datetime value, so the inserted value becomes 0000-00-00 00:00:00.
Try by changing your calendar format to (YYYY-MM-DD H:i:s). I assume that you are using datatime datatype for the Valid_till column. Click the below to know more about mysql data structure.
Mysql Date format

Related

Load the date separated by '/' with the Datetime datatype in sql server

INSERT INTO PUZ_DATE_FORMAT
SELECT FORMAT(GETDATE(),'d', 'it-IT') AS ItalianDate
I get this error:
Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
My table contains only a single column of datetime datatype, and the format statement which I've written will give the output like 15/12/2017.
But when I try to insert a row into the table, it won't allow me to do so. It allows only dd-mm-yyyy format - not dd/mm/yyyy. Why?
Basically what you are trying to do is insert a string value to a Datetime field. In doing so, SQL Server try to cast the string to a datetime during the insert. If your string is in the Universal format (yyyy-MM-dd) it can successfully parse it without any issue. Else the string has to be in the datetime format as Server's culture.
I guess your Server's culture is en-US which has the date time format as "MM/dd/yyyy". With regarding to your date (15/12/2017) server thinks 15 is the month, 12 is the day and so on. Which is obviously falling.
If you tried this before 12th of the month, It can successfully cast, But to an incorrect value.
Further, you are not doing the right thing by, casting a Datetime to a string and then try to insert in to Datetime field. Try to store the raw Datetime in database and format accordingly when displaying in UIs.
Cheers,

How to declare Datetime type column with DDMMYYYY format in MSSQL

Below is my table structure:
DECLARE #FinalTable TABLE
(
[BranchID] BIGINT NULL,
[BranchName] NVARCHAR(MAX) NULL,
[Date] DATETIME NULL
)
My MSSQLSERVER is too crazy. It is saving the datetime in MMDDYYYY format, not knowing I required SELECT statement output in DDMMYYYY format. I am tired of using that CONVERT() and CAST() functions each and every time to in SELECT statement.
Is there any way to declare table column with my datetime format ( i.e. DDMMYYYY) so that I don't need to use CONVERT() or CAST() functions every time.
No, it's not. There is no formatting information at all associated with the field.
The value is not formatted by the database, it's returned only as a point in time. Formatting that value into it's textual representation is done by the applcation that is getting the data from the database.
So, there is nothing that you can do in the database to change how the date/datetime value is formatted, you have to change that where the data is displayed.
Date Time gets stored in database with the default format based on selected default language for the user. However you can manipulate the format when fetching the date time.
Following is the way to fetch the data with different defined formats:
All Formats for DateTime
But, if you still want to store the Date-Time of your own choice format then you have to create a column with Varchar DataType and later convert into date when fetching it (Not recommended)
Convert(datetime, Expression, FormatStyle)
For more information View the following answers:
Answer1
Answer2

What should be a default datetime value?

I am inserting Excel Sheet records in my DataTable in c# and passing this DataTable to an SQL stored procedure. In my c# code I have put certain checks for empty data cells of Excel sheet to avoid Exceptions. But It seems like I am missing something while giving a default value for my SQL Date field.
string InvoiceDate = (row.Cells[3].Text == " ") ? "0/00/0000 00:00:00 AM" : (row.Cells[3].Text);
And I get the following error:
String was not recognized as a valid DateTime.Couldn't store
<0/00/0000 00:00:00 AM> in InvoiceDate Column. Expected type is
DateTime.
Edited -
Declaration of SQL field [InvoiceDate]
[InvoiceDate] [date] NOT NULL
Please don't suggest inserting null as I cannot Insert null for this column.
First, There is no 00/00/0000 date, not in the real world and not in sql server.
Second, why do you even need a default values? just use null instead.
Third, use ISO 8601 format for specifying dates in strings (yyyy-mm-ddThh:mm:ss)
Forth, As Giorgi rightfully pointed out in his comment, why even use strings for a datetime value? use a variable of type DateTime in c#. note that it's min value is different then the sql server DateTime data type min value.
If your datetime column is not nullable, you can use 1753-01-01 (min value for datetime) or 9999-12-31 (max value for datetime)
One last thing, you might want to consider using either datetime2 or separate the date and time to different columns (data types date and time, of course). Why? read here.
Try to insert the current date instead:
string InvoiceDate = string.IsNullOrEmpty(row.Cells[3].Text) ? DateTime.Now.ToString() : (row.Cells[3].Text);

How to make SQL Server to save datetime with AM/PM format?

I'm trying to make my SQL Server table datetime columns save datetime with AM/PM. How to make SQL Server to save datetime with AM/PM format?
Right now it saves date like this: 2012-01-23 14:47:00.000
Is it possible to save it 2012-01-23 02:47:00.000 PM ??
Or does SQL Server save the date and time in this format (2012-01-23 14:47:00.000) all the time and I need to convert it just on output and input?
Is it even possible to save it in this format (2012-01-23 02:47:00.000 PM)? Or does SQL Server save datetime in 24 hour format?
thanks indeed for any help. sorry for language. ;)
Internally the date and time are stored as a number.
Whether it's displayed in a 12 or 24 hour clock is up to the program formatting it for display.
As Andrew said, Datetime format is stored not as string. so, you can use CONVERT function to get the datetime value in approprate format. for example,
SELECT CONVERT(VARCHAR(20), GETDATE(), 100)
to learn more about datetime formatting, see this article
AM/PM serves only for visualization, if you need to display them, use CONVERT keyword:
SELECT CONVERT(varchar, YourDateTimeField, 109)
FROM YourTable
If you need to store AM/PM - it is makes no sense for datetime type, use varchar type instead.
You can simply use CONVERT function as following:
select CONVERT(VARCHAR,GETDATE(),108)
http://www.fmsinc.com/free/NewTips/SQL/AM_PM_time_format_in_SQL.asp
http://msdn.microsoft.com/en-us/library/Aa226054
http://blogs.msdn.com/b/kathykam/archive/2006/09/29/773041.aspx
Depending on the accuracy of the datetime you are storing you might be able to clean it up with
REPLACE(CONVERT (varchar, YourDateTimeField, 109), ':00.0000000', ' ')
This will not work if your date field is populated with GETDATE() as that means it will contain seconds and milliseconds but it will work if the field is populated by a user and seconds and milliseconds are all zeros

Database and DataTable DateTime format confusion

I want to store a DateTime.Now from my WPF C# application to a DateTime column in SQL CE Database.
I am inserting DateTime.Now.ToString("yyyy-mm-dd HH:mm:ss") then I get the following error:
String was not recognized as a valid DateTime.Couldn't store <2011-54-06 18:54:47> in DateTime Column. Expected type is DateTime.
How to store DateTime in a specific format to SQL CE?
[EDIT]
Correction made to: DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
However, after I insert and populate the data to a DataGrid. It shows 2011-54-06 18:54:47 in stead of yyyy-MM-dd HH:mm:ss format. I specify the format to be insert to database but it seem SQL CE has its own way of storing. Why?
You just change the yyyy-mm-dd HH:mm:ss in to yyyy-MM-dd HH:mm:ss. Obviously it will show like that.Because, you are not inserting the Month but a minute. So, the Datetime is not converted from string format due to wrong format.

Resources