How to set the Asp Calendar Extender DataType to smalldatetime - sqldatatypes

I have implemented a Calendar Extender for my website, I use this to search for dates in my database. The problem is I have all the dates in the database set to datatype smalldatetime and the dates from Calendar Extender is coming out as a type String. So I would like to know how to change the datatype for the date selected from Calendar Extender.
I'm a newbie so please help me out

Assuming that you're using ASP.NET and not classic ASP you can convert a string to datetime using the Convert or Parse method as shown here:
http://msdn.microsoft.com/en-us/library/cc165448.aspx
How are you pushing the data coming from your website to the database? Do you have some kind of data access layer?

Related

Change Datetime format in Microsoft Sql Server 2012

Hi i want to change the default datetime type in sql server. I have already table who has rows and i dont want to delete them. Now the datetime format that had rows is: 2015-11-16 09:04:06.000 and i want to change in 16.11.2015 09:04:06 and every new row that i insert i want to take this datetime format.
SQL Server does not store DATETIME values in the way you're thinking it does. The value that you see is simply what the DBMS is choosing to render the data as. If you wish to change the display of the DATETIME type, you can use the FORMAT() built-in function in SQL Server 2012 or later versions, but keep in mind this is converting it to a VARCHAR
You can get the format you desire via the following:
SELECT FORMAT(YourDateField, N'dd.MM.yyyy HH:mm:ss')
There is no such thing as format of the DATETIME data type, it has no format by nature, formatted is the text representation you can set when converting to VARCHAR or some visualization settings of the client / IDE.
If you, however, want to be able to insert dates using string representations that are alternatively formatted (i.e. control the way string input is parsed to datetime type) you can check SET DATEFORMAT - as explained in the remarks section this will not change the display representation of date fields / variables.
SQL serve provide wide range of date formatting function or way by using that user can change date format as per his requirement.
Some of are giver bellow.
CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)

Creating a custom data type with specific format

I'm moving from an access database to a SQL Server database and wanted to know if I can create my own data type for dates.
I want to have my custom date type to have a format of mm/dd/yyyy instead of yyyy/mm/dd. Is this possible to do?
Use the default/built-in data type to store a specific data type. This will be more efficient and reliable as sql server will only allow the valid data to be stored (data integrity).
Also this will allow you to make use of all the built-in functions to work with that specific data type. In your case if you use Sql Server's datetime data type you will be able to make use of all the datetime functions (DATEDIFF() , DATEADD() , DAY() , YEAR() , MONTH(), DATENAME() etc).
As far as how you see the date/datetime values stored in your database, again you will always have built-in datatime functions to format the date values as it suits you.
Once you have stored the date/datetime values in sql server database and you want to view date value as mm/dd/yyyy instead of yyyy/mm/dd simply do the following:
SELECT CONVERT(VARCHAR(10), [DateColumn], 101)
Custom datatypes are there to be used but there are some very odd issues with them, avoid them whenever you can :)

How do you convert SQL mm/dd/yy to mm/dd only?

How do you convert SQL mm/dd/yy datetime to mm/dd only? On Microsoft server.
Thanks all.
With dates and times it is an extremely common mistake to believe that what you see is what is stored. If the field is date, datetime, smalldatetime or datetime2 then what is stored are integers, not strings. So if the field is one of these, then:
convert(varchar(5),[date_field],1)
or
format([date_field],'MM/dd') -- mssql 2012 onward
If the information is a string already then left() will do the job.
Since you have specified an input format, the input must already be a string. Simply truncate with
cast(dateIn as char(5)).
You can use LEFT to just return the day and month:
SELECT LEFT('12/12/2000', 5)
I realize this isn't directly answering your question the way you asked it, but the best advice I can give is: Don't.
Instead, send back the field in its native datetime type. The database is not the place to be doing formatting. Instead, format the date in your application code.
For example, if you are calling SQL Server from a C#/.NET application, you could retrieve the value from a DataReader like this:
DateTime dt = (DateTime) reader["YourDateTime"];
Then you would format it as a string like this:
string s = dt.ToString("MM/dd");
This will ensure that the date is formatted correctly. If you are using a different language to call SQL Server, there are probably similar methods in that language.
One of the problems with the other approach mentioned (trunacating the string) is that the original value might not be formatted in mm/dd/yyyy to begin with. That all depends on the environment settings where the SQL Server is running. If you run the same code on an environment with dd/mm/yyyy settings, you would have unexpected results. This is avoided by using the native data type, the way I described.

how to change datetime format for the whole database?

My application is based on format: mm/dd/yyyy
After deploying the database to the customer's side, it is based on: dd/mm/yyyy
I dont want to change all of my queries, so how can I change all the datetime format for the whole database?
It is MSSQL Server 2005
Thanks in advance.
If you wanted to you could write a class which would interoperate the date() function (which produces a timestamp) into a real date, which you could then insert into the database.
But you should be more specific, ie, what languages are you using to code in, if you are, etc?
You'll need to change the default language for the database and then change the default language for all of your logins. I have a blog that explains this:
Setting a standard date format
look at :Change default date time format on a single database in SQL Server
If question is related with SQL server only, it should be closed. If no, it should be moved to C# topic.

Convert Coldfusion Error.DateTime to a normal format?

At my company we store the information gathered from our site wide error template, into the database, but because of the format of the error.datetime it is making hard for me to do any sql queries for different date ranges.
Has anyone used some t-sql or coldfusion code to convert it to a mm/dd/yyyy format?
Here is an example of the format it currently is as.
Sun Jun 13 21:54:32 CDT 2010
But for any queries, I need to do, I have in a better format, I believe.
On the CF side, you should be able to user createOdbcDateTime() to correctly format it for the database or dateformat() to format it as text. If the date is coming back as text instead of a date object, you could use parseDateTime() to convert to a date object.
As an alternative, you could avoid having to convert dates at all if you just use the SQL Server built-in getDate() function to fill out your date column as the error is being inserted into the database.
It may not be exactly the same time (i.e. it might be out by a ms or 10) but it should be pretty close and perhaps good enough for your purposes.
Just make sure that your database server and application server are time synchronised!