Best format for storing dates in sql

Asked
Active3 hr before
Viewed126 times

6 Answers

format
90%

SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS, DATETIME - format: YYYY-MM-DD HH:MI:SS,SQL Server comes with the following data types for storing a date or a date/time value in the database:,TIMESTAMP - format: YYYY-MM-DD HH:MI:SS

load more v
88%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , What kind of systems do we need to coarse-grain to observe interesting phenomena? , What happens after a professional unintentionally crashes in a simulator? Can they be disciplined? ,My question is in 2 parts. Firstly, is it quickest a) storing all date formats in unixtime in sql and converting to date format through the server side query or b) storing all date formats in date format and converting to unixtime only when accessing them

I have an SQL table which contains 3 date columns. All 3 of these can be updated quite regularly by multiple users of the site, and entries are also entered in to it as follows:

mysql_query("INSERT INTO `regular` (
`regularid`,
`propertyid`,
`userid`,
`billdate`,
`billfrequency`,
`enddate`,
`amount`,
`description`,
`payee`,
`payer`,
`lastpayment` 
)
VALUES (
'', '{$_POST['propertyid']}', '$varuserid', FROM_UNIXTIME($date), '{$_POST['billfrequency']}', 'FROM_UNIXTIME($edate)', '$amount', '{$_POST['description']}', '{$_POST['payee']}', '{$_POST['payer']}', 'FROM_UNIXTIME($ldate)'
)") or die(mysql_error);

Print "A regular payment for &pound;".$_POST['amount']." has been created<br/>\n";
load more v
72%

DBA Database Administration Performance Tuning Monitoring Extended Events PowerShell SQL Server Agent Management Studio Backup Restore Availability Groups Webinars All Categories , All right, so you've not only, instead of getting wrong data, now you're getting empty data, which is not any better. And again, this 20200420 format always works. This could also happen if you have the times included in these formats. , And then this one, this surprises a lot of people. YYYY-MM-DD, this is ISO 8601. This is the standard, this is how it's defined. However, in SQL Server this can be misinterpreted in more than half of the languages that SQL Server supports, all right? ,The below script will allow you to run the tests discussed in the video.  You can also download the script.

0
65%

Timestamp(tz) or interval,In addition to storing date information, timestamps also include time data. “Timestamp” does not include time zone data, but “timestamptz” does. Timestamptz converts and stores time values in UTC by default. ,Example: Use TO_CHAR() to calculate the number of page impressions by month. This query takes a column with date values, formats the month as a string value in a column called “month”, sums the total number of page impressions by month and orders them by month.,Timestamp - contains date and time data, no time zone data

SELECT
TO_CHAR(date, 'mm') AS month,
   SUM(page_impressions)
FROM facebook_pages
GROUP BY month
ORDER BY month
load more v
75%

Storing Date and Time in 2 separate columns or one column using DateTime?,We can store Date and Time information in a couple of ways. What is the best approach for storing DateTime information?,Connect and share knowledge within a single location that is structured and easy to search., Yes, agree @ypercubeᵀᴹ. Timestamp usually means both date and time. I explicitly mentioned DateTimeStamp word, so anyone can understand that we are talking about date and time both. But you are also correct. Modified the answer. – Ashwini Mohan Mar 11 '18 at 6:10

If you store the values separately, you'll invariably run into "bugs" that are difficult to detect. Take for instance the following:

IF OBJECT_ID('tempdb..#DT') IS NOT NULL
DROP TABLE #DT;
CREATE TABLE #DT
   (
      dt_value DATETIME NOT NULL, d_value DATE NOT NULL, t_value TIME(0) NOT NULL
   );

DECLARE @d DATETIME = '2016-09-20 09:34:00';

INSERT INTO #DT(dt_value, d_value, t_value)
SELECT @d, CONVERT(DATE, @d), CONVERT(TIME(0), @d);

SET @d = '2016-09-20 11:34:00';

INSERT INTO #DT(dt_value, d_value, t_value)
SELECT @d, CONVERT(DATE, @d), CONVERT(TIME(0), @d);

/* show all rows with a date after 2016-07-01 11:00 am */
SELECT *
   FROM #DT dt
WHERE dt.dt_value >= '2016-07-01 11:00:00';

/* show all rows with a date after 2016-07-01 11:00 am */
SELECT *
   FROM #DT dt
WHERE dt.d_value >= CONVERT(DATE, '2016-07-01')
AND dt.t_value >= CONVERT(TIME(0), '11:00:00');
load more v
40%

This type of data is referred as temporal data and every database engine has a default storage format and data types for storing them. The following table shows the data types supported by the MySQL database server for handling the dates and times.,The following statement demonstrates how to insert a date value in database table:,Along with strings and numbers, you often need to store date and/or time values in a database, such as an user's birth date, employee's hiring date, date of the future events, the date and time a particular row is created or modified in a table, and so on.,While working with the database of a large application you often need to store record creation time or last modification time in your database, for example, storing the date and time when a user sign up, or when a user last updated his password, etc.

INSERT INTO employees(emp_name, hire_date, salary)
VALUES('Adam Smith', '2015-06-24', 4500);
load more v

Other "format-undefined" queries related to "Best format for storing dates in sql"