SQL Server - order

Asked By farsha
26-Jun-07 08:19 AM
Hi,
There is a field called contractMonth in a table called table1.
The datatype of this field is varchar(50).
The data is something like:

Apr 2007
June 2008
Sep 2007
...

How is it possible to order this field.
For example I would like to retrieve something like the following:
Apr 2007
sep 2007
June 2008
...

Thanks
Varchar
(1)
Dec
(1)
Bit
(1)
Database
(1)
RoyIn
(1)
ContractMonth
(1)
AndSeptember
(1)
September
(1)
  Raymond D'Anjou replied...
26-Jun-07 09:24 AM
...order by right(contractMonth, 4), contractMonth
  Uri Dimant replied...
26-Jun-07 08:31 AM
Hi
create table #t (c varchar(50))

insert into #t values  ('Apr 2007')
insert into #t values  ('June 2008')
insert into #t values  ('Sep 2007')
insert into #t values  ('Nov 2007')
insert into #t values  ('Feb 2006')

select * from #t order by convert(datetime,c,103) asc
  Russell Fields replied...
26-Jun-07 08:39 AM
farshad,

Assuming that the character strings are _always_ valid month year
combinations, you could do the following:

SELECT contractMonth
FROM table1
ORDER BY CAST (contractMonth AS DATETIME)

Of course, I would not trust all my contractMonth values to be valid unless
some process was already assuring that.  A better method would be to make
the contractMonth a datetime column and just make them all fall on the first
day of the month.  It is better to leverage the native capabilities of the
database than try to fool with less well defined data.

RLF
  Roy Harvey replied...
26-Jun-07 09:16 AM
If the data can not be converted to datetime (for example Sep and
September are fine, but Sept will not convert) you may have to resort
to extreme measures, perhaps something like:

ORDER BY CASE WHEN contractMonth LIKE '%2000%' THEN 2000
WHEN contractMonth LIKE '%2001%' THEN 2001
WHEN contractMonth LIKE '%2002%' THEN 2002
WHEN contractMonth LIKE '%2003%' THEN 2003
WHEN contractMonth LIKE '%2004%' THEN 2004
WHEN contractMonth LIKE '%2005%' THEN 2005
WHEN contractMonth LIKE '%2006%' THEN 2006
WHEN contractMonth LIKE '%2007%' THEN 2007
WHEN contractMonth LIKE '%2008%' THEN 2008
WHEN contractMonth LIKE '%2009%' THEN 2009
END,
CASE WHEN contractMonth LIKE '%jan%' THEN  1
WHEN contractMonth LIKE '%feb%' THEN  2
WHEN contractMonth LIKE '%mar%' THEN  3
WHEN contractMonth LIKE '%apr%' THEN  4
WHEN contractMonth LIKE '%may%' THEN  5
WHEN contractMonth LIKE '%jun%' THEN  6
WHEN contractMonth LIKE '%jul%' THEN  7
WHEN contractMonth LIKE '%aug%' THEN  8
WHEN contractMonth LIKE '%sep%' THEN  9
WHEN contractMonth LIKE '%oct%' THEN 10
WHEN contractMonth LIKE '%nov%' THEN 11
WHEN contractMonth LIKE '%dec%' THEN 12
END

Roy Harvey
Beacon Falls, CT

On Tue, 26 Jun 2007 05:19:02 -0700, farshad
  Uri Dimant replied...
26-Jun-07 09:26 AM
Hi Roy

In my opinion  it would be better to 'fix' the data  to be converted to
datetime insead of building so long long query. Image if he has
to sort contracts from 1950.....
Just my two cents
  Raymond D'Anjou replied...
26-Jun-07 10:53 AM
Forget this.
It was a holiday weekend in Quebec and I may have had a bit too much brew.
I should be back to normal tomorrow.
  Roy Harvey replied...
26-Jun-07 10:23 AM
On Tue, 26 Jun 2007 16:26:47 +0300, "Uri Dimant" <urid@iscar.co.il>


Of course it would be better to fix the data, if fixing the data (and
KEEPING it fixed!) is an option.  Of course a real fix would be to get
away from storing 'June 2008' and 'Sep 2007' in the first place and
use datetime.

Roy Harvey
Beacon Falls, CT
help
Server This error happen when I tried to insert a value too long in a varchar column: (insert "123456" in a column varchar(3)) Is there a way to tell the server to truncate the value and insert SQL Server 2005 (1) SQL Server 2000 (1) SQL Server (1) Nvarchar (1) Declare (1) Varchar (1) Proc (1) Bit (1) insert into my_table ( narrow_column , <other columns. . .> ) select left(blablabla, 3) , <other columns. . .> from some_other_table at http: / / www.microsoft.com / sql / prodinfo / previousversions / books.mspx Erland wrote on Tue, 02 Dec 2008 07:07:29 -0800: What would the performance implications of using the metadata function 2 for Unicode columns, and ignore it if the response is -1 (which is for varchar(max)), but otherwise this should allow the maximum string length to be determined dynamically in match it. - - Dan Daniel Crichton (msnews@worldofspack.com) writes: In my opinion this is a bit overambitious. And if this is code that is to be run plain users, keep in description: This error happen when I tried to insert a value too long in a varchar column: (insert 123456 in a column varchar(3)) Is there a way to tell the
Not dropped' * / GO CREATE Procedure dbo.ESIMonthly ( @ProcessDate DateTime = Null, @SubsidyPerChild SmallMoney = 100.00, @Rerun bit = 0, @Payments int OUTPUT, @TotalPaid Money OUTPUT ) AS SET NOCOUNT ON; / * Misc work areas * / Declare 4) Declare @RunDay Int Declare @MonthStart DateTime Declare @MonthEnd DateTime Declare @LastRun DateTime Declare @VendorName Varchar(30) Declare @VendorNameAlternate Varchar(15) Declare @BankAccountPerson Varchar(80) Declare @BankAccountType Varchar(25) Declare @NumParents Int Declare @NumChildren Int Declare @VendorId Int Declare @InvoiceId Int Declare @CycleID Int Declare @Reason Varchar(100) Declare @CalcSubsidy SmallMoney Declare @CalcEmployerContribution Decimal(5, 2) Declare @VendorIdentity Int Declare @InvoiceIdentity Int area code 999-9999 * / Declare @Return Int Declare @SubsidyCount Int Declare @SubsidyTotal SmallMoney Declare @LogMessage varchar(100) Declare @LoopSW bit / * Misc work areas * / / * For error logging * / Declare @Error int Declare @ErrorMessage nvarchar(4000) Declare @ErrorProcedure
to create ZPL code for printing labels. Table information: create table #test_label_creation (POnum int, supplier varchar(25), part int, recvqty dec(5, 5)) select POnum, supplier, part, recqty from #test_label_creation returns POnum supplier part recvqty 14114 may be able to offer. SQL Server Programming Discussions SQL Server (1) INNER JOIN (1) Varchar (1) Decimal (1) Desc (1) Bit (1) Dec (1) POnum (1) Hi j, First, make sure you have a table of numbers (google not null and c.cat_supplier = o.orl_supplier and o.orl_part = c.cat_part I agree the dec(5, 5) was a bit much on the received quantity, the level of precision is high from our canned application
fogetting some syntax this gives an error on the second 'into' CREATE PROCEDURE Assignments @Person varchar(65), @Project char(5), @Sep float, @Oct float, @Nov float, @Dec float, @Jan float, @Feb float, @Mar float, @Apr float, @May float, @Jun float, @Jul float, @Aug float, @cUser varchar(65), @Type varchar(10) As Begin Try If (@Type = 'New') begin insert into MonthHours (Person, Project, Sep, Oct, Nov, Dec, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, AssignBy) values (@Person, @Project, @Sep, @Oct, @Nov, @Dec, @Jan, @Feb, @Mar, @Apr, @May, @Jun, @Jul, @Aug, @cUser) insert into AssignmentLog(LogType, Person, Project Sep, Oct, Nov, Dec, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, cUser) values (@Type, @Person, @Project, @Sep, @Oct, @Nov, @Dec, @Jan, @Feb, @Mar, @Apr, @May, @Jun, @Jul, @Aug, @cUser) end IF (@Type = 'Modify') begin update