Change Date Format in SQL Server 2008 - sql-server-2008

I have a table where users used to enter random date formats like
dd/MM/Year or MM/dd/Year.
But I want to update all those dates to this format Year/MM/dd
I have made a cursor and changed data by CHAR INDEX and sub string..
but is there any easy way to change the date format to the one i want ?
and also how can I make my default database date format = Year/mm/dd
only database not server?

You can convert it into different styles.
for example:
select convert(varchar(15),getdate(),103)
will give today's date in dd/mm/yyyy.
Full list of converting styles supported by MSSQL is here

Related

Date Styles in MySQL

I'm trying to input a specific date type into my mysql database.
The format is dd-'month'-yy where month is the abbreviated month. For example, I would like to put in, 12-DEC-78 which would be December 12, 1978.
I've tried inputting that style with just the DATE data type, but it just reformats to 0's- like 0000-00-00.
I've looked all over the MySQL page itself and have had no luck with finding a data type that supports this format at table creation. Is there a type I'm overlooking or a setting to switch over to this method?
You will need to convert that date into a MySQL friendly format. STR_TO_DATE() will do that for you:
STR_TO_DATE('12-DEC-78', "%d-%b-%y")
You may want to try this:
Change the DATE type to char(9), name "olddate" This allows you to insert '12-Dec-78' format.
After you have inserted the CSV, create a new column with DATE type, name "newdate"
UPDATE table SET newdate=STR_TO_DATE(oldate, "%d-%b-%y")
Remove olddate column

How to find Date format in MySQL

I am looking to simply find out what is the current Date format that I have in m MYSql Database. This should be easy but I am not finding the command or function to do this. I simply want to query for date format and have it return what it is currently set at, basically; DD:MM:YY or YYYY:MM:DD or whatever the format currently is set at for a particular DB.
Thank You
By default, the date format is YYYY-MM-DD. However you can convert any date with the CONVERT()-function. Make sure to look this function up.
You'll have lots of possibilities.

Mysql Date formt

How to Create a colummn DATE with format DD/MM/YYYY?
Mysql come for defaut with this formar YYY/MM/DD.
I need isert via HTML5 date Day/Month/Year, for this format come in DD/MM/YYYY
On your insert/select statements, you can convert MySQL Date Functions:
SELECT ... DATE_FORMAT(field,'%d/%m/%Y');
INSERT ... STR_TO_DATE('formatteddate','%d/%m/%Y')
you can check your js file if any,corresponding to date and change format to MySQL database format
I guess you could save it as a varchar, but that's messy. Your best option is to have MySQL save the date in the default format, then simply format the date to DD/MM/YYYY upon retrieval.

How to import Excel's serialized datetime into MySQL

How do I import and preserve date format fields such as "8/21/2012" from Excel to MySQL?
I am using MySQL Workbench and the Excel MySQL Excel data transfer plug in. When I select the Excel data I want to import into MySQL, I get a window where I declare variable types for all fields. All fields and declarations work as expected except for date and time fields. Both date and time switch from 8/21/2012 to a number like 398475 etc. How can I import these fields into MySQL by preserving the dashed mm/dd/yyyy format? I assume the same procedure will work for time as well.
Alternatively, is there a way to convert the serialized datetime value (a float, representing the number of days since 1/1/1900) back to mm/dd/yyyy in MySQL ?
Thank you!
You can convert your date cells into a MySQL supported format using the TEXT function.
=TEXT(A1,"YYYY-MM-DD")
This will convert a date in cell A1 to the yyyy-mm-dd format that the MySQL date field expects.
To get the serialized date format (say 36422) into a useful date format in MYSQL use the interval function added to the base date that Excel uses (which is actually 1900-00-00 but since that doesn't exist you will have to use 1900-01-01 which is why we subtract 2 from the date column)
`'1900-01-01' + INTERVAL(Your_Date_Column - 2)DAY`
You can convert your datetime cells into a MySQL supported format using the TEXT function
=TEXT(A1,"YYYY-MM-DD HH:MM:SS")
Simply use Date (java.util package) for this.
Date d=Date date=cell.getDateCellValue();
Now Use it as you want.

How to make the default date format dd/mm/yyyy in SQL Server 2008?

I have an Excel file that contains a column full of dates in the dd/mm/yyyy format. When I try to import it using openrowset, it said that there was a datatype mismatch. I have a table where the date is defined as type date. Now, I know that the default date format in SQL Server is yyyy-mm-dd. How can I avoid this conflict? Is there a way I can make the default date type be dd/mm/yyyy? I need to do this import operation everyday and it has to be automated and so I cannot afford it to fail in between. I tried using sp_addlanguage to make it British as the default date type is dd/mm/yyyy there, but it didn't work :(. I'm using SQL Server 2008 and Windows 7, if that is of any help. Please help me out! Thanks!
You could CONVERT the incoming data before you insert it. So, in the openrowset statement, where you select the field, you could surround it with a CONVERT statement. Here's an example:
print convert(date,'19/07/2010',103)
This is a UK style date, but if you run it you can see that it's converted it to SQL-friendly format.