How can I store date only in datetime field in WebMatrix with Sql Server CE? - sql

I was wondering if there was a way to store a date (example: 01/01/2013) as datetime without SQL Server CE adding the time (example: 12:00:00 AM).
I could always store it as the string "01/01/2013" but I really want to be able to compare the dates on querying the database.
I realize that as long as I only stored the date part, all of the times in the datetime field would have equal values (i.e. 12:00:00 AM), so comparing them wouldn't be a problem and I could just always ignore the time part, however, it seems ridiculous to have this unnecessary data appended to every entry in the table.
Is there a way to store only the date part of the datetime as datetime so that the dates can still be compared in the SQL query or do I just need to live with this overhead and move on?
Side Note:
I just spent the last 30 minutes searching Google and SO for an answer I was sure was already out there, but to my surprise, I couldn't find anything on this issue.
Update:
The conclusion I have come to is that I will just accept the time in the datetime format and let it always default to 12:00:00 AM by only adding the date part during the INSERT statement (e.g. 01/01/2013). As long as the time part always remains the same throughout, the dates will still be easily comparable and I can just trim it up when I convert it to string for screen display. I believe this will be the easiest way to handle this scenario. After all, I decided to use SQL for the power of its queries, otherwise, I might have just used XML instead of a database, in the first place.

No you really can't get rid of the time component. It is part of the data type defined by sql server. I was very annoyed by it until I found that I could still display the dates without the time using JQuery to reformat them with the date formatter plugi:
https://github.com/phstc/jquery-dateFormat
Good Luck!

select CONVERT(date, GETDATE())

Related

How to get only a date from a datetime column?

I have a report to do, so I need to return a date from a datetime column, but it need to come only with the date from the column.
I am using the Microsoft SQL Server 2014. I've already tried to use CONVERT(name_of_the_column, GETDATE()) but I realised the it only works to return the current datetime from the server.
How can I do that?
Use CONVERT(DATE, <expression>).
create table t (dt datetime);
insert into t (dt) values ('2022-10-20 12:34:56');
select *, CONVERT(DATE, dt) from t;
Result:
dt (No column name)
----------------------- ----------
2022-10-20 12:34:56.000 2022-10-20
See example at db<>fiddle.
I prefer to use CAST instead.
CAST(name_of_the_field AS DATE)
I need to return a date from a datetime column
There are several things to cover in order to fully answer this.
First, if that really is a datetime column, something to understand here is datetime values will ALWAYS have a time component. It's even right there in the name. There is no way to return a datetime value that does not have a time portion. The closest you can get is casting to a string or date, but that's not really the same thing. Typically what happens instead is the time portion is truncated back to midnight (all 0s)... but that time portion is still there. That's where you see answers like this:
dateadd(dd, datediff(dd,0, name_of_the_column), 0)
or this:
CONVERT(varchar(10), name_of_the_column,101)
The next to thing to understand is the one thing you should not do is convert the date value to a string, as in the second example.
Because of cultural/internationalization issues, converting datetime or numeric values to and from strings is much slower and more error-prone than you'd like to believe. It's something to avoid, and probably best left to your client code or reporting tool.
A final consideration is datetime values NEVER have any time zone awareness (that would involve datetime2). So if there's anything on the client to show this in a local time it may be picking the wrong offset. Do that with a value where the time portion was already truncated, and you can end up with unexpected results.
Put all this together, and what you should do is CAST() to a DATE:
CAST(name_of_the_column AS DATE)
Now I have seen this comment on another answer with this recommendation:
keeps turning back time
This could be the time zone mismatch I've already mentioned, or it could also be that name_of_the_column is not really a datetime column in the first place, but rather a string/varchar column pretending to be a datetime column. In that case, the CAST() may be parsing the values with different cultural rules than you expect. It goes back to that whole "slower and more error-prone/something to avoid" thing again. Also in this is the case the schema is considered broken, and you really should fix it to use actual datetime or datetime2 values.
Speaking of "fix it", I also noticed the SQL Server 2014 tag. SQL Server 2014 has been fully end of life since 2019. That means it has not received any updates — not even critical security patches! — for several years now. It's dangerous and irresponsible to still be using it. Updating to a supported version is job #1 here.
I don't know why, but the only way that have returned the result I want to reach was using
CONVERT(VARCHAR, TB.COLUM, 103) AS 'NAME'.
But as mentioned before, this is not a good practice, and than I resolved to made do the convertion in the report tool of the system that am I working

Is it possible in SSMS to pull a DATETIME value as DATE?

I want to start by saying my SQL knowledge is limited (the sololearn SQL basics course is it), and I have fallen into a position where I am regularly asked to pull data from the SQL database for our ERP software. I have been pretty successful so far, but my current problem is stumping me.
I need to filter my results by having the date match from 2 separate tables.
My issue is that one of the tables outputs DATETIME with full time data. e.g. "2022-08-18 11:13:09.000"
While the other table zeros the time data. e.g. "2022-08-18 00:00:00.000"
Is there a way I can on the fly convert these to just a DATE e.g. "2022-08-18" so I can set them equal and get the results I need?
A simple CAST statement should work if I understand correctly.
CAST( dateToConvert AS DATE)

Is it possible to add just Date in a datetime column and not get appended 00:00.000

MY Table consists of a datetime column and I wanted to know if its possible to add just Date in the column without getting appended zeros in Time part. Currently I used
CONVERT(date, getdate()) and tried to insert , but it comes in as
2013-01-20 00:00:00.000 in the column.
Pardon me if this is a very basic question
Yes, it is. Use the DATE data type instead of DATETIME.
However do not concern yourself with whether the 0s are there are not - that's not how SQL Server stores it internally, it's just how Management Studio is presenting it to you.
If you are using SQL Server you could use the Date type rather than DateTime. Otherwise the zeros will be there for the time portion. Of course you can choose not to display the time portion in your application if desired.

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.

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.