I try to convert date format in sql but it still doesn't work - sql

I use the CONVERT() function to trying convert the date format like DD/MM/YYYY with code 103 when I query database, and nothing happens. The data field still displays default format with YYYY/MM/DD.
UPDATE STAFF
SET BIRTH = CONVERT(smalldatetime,'26/08/1900',103)
WHERE ID = 'SF01'
How can I fix this problem ? I'm a newbie so i don't know lots of SQL

Avoid formatting dates in your database, it should be done in the application that queries the data. But IF you still need to do it in your database for whatever reason:
If you want to store the date in the DD/MM/YYYY format in your table then you can do 2 things,
Change your column data type to varchar(not ideal and you should try avoiding this method)
Change the regional settings on the machine running SqlServer and keep it as a date type (this is somewhat useless if you plan on querying data from another app as the app will probably format the date to its local format).
In case you decide to store it as varchar you will need to use the Format function when inserting or updating like this: Format(MyDate, 'DD/MM/YYYY').

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 :)

Date format is changing in SQL developer

I need to import the excel data into oracle database through SQL developer. I have change the date format in excel to the standard oracle format(DD_MON_YYYY HH:MI:SS AM). But while importing in SQL developer this date format is not reflecting, it is giving original date format as there in excel before I changed the format. How can I change the format?
Excel data:
In SQL developer:
As for my knowledge, your import is all right. Importance of the date format is to identify the date field as a date field when you are importing the data to database. After the import, database keep the date values in a default date format. So when you are retrieving the data, you have to specify the date format in your query to get what you want. In other words, use a query to get the data in the database.
Ex:
select to_date(date_field,'DD_MON_YYYY')
from table_name
You just need to change the data format in the SQL Developer preferences, tools > preferences > database > NLS
This will set the default format for displaying dates. You can always get it exactly the way you want for your queries by doing what +Asanga shows.

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.

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!