Tuesday, April 23, 2024
HomeJavaWhy use Date as YYYYMMDD in SQL Server?

Why use Date as YYYYMMDD in SQL Server?


While you enter a Date in SQL SERVER for inserting knowledge or filtering knowledge in WHERE clause, what you enter is String. SQL Server infernally converts that formatted String to completely different date and time knowledge sorts in Java e.g. DATETIME, DATE, TIME, DATETIME2 and SMALLDATETIME. This conversion is topic to language setting in SQL SERVER, for instance, the identical date 03/07/2015 could be interpreted as third March 2015 if language setting is US English (MMDDYYYY) and third July 2015 if language is ready to British English(DDMMYYYY). To keep away from this, you’ll be able to enter date as YYYYMMDD format, which is taken into account language impartial and at all times interpreted appropriately by Microsoft SQL Server.

For instance, the date 20150709 will at all times interpreted as ninth July 2015 by SQL Server, no matter language setting. Because of this it is thought-about greatest observe to enter date as YYYYMMDD String in Microsoft SQL Server. 

Necessary factors

1) When a language setting is utilized in Microsoft SQL Server, it units many variable respective to that language and certainly one of them is DATEFORMAT. That is why a String not entered in language impartial approach will likely be interpreted in a different way by completely different SQL Server cases with completely different language settings. 

2) Similar date 07-12-15 could be interpreted as seventh December 2015 if language is ready to en-UK, twelfth July if language is American English.

3) If you don’t need that SQL Server implicitly convert VARCHAR to DATETIME sort, you should utilize PARSE() or CONVERT() perform to offer a formatting model. This can be certain that SQL SERVER interpret the String in the identical date format no matter language setting e.g. 

SELECT CONVERT(DATETIME, '09-10-2015', 104); 

will be certain that given date is at all times on DD-MM-YYYY format, also referred to as British Date format. 

4) The language setting solely have an effect on the String to Datetime conversion for inserting knowledge into SQL SERVER. It will not have an effect on the presentation of that knowledge into SQL shopper e.g. ODBC or SQL SERVER Administration Studio.

5) In SQL Server 2008, separate DATE and TIME knowledge sorts have been launched, additionally they have couple of different DATEFORMAT which is language impartial, e.g. YYYY-MM-DD (2015-07-31) can be language impartial for DATE sort however on account of legacy causes it isn’t language impartial for DATETIME and SMALLDATETIME sorts. Alternatively, YYYYMMDD is language impartial for all of date and time knowledge sorts in SQL SERVER. 

6) When Microsoft SQL Server converts a String to DATETIME which solely accommodates date half, it thought-about time advert midnight and enter ’00:00:00.000′ for time half. This is essential element to know in case your DATETIME column accommodates each date and time half. Why? as a result of it could create delicate bugs whereas evaluating Date in Java as proven right here.

That is all about Why must you at all times enter date as “YYYYMMDD” in SQL SERVER. This can keep away from the bug created by completely different language setting at completely different SQL SERVER cases. It is the language impartial format for all SQL SERVER DATE and TIME knowledge sorts together with DATETIME, DATE, TIME, DATETIME2 and SMALLDATETIME.  

You can even additional learn Microsoft SQL SERVER 2012 T-SQL Fundamentals and Querying Microsoft SQL SERVER 2012, the 70-461 Examination Information to be taught extra about SQL Server particulars like this.

Different associated SQL queries, Interview questions, and articles:

  • Tips on how to discover the second highest wage in a desk? (resolution)
  • Prime 5 Books to be taught Superior SQL and Database Design (books)
  • Write a SQL question to repeat or backup a desk in MySQL (resolution)
  • Distinction between the Distinctive and Major keys within the desk? (reply)
  • Tips on how to migrate SQL queries from Oracle to SQL Server 2008? (reply)
  • Distinction between clustered and non-clustered indexes in SQL? (reply)
  • What’s the distinction between UNION and UNION ALL in SQL? (reply)
  • Prime 5 Programs to be taught MySQL Database for Inexperienced persons (Programs)
  • 5 Free Programs to be taught T-SQL and SQL Server for Inexperienced persons (Programs)
  • Prime 5 Web sites to be taught SQL on-line for FREE? (useful resource)
  • 5 Greatest PostgreSQL Programs for Inexperienced persons (on-line programs)
  • 5 Programs to be taught Oracle and Microsoft SQL Server database (programs)
  • Distinction between View and Materialized View in Database? (reply)
  • 5 Programs to be taught Database and SQL Higher (programs)
  • 10 Free SQL and Database Programs for Inexperienced persons (free programs)

Thanks for studying this text up to now. In case you like these  SQL tutorial then please share them with your folks and colleagues. When you’ve got any questions or suggestions then please drop a observe.

P.S. – In case you are fascinated about studying Database and SQL and
searching for some free assets to start out your journey then you may as well
check out this record of Free SQL Programs for Inexperienced persons to kick-start your studying.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments