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.
| Author |
Topic |
|
ranta007
Starting Member
8 Posts |
Posted - 2007-06-22 : 11:09:51
|
| I am new to MS SQL coding and I am having a problem with date conversions. In PL/SQL, I could convert numeric months into character months in their own columns by using the DECODE function. An example would be: DECODE(to_char(M.My_Table, 'MM'), '04', 'Apr', '05', 'May', '06', 'Jun', '07', 'Jul', '08', 'Aug', '09', 'Sep', '10', 'Oct', '11', 'Nov', '12', 'Dec', '01', 'Jan', '02', 'Feb', '03', 'Mar'), DECODE(to_char(M.My_Table, 'MM'), '04', 'Q1', '05', 'Q1', '06', 'Q1', '07', 'Q2', '08', 'Q2', '09', 'Q2', '10', 'Q3', '11', 'Q3', '12', 'Q3', '01', 'Q4', '02', 'Q4', '03', 'Q4'),How does one convert something like this in MS SQL? |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-06-22 : 11:13:20
|
| right('00' + convert(varchar(2),datepart(dd,dte)),2)left(datename(month,dte),3)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-06-22 : 11:15:47
|
Use a CASE. You can read about CASE in SQL Server Books Online.select case MyTable.MM when '01' then 'Jan' when '02' then 'Feb' ... and so on.. when '12' then 'Dec' end CODO ERGO SUM |
 |
|
|
ranta007
Starting Member
8 Posts |
Posted - 2007-06-22 : 11:17:08
|
| Thanks for such a quick answer. Does that work also for converting and sorting the months into quarters? |
 |
|
|
ranta007
Starting Member
8 Posts |
Posted - 2007-06-22 : 11:23:00
|
| Thanks for all the help all. I really apreciate it.It's not the strongest who will survive;nor is it the most intelligent;but the one most responsive to change. |
 |
|
|
ranta007
Starting Member
8 Posts |
Posted - 2007-06-22 : 12:27:25
|
| I tried using the CASE coding and was getting a syntax error "Cannot call methods on varchar". When I took out the .MM from the coding it would return my else statement of No Entry.CASE c.INFO_DATE.MM when '01' then 'Jan' when '02' then 'Feb' when '03' then 'Mar' when '04' then 'Apr' when '05' then 'May' when '06' then 'Jun' when '07' then 'Jul' when '08' then 'Aug' when '09' then 'Sep' when '10' then 'Oct' when '11' then 'Nov' when '12' then 'Dec' else 'No Entry' end as 'Event Month',I've tried the SQL Server Books Online but nothing shows up for this.========================It's not the strongest who will survive;nor is it the most intelligent;but the one most responsive to change. |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-06-22 : 13:23:43
|
| Explain c.INFO_DATE.MM? Who is the db, who is the owner, who is the table?Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
ranta007
Starting Member
8 Posts |
Posted - 2007-06-22 : 15:59:59
|
| Here is my query in whole.SELECT O.fn AS 'FTS', O.FullName, c.WRK_ID AS 'WRK ID', om1.SHORT_DESC_E AS 'WRK Type', c.WRK_ENTRY_DATE AS 'Entry Date', O.MANAGE_LOCATION AS 'Present Location', CASE c.WRK_SOURCE_INFO_DATE when '01' then 'Jan' when '02' then 'Feb' when '03' then 'Mar' when '04' then 'Apr' when '05' then 'May' when '06' then 'Jun' when '07' then 'Jul' when '08' then 'Aug' when '09' then 'Sep' when '10' then 'Oct' when '11' then 'Nov' when '12' then 'Dec' else 'No Entry' end as 'Event Month', ctc.SURNAME, ctc.FIRSTNAME, d.Date_Of_Creation FROM rts_local.dbo.mainwork c, rts_local.dbo.ctc_personnel_table rts, profile.dbo.date d, profile.dbo.CLIENT O, rts_local.dbo.oml_table om, rts_local.dbo.oml_table om1 WHERE c.OID = O.oid AND c.WRK_RECORD_FINAL_FLAG = om.INTERNAL_CODE AND c.WRK_RECORDED_BY = rts.CTC_NUMBER AND c.WRK_SOURCE_INFO_CODE = om1.INTERNAL_CODE AND ((c.COMPLETING_OFFICE = '89900') AND (om.TABLE_TYPE_CODE = '0499') AND (om1.TABLE_TYPE_CODE = '0639') AND (c.WRK_SOURCE_INFO_DATE Between '2007-04-01' And '2008-03-31')) ORDER BY c.WRK_RECORD_FINAL_DATE DESC========================It's not the strongest who will survive;nor is it the most intelligent;but the one most responsive to change. |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-06-22 : 16:20:55
|
You have an older way of joining tables and perhaps missing a few joins too. Use the ANSI standard.SELECT..FROM TableA AJOIN TableB B ON A.somecolumn = b.somecolumnJOIN TableC C on B.Somecol2 = C.Somecolumn3WHERE A.somethin = 'xx'AND B.Somethingelse = 'vv' Instead ofSELECT..FROM TableA A, TableB B TableC C Where A.somecolumn = b.somecolumnAnd B.Somecol2 = C.Somecolumn3And A.somethin = 'xx'AND B.Somethingelse = 'vv' Aside from this, is the WRK_SOURCE_INFO_DATE numeric or string type? Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-06-22 : 16:21:14
|
It is not valid to have column name in single quotes. Use double quotes or use brackets.SELECT O.fn as [FTS], O.FullName, c.WRK_ID as [WRK ID], om1.SHORT_DESC_E as [WRK Type], c.WRK_ENTRY_DATE as [Entry Date], O.MANAGE_LOCATION as [Present Location], CASE c.WRK_SOURCE_INFO_DATE when '01' then 'Jan' when '02' then 'Feb' when '03' then 'Mar' when '04' then 'Apr' when '05' then 'May' when '06' then 'Jun' when '07' then 'Jul' when '08' then 'Aug' when '09' then 'Sep' when '10' then 'Oct' when '11' then 'Nov' when '12' then 'Dec' else 'No Entry' end as [Event Month], ctc.SURNAME, ctc.FIRSTNAME, d.Date_Of_CreationFROM rts_local.dbo.mainwork c, rts_local.dbo.ctc_personnel_table rts, profile.dbo.date d, profile.dbo.CLIENT O, rts_local.dbo.oml_table om, rts_local.dbo.oml_table om1WHERE c.OID = O.oid AND c.WRK_RECORD_FINAL_FLAG = om.INTERNAL_CODE AND c.WRK_RECORDED_BY = rts.CTC_NUMBER AND c.WRK_SOURCE_INFO_CODE = om1.INTERNAL_CODE AND ((c.COMPLETING_OFFICE = '89900') AND (om.TABLE_TYPE_CODE = '0499') AND (om1.TABLE_TYPE_CODE = '0639') AND (c.WRK_SOURCE_INFO_DATE Between '2007-04-01' And '2008-03-31'))ORDER BY c.WRK_RECORD_FINAL_DATE DESC CODO ERGO SUM |
 |
|
|
ranta007
Starting Member
8 Posts |
Posted - 2007-06-22 : 17:32:00
|
quote: Originally posted by dinakar You have an older way of joining tables and perhaps missing a few joins too. Use the ANSI standard.SELECT..FROM TableA AJOIN TableB B ON A.somecolumn = b.somecolumnJOIN TableC C on B.Somecol2 = C.Somecolumn3WHERE A.somethin = 'xx'AND B.Somethingelse = 'vv' Instead ofSELECT..FROM TableA A, TableB B TableC C Where A.somecolumn = b.somecolumnAnd B.Somecol2 = C.Somecolumn3And A.somethin = 'xx'AND B.Somethingelse = 'vv' Aside from this, is the WRK_SOURCE_INFO_DATE numeric or string type? Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/
========================It's not the strongest who will survive;nor is it the most intelligent;but the one most responsive to change. |
 |
|
|
ranta007
Starting Member
8 Posts |
Posted - 2007-06-22 : 17:33:20
|
quote: Originally posted by dinakar You have an older way of joining tables and perhaps missing a few joins too. Use the ANSI standard.SELECT..FROM TableA AJOIN TableB B ON A.somecolumn = b.somecolumnJOIN TableC C on B.Somecol2 = C.Somecolumn3WHERE A.somethin = 'xx'AND B.Somethingelse = 'vv' Instead ofSELECT..FROM TableA A, TableB B TableC C Where A.somecolumn = b.somecolumnAnd B.Somecol2 = C.Somecolumn3And A.somethin = 'xx'AND B.Somethingelse = 'vv' Aside from this, is the WRK_SOURCE_INFO_DATE numeric or string type? This string is numberic with a date format of yyyy-mm-dd (i.e. 2007-06-22).Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/
========================It's not the strongest who will survive;nor is it the most intelligent;but the one most responsive to change. |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-06-22 : 17:39:16
|
Well.. how can it be numeric if the values are like "2007-06-22" ? you need to change this :CASE c.WRK_SOURCE_INFO_DATE when '01' then 'Jan' when '02' then 'Feb' when '03' then 'Mar' when '04' then 'Apr' when '05' then 'May' when '06' then 'Jun' when '07' then 'Jul' when '08' then 'Aug' when '09' then 'Sep' when '10' then 'Oct' when '11' then 'Nov' when '12' then 'Dec' else 'No Entry' end as [Event Month], to this:CASE MONTH(c.WRK_SOURCE_INFO_DATE) when 1 then 'Jan' when 2 then 'Feb' when 3 then 'Mar' when 4 then 'Apr' when 5 then 'May' when 6 then 'Jun' when 7 then 'Jul' when 8 then 'Aug' when 9 then 'Sep' when 10 then 'Oct' when 11 then 'Nov' when 12 then 'Dec' else 'No Entry' end as [Event Month], Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-06-22 : 18:01:50
|
I find this quite easierDECLARE @Test TABLE (dt DATETIME)INSERT @TestSELECT NULL UNION ALLSELECT '20070101' UNION ALLSELECT '20070201' UNION ALLSELECT '20070301' UNION ALLSELECT '20070401' UNION ALLSELECT '20070501' UNION ALLSELECT '20070601' UNION ALLSELECT '20070701' UNION ALLSELECT '20070801' UNION ALLSELECT '20070901' UNION ALLSELECT '20071001' UNION ALLSELECT '20071101' UNION ALLSELECT '20071201'SELECT dt, CASE WHEN dt IS NULL THEN 'No Entry' ELSE LEFT(DATENAME(MONTH, dt), 3) END AS [Event Month], CASE WHEN dt IS NULL THEN 'No Entry' ELSE 'Q' + DATENAME(QUARTER, dt) END AS [Event Quarter]FROM @Test Peter LarssonHelsingborg, Sweden |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-06-22 : 18:56:06
|
| Peso is right..there's the DateName function you can use..Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
ranta007
Starting Member
8 Posts |
Posted - 2007-06-22 : 19:14:27
|
| Thanks for all your help. I'm new to SQL Coding and not ashamed to admit it. I have obviously a lot to learn and I appreciate all of you taking the time to help me with this problem of mine.I'm done for the weekend now but I'll try it on Monday when I get in. Have a good weekend all.========================It's not the strongest who will survive;nor is it the most intelligent;but the one most responsive to change. |
 |
|
|
|
|
|
|
|