Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to order by month which is varchar field according to date format?


How to order by month which is varchar field according to date format?

Author
Message
naisu1383
naisu1383
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 33
I have month field in format as JAN-12 with data type varchar. Now i wan to order by this field converting it into date like jun-12 then jul-12. Currently it orders like jul-12 then jun-12 as it is in varchar format. So how can this be achieved?
rajprabuit
rajprabuit
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 20
You can add another column 'YearMonth' in which u should store values like '201201','201202'....

If u do this, u can sort month field easily
spaghettidba
spaghettidba
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5730 Visits: 13305
You can order by the datetime column, even if you don't include it in the select list.

--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14942 Visits: 38939
any solution is going to involve some sort of convert text to date , or a hard coded CASE statement int he order by.

can you fix the incoming data to return a datetime instead of the text version you are currently receiving?


this is a quick and dirty bandaid:

SELECT * FROM SOMETABLE
ORDER BY
CASE
WHEN monthfield LIKE 'jan%' THEN 1
WHEN monthfield LIKE 'feb%' THEN 2
WHEN monthfield LIKE 'mar%' THEN 3
WHEN monthfield LIKE 'apr%' THEN 4
WHEN monthfield LIKE 'may%' THEN 5
WHEN monthfield LIKE 'jun%' THEN 6
WHEN monthfield LIKE 'jul%' THEN 7
WHEN monthfield LIKE 'aug%' THEN 8
WHEN monthfield LIKE 'sep%' THEN 9
WHEN monthfield LIKE 'oct%' THEN 10
WHEN monthfield LIKE 'nov%' THEN 11
WHEN monthfield LIKE 'dec%' THEN 12
ELSE 13
END,monthfield



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3445 Visits: 33014

USE [tempdb]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[testmonths]') AND type in (N'U'))
DROP TABLE [dbo].[testmonths]
GO

CREATE TABLE [dbo].[testmonths](
   [testMonth] [varchar](50) NULL
) ON [PRIMARY]

GO

INSERT INTO [dbo].[testmonths]([testMonth])
SELECT N'May-11' UNION ALL
SELECT N'May-12' UNION ALL
SELECT N'May-13' UNION ALL
SELECT N'Jun-12' UNION ALL
SELECT N'Jul-12' UNION ALL
SELECT N'Aug-13' UNION ALL
SELECT N'Jun-11' UNION ALL
SELECT N'Jul-11' UNION ALL
SELECT N'Aug-11' UNION ALL
SELECT N'Aug-12' UNION ALL
SELECT N'Jun-13' UNION ALL
SELECT N'Jul-13'


SELECT testMonth
FROM testmonths
ORDER BY CONVERT( datetime , '01-' + testMonth );




________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

naisu1383
naisu1383
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 33
Thnx... J Livingston SQL it did worked...
subbu1
subbu1
SSC Eights!
SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)

Group: General Forum Members
Points: 947 Visits: 695
pls try below code
declare @t1 table(id varchar(10))
insert into @t1(id)values('Jan-12'),('Feb-12'),('Mar-12'),('APR-12')
select *,convert(date,convert(varchar(10),'01'+'-'+id)) new from @t1
order by 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search