mysql datetime quick structure - sql

how good is the datetime for questions like: Compare sales from Monday and Thusday?
Mysql keeps datetime as a unix-timestamp internally? So finding mondays will be quite expensive.
Has anybody got experiance how much better mysql performs when an extra attribute "day" is introduced and given an index? Will indies liked this be used at all? It will only have 7 different states...

how good is the datetime for questions like: Compare sales from Monday and Thusday?
DATETIME is your best choice, as it's mySQL's native format and any date operations are highly optimized for it.
Mysql keeps datetime as a unix-timestamp internally?
Nope. I don't know what mySQL uses to store DATETIMEs internally, but it's not as Unix timestamps:
The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

Related

bigquery converting the string datetime with timezone

A table with terabytes of data in bigquery got multiple columns set as string format but actually they contain datetime strings like
2016-10-24 15:00:00
I tried answer from this link to convert (CAST) the fields into timestamp format as below
SELECT
CAST( MURDER_DATE AS TIMESTAMP) AS CONVERTED_MURDER_DATE, *
FROM `death_list`;
That works but it converts all strings into timestamps with UTC timezone as below
2007-03-23 15:00:00.000 UTC
I need the data in a different timezone. Any clue?
Try using
DATETIME(CAST( MURDER_DATE AS TIMESTAMP), "Australia/Sydney"))
To my view, it seems to be a current limitation of BigQuery:
Timestamp type is always stored in UTC format. And you have no way to add any "timezone" information to it.
Datetime type nor stores any information about the timezone. You could still have a internal convention in your team/company that says that all the Datetime columns are stored in your local timezone, but I personally find it very awkward.
What we've decided so far in our company is to store everything in Timestamp (thus UTC format), and we never use Datetime due to lack of precision regarding the time zone. Then, if a client wants to get the information in another timezone, it has to do the conversion itself when reading the data.

How to insert datetime with timezone to SQLite?

I know that to insert a datetime this format yyyy-mm-dd hh:mm:ss should be used.
However my dataset has a timestamp field that looks like yyyy-mm-dd hh:mm:ss +/-0X:00, where X can take many values and is different from my computer's local timezone.
What is the best way to insert a datetime field with such timezone information to SQLite?
SQLite's built-in date and time functions understand the timezone specification in these strings, but with different timezones, any other operations on these strings (even searches and comparisons) will not work correctly.
If you want to handle time zones, you have to either
convert all timestamps to one specific time zone (UTC), so that you can use them for sorting and searching; or
leave the time zone information in there, but do all searching, sorting and other computations not in SQL but in your application.

Which one is more desired when dealing with dates? sql DateTime or nvarchar string?

Does SQLs built-in DateTime type has any merits over nvarchar type?
If it were you , which one would you use?
I need to store dates in my SQLServer database and I'm curious to know which one is better and why it is better.
I also want to know what happens if I for example store dates as string literals (I mean nvarchar )? Does it take longer to be searched? Or they are the same in terms of performance ?
And for the last question. How can I send a date from my c# application to the sql field of tye DateTime? Is it any different from the c#s DateTime ?
You're given a date datetype for a reason, why would you not use it?
What happens when you store "3/2/2012" in a text field? Is it March 2nd? Is it February 3rd?
Store the date in a date or datetime field, and do any formatting of the date after the fact.
EDIT
If you have to store dates like 1391/7/1, your choices are:
Assuming you're using SQL Server 2008 or greater, use the datetime2 data type; it allows dates earlier than 1753/01/01 (which is what datetime stops at).
Assuming you're using SQL Server 2005 or earlier, store the dates as Roman calendar dates, and then in your application, use date/time functions to convert the date and time to the Farsi calendar.
Use the correct datatype (date/datetime/datetime2 dependant on version and requirement for time component).
Advantages are more compact storage than storing as a string (especially nvarchar as this is double byte). Built in validation against invalid dates such as 30 February. Sorts correctly. Avoids the need to cast it back to the correct datatype anyway when using date functions on it.
If I'm storing a DateTime value, and I expect to perform date-based calculcations based on it, I'll use a DateTime.
Storing Dates as strings (varchars) introduces a variety of logistical issues, not the least of which is rendering the date in a proper format. Again, that bows in favor of DateTime.
I would go with the DateTime since you can use various functions on it directly.
string wouldn't be too much of a hassle but you will have to cast the data each time you want to do something with it.
There is no real performance variance while searching on both type of fields so going with DateTime is better than strings when working with date values.
you must realise the datetime datatype like other datatypes is provided for a reason and you should use the datatype that represents your data clearly.. Besides this you gain all the functionalities/operations that are special to the datetime datatype..
One of the biggest gains is correct sorting of data which will not be possible directly if you use nvarchar as your datatype.. Even if you think you dont need sorting right now there will be a time in the future where this will be useful.
Also date validation is something that you will benefit from. There is no confusion of the dateformat stored i.e dd/mm or mm/dd etc..
There is lot discussed about the subject. There is good post on the SQLCentral forum about this particular subject DateTime or nvarchar.
In short, nvarchar is twice as longer as datetime, so it takes more space and on the long range, any action affecting it will be slower. You will have some validation issues and many more.

Sql Server 2008 - Convert localized DateTime column to UTC

I have some columns in a few tables in my database that are all in the localized server DateTime values. I want to convert these all to UTC DateTime values (does not need to be DateTime2 since it will be assumed the new values will be in UTC).
I would like to do this in a single SQL statement. I am not entirely sure how to perform this as I do not spend a lot of time in the database.
Assumptions can be made that my table name is TestTable and my localized DateTime column is called TestDateTime.
I would want the script to do the update to the table as well as the conversion.
Any way to possibly do this?
You can substract getutcdate() from getdate() to find the difference:
select DateAdd(s,DateDiff(s, getdate(),getutcdate()), LocalizedDateColumn)
This assumes you are in the same summertime/wintertime state as the time you're trying to convert. Not ideal, but the best conversion I know of that you can do in pure SQL.
A C# client has a much stronger conversion:
yourDateTime.ToUniversalTime()
Unlike the SQL version, the .NET version will correctly convert a wintertime date during the summer.

Store time of the day in SQL

How would you store a time or time range in SQL?
It won't be a datetime because it will just be let's say 4:30PM (not, January 3rd, 4:30pm).
Those would be weekly, or daily meetings.
The type of queries that I need are of course be for display, but also later will include complex queries such as avoiding conflicts in schedule.
I'd rather pick the best datatype for that now.
I'm using MS SQL Server Express 2005.
Thanks!
Nathan
Personally I would find this a reason to upgrade to 2008 which has a separate time datatype.
I would recommend still using a DateTime data type and ignoring the date values--ideally using the static MinDate for SQL (Google it). This will give you the benefits of working with a strongly typed field and the only cost will be a few extra bytes.
As for ranges, store them in two separate columns. Then you can subtract one from the other to determine the difference.
Edit: did some Googling.
SQL Server 2008 adds a Time data type, so you might want to consider that.
You can use SQL 2005's DateTime type and combine it with the CONVERT function to extract just the HH:MM:SS.MMM
SELECT CONVERT(VARCHAR(12), GETDATE(), 114) AS [HH:MI:SS(24H)] (Found on this handy-dandy page)
Different SQL versions support different minimum dates. You could use a static date that will be supported by all such as 1/1/2000, or you could use SQL 2005's minimum value of 1/1/1753 and append the time values to that startic day
So if you stick with 2005, pick your static date, like 1/1/2000, and store your times on it. So 1m:30s would be 2000-1-1 00:01:30.000, and 1h:15m would be 2000-1-1 01:15:00.000
You can then do Date2 - Date1 and get your result of (1h:15:m - 1m:30s) 2000-01-01 01:13:45.000. CONVERT it and you'll have 1:13:45.
You could store it as an int as 24 hour time and format as needed.
Or store it as a datetime with some fixed date and remove it as needed for display:
Jan 1 2000 4:30PM
I would go with datetime field as it gives you the power of all the datetime related functionality.
You might want to consider storing it as an int column representing the number of minutes since midnight. In your entity you could expose this as a TimeSpan (or int) representing the same thing. You'd only need to convert between your display values (time format) and the database value (minutes) in order to perform your queries and this could easily be done in your entity (TimeSpan.TotalMinutes, for example).
to me it sounds like you're developing a type of meeting scheduler or something to display the meetings.
i think that i would set it p with 2 columns MeetingStart and MeetingEnd, both as datetime fields. This way, you can determine the length of the meeting, and since you already have the date you can easily use it to display it on a calendar or something.