简体   繁体   中英

Date format in SQL, Change default language or convert to varchar?

To begin with - I am very new to the whole DB and SQL business so apologies if this post might seen 'ridiculous' but after a few days of research I am genuinely stuck and confused. I hope someone could enlighten me.

I set myself a little practice project - property management software. I am using HeidiSQL as DBMS and Eclipse/Java/JavaFX8 for writing the application.

The problem I came across is Date type and format. By default it is set to US format (yyyy-mm-dd) where I want it to be in the UK one (dd-mm-yyyy). After the research I found that some people change default language to UK English. I tried to do it but I failed. A lot of example codes I found mentioned MS SQL Server - would I be better of switching to another DBMS or does it not really matter? I personally don't see how if all the queries are written using SQL but I guess there could be 'internal'queries that are specific for program used. Also, with this approach, would I change default language for a session, database or a whole server?

Another approach that I found is to convert Date to Varchar. I found that rather easy to implement but then I found and article that said it is a bad practice. It pretty much said that Date type is provided for a reason so if you need an actual date in ur db then it is the type that should be used and not Varchar.

In my program I will be using dates to perform some operations. I am struggling to come to conclusion which method I should use (and how to correctly implement it). A user will enter date using GUI and behind the scenes it will have to be converted into a correct format and type. Then in database (depending on the approach) it will have to be converted again. My aim is to find something that is the most, or close to the most efficient solution.

I have little knowledge regarding Databases - very basics I was thought for 4 months at university and my own research, so I really would appreciate if someone could clear things up for me and share the expertise and knowledge with me.

I think what you might want to use is str_to_date : See this .

You can convert every string into a date by telling MySQL where is days, months and years. For example :

SELECT STR_TO_DATE('31-12-2014','%d-%m-%Y');

Gives you :

'2014-12-31'

You haven't to change the date format in MySql. You have just to speciiy the date format when you make a query. Please see the MySql manual for more details.

To specify the format dd-mm-yyyy in your queries you can do like this :

SELECT date_format('date_field','%d/%m/%Y') as df FROM 'table'

Hope this could help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM