A user is having the datetime format in month/day/year and he want to store the data in the format of day/month/year. (A typical datetime format operation in sql server).
It can be achieved using convert function,
Convert function will convert the data into required format. The syntax is,
Convert(datatype, data (or) columnname , format you want)
For example,
convert(varchar(10),dat,103) will fetch the output in the format of date/month/year format.
Let's see a small example to check it.
drop table venkat1
create table venkat1(id int, rating int, dat datetime,auditrecord int)
insert into venkat1 values(1,1,'7/1/2006',1)
insert into venkat1 values(1,3,'7/1/2006',2)
insert into venkat1 values(1,5,'7/2/2006',3)
insert into venkat1 values(1,2,'8/10/2006',4)
insert into venkat1 values(2,4,'8/7/2006',1)
select * from venkat1
select dat from venkat1
select convert(varchar(10),dat,103) from venkat1
select convert(varchar(10),dat,101) from venkat1
Another option, a lengthy operation of doing the above functionality
select convert(datetime,convert(varchar(10),day(dat) ) +'/'+ convert(varchar(10),month(dat) ) +'/' + convert(varchar(10),year(dat)))as da from venkat1
Happy Learning!!!
Thanks and Regards,
Venkatesan prabu .J
No comments:
Post a Comment