Solved

SQL Server 2008 Date

Posted on 2010-11-16
17
648 Views
Last Modified: 2012-05-10
in  SQL server 2008 date values is comming yyyy-dd-mm (eg:2010-11-16). i need to convert it to DD/MM/YY . i need to changed the Date filed Called Orderdate
0
Comment
Question by:lankapala
  • 6
  • 5
  • 2
  • +2
17 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34151027
Is it coming in as a varchar?
convert(char(8), Orderdate, 3)
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34151053
If orderdate is a date field, then it is internally stored without formatting - that is a function of the display.
0
 

Author Comment

by:lankapala
ID: 34151154
Orderdate in my database is datetime, i need to permantly convert to my Orderdate date type DD/MM/YY, if i used
select Select convert(char(8), Orderdate, 3) from Orders; it will show only that time, i need to change Permently.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:lankapala
ID: 34151193
is possible to use below code
ALTER TABLE Orders
ALTER COLUMN  OrderDate  convert(char(8), OrderDate, 3)

0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34151206
see http:#a34151053
If the field is datetime, there is NO inherent formatting.  It is stored internally as some numeric value, not a string in yyyy-mm-dd nor dd/mm/yy or any text.

You need to specifically format it on display, be it in SSMS or other.
The only other option is to change the field to Char(8) for dd/mm/yy but doesn't sound like what you want.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34151260
If you want to permanently change the column type to char containing dd/mm/yy then

alter table orders add orderdate2 char(8);
update orders set orderdate2 = convert(char(8), orderdate, 3);
alter table orders alter column orderdate char(8);
update orders set orderdate = orderdate2;
alter table orders drop column orderdate2;
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34151597
>>in  SQL server 2008 date values is comming yyyy-dd-mm<<
As cyberkiwi has pointed out that is simply not correct.  You are confusing SQL Server 2008 with SSMS.  SSMS is the application used to view data in MS SQL Server.  The date data type has no interal formatting whatsoever.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34153616
>it will show only that time, i need to change Permently.

can you please explain why you need that?

I presume that a computed column would be the "best" solution, as having a date stored in varchar field with that format you request is highly ... unprofessional, as it WILL result in unefficient queries...
0
 

Author Comment

by:lankapala
ID: 34256325
Not working
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34257033
WHAT is not working?
WHAT is the error message?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34257861
lankapala,

>>Not working<<
If you honestly want to get a solution to your question consider providing more feedback.

Supposing your took your car to be repaired and told your mechanic that it was "Not working".  What do you think would be his/her response?
0
 

Author Comment

by:lankapala
ID: 34329027
Appologies for puting not working,
I need to permentaly change date type dd/mm/yyyy ,
below coding is working perfectly,but its not DATE type
update orders set orderdate2 = convert(char(8), orderdate, 3);
alter table orders alter column orderdate char(8);
update orders set orderdate = orderdate2;
alter table orders drop column orderdate2;

I need to change it UK format.

i try using below coding its not working
USE master
GO
ALTER LOGIN Ryan WITH DEFAULT_LANGUAGE = British
GO
SET DATEFORMAT dmy;
EXEC sp_defaultlanguage 'Ryan', 'british'
SELECT * FROM sys.syslanguages


SET DATEFIRST 1

i try below coding
ALTER TABLE Orders
ALTER COLUMN  OrderDate  convert(char(8), OrderDate, 3)

error showing
Incorrect syntax near the keyword 'convert'.


Can any one help me to solve this issue
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34330102
What is the datatype of the column OrderDate?
If it is CHAR containing yyyy-mm-dd, and you want it changed to DATE then

alter table orders add orderdate2 datetime; -- or DATE for sql 2008
update orders set orderdate2 = case when isdate(orderdate) then orderdate end;
alter table orders alter column orderdate datetime;
update orders set orderdate = orderdate2;
alter table orders drop column orderdate2;

If it is ALREADY DATE(TIME), then you're going up a steep hill.  There is no inherent format to DATE fields.
SSMS will ALWAYS show a datetime (unless you convert it to some specific char format) as "yyyy-mm-dd hh:mm:ss.zzz" because you CANNOT set a "display format" against a DATE(TIME) field.
If you want you can add a computed column that will always display dd/mm/yy though.

ALTER TABLE orders ADD OrderDateDisplay convert(varchar,orderdate,3)

Finally, you also have a very confused question
yyyy-dd-mm (eg:2010-11-16)
Is it DD-MM or MM-DD ???

If it is REALLY coming in as varchar yyyy-dd-mm, AND the field orderdate is currently VARCHAR, then add the dateformat before using the first block of code in this comment

set dateformat ydm
-- remember to set it back to dmy after the operation
-- set dateformat dmy
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34330105
I really have to repeat this

There is no inherent format to DATE(TIME) fields.
SSMS will ALWAYS show a datetime (unless you convert it to some specific char format) as "yyyy-mm-dd hh:mm:ss.zzz" because you CANNOT set a "display format" against a DATE(TIME) field.
0
 

Author Comment

by:lankapala
ID: 34452116
ok
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 34709669
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question