Aim:
I want to store date in database in dd/MM/yyyy format.
I have first formatted it using DateFormat which results in changing it to String datatype and then trying to convert back to date datatype by using to_date function but it gets stored in as 19-DEC-12 i.e dd-MON-yy format.
Code:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date dob=JDateChooser.getDate();
java.sql.Date dobloc=new java.sql.Date(dob.getTime());
String dobloc1=df.format(dobloc);
String sql="INSERT INTO ADMIN.DEPARTMENT(date_of_birth) VALUES (to_date(?,'dd/MM/yyyy'))"
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, dobloc1);
Dates are not kept with the format you specify in any database (unless you use a string field) Most database systems store datetime values as a "double" value.
You should put the date into the database and worry about formatting when you are getting the data from the db or presenting it to the user.
I think you are misunderstanding how date actually works in Oracle.
The date is not stored like "19-DEC-12" or "19/12/2012", it is stored as 7-bytes.
See here for further details.
If you select a date column it is formatted using NLS_DATE_FORMAT.
This is the default mask used to format those 7-bytes.
Of course if you use a custom format mask like to_char(sysdate,'YYYY MM DD') the default NLS_DATE_FORMAT is ignored.
As I read in the above comments
Actually I am trying to store in a correct format because i am displaying a pure copy of my Database table on some other forms using JTable so the date formatting in which date is stored gets displayed directly. I dont want to convert Date Format on my large number of Jtables which may make me to change the Date format again and again as my tables goes on increasing
I suggest you also save the specific formatting mask used to format the dates in another column in your DB-Table.
Before you display your date in the JTable you format it with this mask using DateFormat.
Related
I have a wrong date in the database 2022-10-14 12:59:00 , I want to change it to 2022-14-10 12:59:00. Is there a direct convert function I could use, or do I have to split and put the date correctly. Just trying to get some ideas to do this the easy way. I tried the convert functions but I guess the original date as to be corrected first is it?
You can use the CONVERT() function to go from a date type to a string with a particular "style". There are styles for different presentation formats to represent the different date presentation formats around the world.
To make this work, the data needs to be in a date type, not just a string. (or you can convert from a string type to a date type, then convert back to a string).
Date style descriptions
I have a view in SQL Server 2012 and there is a column of containing dates×. I have been trying to convert the date column as '20/10/2018 18:00' format (no second) by using this feature and lots of approaches on Stackoverflow and other web sites:
FORMAT(StartDate, 'dd.mm.yyyy hh:mm')
However, as the data type of this column is Varchar (String) rather than DateTime, I encountered some problems in C# side and I want to perform this conversion on the database side without changing the data type of the generated format). Is there any way to achieve this?
SQL Server doesn't have a "date format" per se. The formatting of datetime fields is only performed when presenting the datetime to an output - that is, when converting it to a string.
There is a default format for presentation that is controlled by the server's collation setting. However, internally the date is stored as a numeric value (actual format varies by type, as datetime and datetime2 have different internal formats), and that value has no associated formatting.
You can store your date without seconds by using a smalldatetime field, or by manipulating the input data to trim off the seconds value. But, unless you store your date as a string, which is absolutely not recommended, you will not be able to save an output format different from the default collation-driven format in a datetime field.
I would migrate that column to a datetime (or some variant) if possible. Alternatively if that would affect too many things, you could make a computed column on the table which converts the string date you have to a datetime. That way the database doesn't have to care about the formatting at all; it just works with the proper DateTime data type.
If neither of those is an option, you can just pas the string to C# and use DateTime.TryParse() to convert it to a C# DateTime object.
In either case, it's preferable to work with the date as a DateTime up until the very last minute where you need to format it for display somewhere.
i have a problem in converting a string date value into valid date format in pentaho-data-itegration .could someone help me to convert string date'08-Mar-2017' into date format (2017-03-08 00:00:00) in pentaho.
Date do not have a format in PDI.
To convert the '08-Mar-2017' string to a date, use the Select value step, Meta-data tab, and tell your field should be of type Date, and the conversion format is dd-MMM-yyyy (type it, as it not on the drop down list).
To convert the date to the '2017-03-08 00:00:00' string, use the Select value step, Meta-data tab, and tell your field should be of type String, and the conversion format is yyyy-MM-dd hh:mm:ss.
However, you most probably do not need to do it.
The only time a date format is used is for data input and output. Plus it is transparent, most of the time.
If your data comes from a file or data grid or a computation or many other steps, you can specify its format. If is comes from a Input table, think about converting with the sql build-in function : to-date, convert, cast, date depending on your database system.
If your output is a file or an Excel or a script, you can specify its format (which by default is yyyy-MM-dd hh:mm:ss). If it's a sql insert or update, don't do anything and let the databases do the conversion.
probably an easy one but i cannot really figure it out.
In VB, i have a DateTimePicker. I am able to set a date and store that date in a different format into a database (unfortunately non optional).
I enter dates in the format dd.MM.yyyy and the code stores it as yyyymmdd in the Database nicely using
foo = DatePicker.Value.ToString("yyyyMMdd"))
The problem is the other way around: I read dates in yyyyMMdd from the database and would like to set the DateTimePicker to that value, however it must still display the value as dd.MM.yyyy.
DatePicker.Value = ds.Tables("items").Rows(0).Item(9)
does not work (the value in yyyyMMdd is stored in that table field). I also tried something like
DatePicker.Value = New Date("20130303")
with no success. How would one cleverly parse such a string to another date format? I am sure, that there is a more efficient way than as using
DatePicker.Value = New Date(year, month, day)
after splitting the string manually into a 4 byte year, a two byte month and a two byte day.
Thanks your your help.
Luke
The DateTimePicker.Value properties expects a DateTime, so you need to convert the column content (it appears to be a string) to a DateTime.
After setting the Value property it is the DateTimePicker job to display that Date in the format required by your application.
If you are absolutely sure about the format stored in the colum then you can use DateTime.ParseExact
Dim tb as DataTable = ds.Tables("items")
DatePicker.Value = DateTime.ParseExact(tb.Rows(0).Item(9), "yyyyMMdd", CultureInfo.InvariantCulture)
Now let me say one thing. Do not store dates in a text column also if you use a format like "yyyyMMdd". A DateTime value should be stored in a DateTime column
I know that it is possible to reformat the DateTime data type of the current date and time by using select convert. However, I havent been able to find a method to reformat an existing column (DateTime data type) to: hh:mm:ss yyyy/mm/dd. Or even better, I would like to reformat it to show time only. I dont want to simply convert to time data type because I am working with a chart that accepts either Date or Date time. But what I really want to display on that specific axis of the chart is time. Is there any way to reformat DateTime to my requirements? Thanks.
SQL Server's convert function can take an optional style argument depending upon the datatype. This is how you can get a datetime converted to a string in a variety of formats. For example, try running:
select convert(varchar(30), getdate(), 108) -- returns hh:mi:ss
You can replace getdate() in this example with a column name as well.
There are many styles available, as shown in the documentation.
Note that you will be returning a varchar, so you may want to sort by the original column's datatype.
You can use SET DATEFORMAT but this will change all DateTime's on your SQL Server. You can do some custom formatting, but it will convert the DateTime into a VarChar, so you won't be able to treat it as a DateTime.
SET DATEFORMAT: http://msdn.microsoft.com/en-us/library/ms189491.aspx
I'm not sure if you will be able to use the format you're asking about, though.