| Author |
Topic |
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-02-04 : 23:19:10
|
| My dilema would probably be nothing to some of you guys but I'm still new to tsql. I have a bunch of info that I need to have ordered by date. I have tried the following:ORDER BY Due_Date DESCI have my dates returned in the format : dd/mm/yyyyBut it only orders the days not months or years.heres the output:28/02/200420/02/200419/02/200410/12/2003 // as you can see this one should be at the top05/02/200405/02/200405/02/200413/02/200405/02/200401/02/2004Any help would be well appreciated |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2004-02-04 : 23:24:19
|
| They're stored as character data, not datetimes. Try this:ORDER BY CONVERT(datetime, Due_Date, 102) DESC |
 |
|
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-02-05 : 01:28:01
|
| I got this error. I did use a Union command.Server: Msg 104, Level 15, State 1, Line 7ORDER BY items must appear in the select list if the statement contains a UNION operator. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-02-05 : 08:45:25
|
| so make sure you add that expression to your SELECT list. you don't need to use the value, just make sure it is calculated.- Jeff |
 |
|
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-02-05 : 19:10:06
|
| I'm sorry to be so slow. Could you modify the change to my select as needed. Here it is:SELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact, dbo.DatePart(Master_Jobs.Due_Date) as Due_Date, Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_WhoThanks |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-02-05 : 19:13:01
|
| Where's the rest of your SELECT statement? Post the entire thing and then we'll see if we can fix it up.Tara |
 |
|
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-02-05 : 20:03:01
|
| --CREATE PROCEDURE spGetJobsByUnreadAndReadByUserID DECLARE @UserID INT-- ASSET @UserID = 5SELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact, dbo.DatePart(Master_Jobs.Due_Date) as Due_Date, Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_WhoFROM Master_Jobs INNER JOINNote ON Master_Jobs.JobID = Note.FK_JobID INNER JOINBackup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOINUser_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOINJob_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOINProfiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOINUsers ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND Job_Assignments.UserID = Users.UserIDWHERE Users.UserID = @UserID AND BackUp_Read = 'Read' AND Master_Jobs.JobID not in (SELECT Master_Jobs.JobIDFROM Master_Jobs INNER JOINNote ON Master_Jobs.JobID = Note.FK_JobID INNER JOINBackup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOINUser_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOINJob_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOINProfiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOINUsers ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND Job_Assignments.UserID = Users.UserIDWHERE Users.UserID = @UserID AND BackUp_Read = 'UnRead')unionSELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact, dbo.DatePart(Master_Jobs.Due_Date) as Due_Date, Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_WhoFROM Master_Jobs INNER JOINNote ON Master_Jobs.JobID = Note.FK_JobID INNER JOINBackup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOINUser_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOINJob_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOINProfiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOINUsers ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND Job_Assignments.UserID = Users.UserIDWHERE Users.UserID = @UserID AND-- Note.FK_UserID = User_Notes.FK_UN_UserID AND BackUp_Read = 'UnRead'--ORDER BY BackUp_Read DESC, JobID DESCORDER BY CONVERT(datetime,Due_Date, 102) DESC GO |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-02-05 : 20:07:39
|
| I don't receive an error when I compile your code in Query Analyzer 2000.Tara |
 |
|
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-02-05 : 20:43:12
|
| I dont receive an error when I click the blue tick but when I click the green play button I get:Server: Msg 104, Level 15, State 1, Line 6ORDER BY items must appear in the select list if the statement contains a UNION operator. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-02-05 : 20:51:07
|
| Instead of "dbo.DatePart(Master_Jobs.Due_Date) as Due_Date" in both SELECTs, change it to:CONVERT(datetime,Due_Date, 102) AS Due_DateYou aren't using DATEPART correctly anyway.Tara |
 |
|
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-02-05 : 20:59:31
|
| I'm going to give that a try. By the way that datepart is one of my own functions. Although I could have chosen a better name. |
 |
|
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-02-05 : 21:04:48
|
| It worked!! Only problem is that I needed that DatePart function so that I would only get the date and not the time on the end. Is there anyway I could combine what you gave me with my datepart function. This is it:CREATE FUNCTION dbo.DatePart ( @fDate datetime )RETURNS varchar(10)ASBEGIN RETURN ( CONVERT(varchar(10),@fDate,103) )END |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-02-06 : 12:04:53
|
| DATEPART can't be one of your own functions. It is a SQL Server function already. Are you even using SQL Server?DATEPART gives you the month, or the year, or the day, but not month-day-year. CONVERT gives you just the day. Look up CONVERT in SQL Server Books Online. You are interested in the styles. So remove DATEPART and put in:CONVERT(varchar(10), Due_Date,103) AS Due_DateTara |
 |
|
|
|