Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Datetime convert

Author  Topic 

Muhrab
Starting Member

3 Posts

Posted - 2012-05-07 : 07:11:56
Hello, can you give me a good tip plz
I need to convert a field in a Table from datatime (2001-07-01 00:00:00.000) to int (20010701)i need to update with this a column in a table.
Thx

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-05-07 : 07:24:11
What is the data type of your column? You can find that out by running this query:
SELECT DATA_TYPE FROM INFORMATION_SCHEMA.[COLUMNS] 
WHERE TABLE_NAME = 'YourTableName' AND COLUMN_NAME = 'YourColumnName'
If it is DATE, DATETIME, DATETIME2, or SMALLDATETIME you are doing the right thing - don't do any changes to the table or the way the column is stored.

If you want to query the table and get the result in the "20010701" format, do it like this:
SELECT CONVERT(CHAR(8),YourColumn,112);
However, it would be better for a variety of reasons to not do this conversion in SQL - instead, simply query the column, send the result to the client (such as a presentation layer) and have the client do the necessary conversion.
Go to Top of Page
   

- Advertisement -