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
 Sorting Date

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-11-17 : 10:24:28
saras writes "In sql server ,How to Sort a date which is converted into varchar.I need a single column"

X002548
Not Just a Number

15586 Posts

Posted - 2006-11-17 : 10:27:31
Rob is now just messing with us



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2006-11-17 : 10:29:37
While I did kill a few quality brain cells last night, I just can't muster the ability to come up with these questions myself.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-11-17 : 13:35:29
unfortunatly there's no way simple way to order dates converted to varchar.

you could try this workaround:

declare @t1 table (col1 varchar(200))
insert into @t1
select convert(varchar(200), getdate()+4, 107) union all
select convert(varchar(200), getdate()+1, 107) union all
select convert(varchar(200), getdate()+3, 107) union all
select convert(varchar(200), getdate()+2, 107) union all
select convert(varchar(200), getdate(), 107)

SELECT * FROM @t1

select distinct t1.*
from @t1 t1
cross join @t1 t2
where t1.col1 <= t2.col1




Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2006-11-17 : 13:37:16
NERD

LOSER

NERD



How you doing?

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-11-17 : 13:39:48
derrick where have you been lately... we missed you



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2006-11-17 : 14:00:23
I've took a position at AMC Theatres running the information services department. Now, I'm buried. I happen to be at PASS this week, so I have more time to post. Hopefully, my time will begin to free up as we get structure, processes, etc in place and start to work a plan. You know how new jobs go.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-11-17 : 14:05:09
quote:
Originally posted by AskSQLTeam

saras writes "In sql server ,How to Sort a date which is converted into varchar.I need a single column"



Use an ORDER BY clause.



CODO ERGO SUM
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-11-17 : 14:11:16
you ruin all the fun Michael



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2006-11-17 : 21:20:44
No, I'm gonna ruin it:

SELECT varcharDateColumn FROM myTable ORDER BY cast(varcharDateColumn as datetime)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-11-18 : 04:35:19
quote:
Originally posted by AskSQLTeam

saras writes "In sql server ,How to Sort a date which is converted into varchar.I need a single column"

You should always use proper datatype DATETIME to store dates

Madhivanan

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

X002548
Not Just a Number

15586 Posts

Posted - 2006-11-20 : 15:02:25
quote:
Originally posted by robvolk

No, I'm gonna ruin it:

SELECT varcharDateColumn FROM myTable ORDER BY cast(varcharDateColumn as datetime)



That could blow up if one of the values is not valid, no?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-11-20 : 15:07:44
Obviously, the application will ensure that there are no bad dates in the varchar date column.







CODO ERGO SUM
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-11-20 : 15:10:29
quote:
Originally posted by Michael Valentine Jones

Obviously, the application will ensure that there are no bad dates in the varchar date column.



[smacks forehead]
What was I thinking
[/smacks forehead]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -