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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Date in desired format

Author  Topic 

kodali_sk
Starting Member

3 Posts

Posted - 2008-10-29 : 14:38:31
Hello,

In a table, I have two date columns: Start_Date and End_Date.

Suppose Start_Date has value: 2008-10-30 13:00:00.000
End_Date has value: 2008-10-30 16:00:00.000

From these values, I want a string which is in the format:

10/28/2008 1.00PM - 4.00PM

Can someone please help me how to do it in SQL.

Thank you.

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-29 : 14:40:45
what if the end date is a different date?
Go to Top of Page

kodali_sk
Starting Member

3 Posts

Posted - 2008-10-29 : 14:44:26
For my purpose, start date and end date are always going to be same. Only time differs.
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-29 : 14:59:23
[code]
declare @start_date datetime
declare @end_date datetime
set @start_date = '2008-10-30 13:00:00.000'
set @end_date = '2008-10-30 16:00:00.000'

select convert(varchar(10), @start_date,101)+right(convert(varchar(20), @start_date,0),7)
+'-'+right(convert(varchar(20), @end_date,0),7)


--------------------------
10/30/2008 1:00PM - 4:00PM

(1 row(s) affected)
[/code]
Go to Top of Page

kodali_sk
Starting Member

3 Posts

Posted - 2008-10-29 : 15:01:35
Awesome.... this is exactly what I wanted. Thank you so much :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-30 : 03:56:09
Where do you want to show formatted dates?
If you use front end application, do formation there


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -