We can format the dates using convert statement in sql server.
In the below example, am trying to fetch the months available in the table. Considering am having 10 rows with different dates. On the high level, I need to fetch the available months in the table records
DROP TABLE SAMPLE
create table sample(id int, BusId varchar(5), amt1 decimal(10,2), amnt2 decimal(10,2), dat datetime)
insert into sample values(1, 'A1234', 100,200,'2008-12-03')
insert into sample values(2, 'A1234', 200,200,'2008-12-30')
insert into sample values(3, 'A1234', 300,200,'2008-12-31')
insert into sample values(4, 'A1234', 400,200,'2009-01-03')
insert into sample values(5, 'A1234', 500,200,'2009-01-22')
insert into sample values(6, 'A1234', 600,200,'2009-02-13')
select * from sample
select * from
(
select CONVERT(CHAR(4), dat, 100) + CONVERT(CHAR(4), dat, 120) AS DAT1 from sample
)t
group by dat1

Venkatesan Prabu .J
No comments:
Post a Comment