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 |
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-12 : 06:37:18
|
| Ok so I have for example the following querySELECT form_data_id, user_id, CONVERT(VARCHAR(50), form_field_value) AS BookingDateFROM form_data_tblWHERE (user_id = 30) AND (form_field_name = 'bookingDate')UNIONSELECT form_data_id, user_id, CONVERT(VARCHAR(50), form_field_value) AS RoomIDFROM form_data_tbl AS form_data_tbl_1WHERE (user_id = 30) AND (form_field_name = 'roomId')And I have 6 or so more "Form_Field_Names" now when I run the following query I get returned 2 30 12 30 12/02/20103 30 13 30 12/02/20104 30 14 30 12/02/2010Is it possible that it could return the results for each form_data_id on a single row so that i.e for 2 i get2 30 1 12/02/20103 30 1 12/02/20104 30 1 12/02/2010 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-12 : 06:41:26
|
| [code]SELECT form_data_id, user_id, MAX( CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS BookingDate,MAX( CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomIDFROM form_data_tblWHERE (user_id = 30) GROUP BY form_data_id, user_id[/code] |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-12 : 07:07:51
|
| Thanks that worked somewhat finally I also have a column startTime and StartMinute so I have BookingDate i.e 12/02/2009 Start Time ie 5 and Start Minute 15...Is it possible also that at the end it sorts by BookingDate, then Start Hours, Then Start Minuteso i.e I would 12/02/2009 3 15 12/02/2009 3 30 12/02/2009 4 15 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-12 : 07:10:48
|
quote: Originally posted by velnias2010 Thanks that worked somewhat finally I also have a column startTime and StartMinute so I have BookingDate i.e 12/02/2009 Start Time ie 5 and Start Minute 15...Is it possible also that at the end it sorts by BookingDate, then Start Hours, Then Start Minuteso i.e I would 12/02/2009 3 15 12/02/2009 3 30 12/02/2009 4 15
sorry didnt get that. do you mean you've time part stored in separate field? |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-12 : 07:17:29
|
| Yes Its Similar to What I had in first queryMAX( CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS BookingDate,MAX( CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomIDSo that was BookingDate and RoomIDI have 2 other Columns Called Start Time and Start Minute so I would gather them as follows MAX(CASE WHEN form_field_name = 'startMinute' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS startMinute, MAX(CASE WHEN form_field_name = 'startTime' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS startTimeFinally I have BookingDate,StartMinute and StartTime (STart Time being the Hours) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-12 : 07:29:34
|
| ok and you want to order based on them? then just use order by bookingdate,startminute,... |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-12 : 07:40:19
|
I would have used a FULL OUTER JOIN:SELECT COALESCE(T1.form_data_id, T2.form_data_id) AS form_data_id, COALESCE(T1.user_id, T2.user_id) AS user_id, CONVERT(VARCHAR(50), T1.form_field_value) AS BookingDate, CONVERT(VARCHAR(50), T2.form_field_value) AS RoomIDFROM form_data_tbl AS T1 FULL OUTER JOIN form_data_tbl AS T2 ON T2.user_id = 30 AND T2.form_field_name = 'roomId'WHERE T1.user_id = 30 AND T1.form_field_name = 'bookingDate' |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-12 : 09:10:48
|
| Ok so basically I haveSELECT form_data_id, user_id, MAX(CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS BookingDate, MAX(CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomID, MAX(CASE WHEN form_field_name = 'startDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS startDateFROM form_data_tblWHERE (user_id = 30)GROUP BY form_data_id, user_idORDER BY startDate DESCWhich works fine (I got rid of Hours and Minutes just working with a single Date Object)Now Im wondering...how could I set that only future dates should be allowedi.e BookingDate > NOW()...when i try it says bookingDate is in invalid column and also im guessing columns needs to be DATETIME |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-12 : 09:57:12
|
Wrap your query in:SELECT *FROM( ... query here ...) AS XWHERE bookingDate > GetDate() but note that in the inner query you have converted bookingDate to VARCHAR, so the conversion and handling will be dubious.Much better to leave as DATETIME datatype and handle formatting of the date to the application, but failing that covert it as the LAST step - i.e. replace the SELECT * in my example with a list of columns and data type conversion.SELECT form_data_id, user_id, [BookingDate] = CONVERT(varchar(24), BookingDate, 113), -- Choose your Date Format here RoomIDFROM( SELECT COALESCE(T1.form_data_id, T2.form_data_id) AS form_data_id, COALESCE(T1.user_id, T2.user_id) AS user_id, -- NOTE: The date format needs to be recognised here!! 103 = dd/mm/yyyy CONVERT(DATETIME, T1.form_field_value, 103) AS BookingDate, CONVERT(INT, T2.form_field_value) AS RoomID -- RoomID is INT, right? FROM form_data_tbl AS T1 FULL OUTER JOIN form_data_tbl AS T2 ON T2.user_id = 30 AND T2.form_field_name = 'roomId' WHERE T1.user_id = 30 AND T1.form_field_name = 'bookingDate') AS XWHERE BookingDate > GetDate() |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-13 : 05:25:40
|
| Ah Cool...Another Thing Bothering me...I would like to pass a 2 paramaters to the above query which are startDate and EndDate and I would like to compare them with all the other Start and EndDates in that table and see if they dont overlap with the ones in there. I.e can I Make a booking for then or is there dates overlapping with it. StartDate and EndDate are 2 other columns like BookingDate above |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-13 : 05:43:36
|
quote: Originally posted by velnias2010 Ah Cool...Another Thing Bothering me...I would like to pass a 2 paramaters to the above query which are startDate and EndDate and I would like to compare them with all the other Start and EndDates in that table and see if they dont overlap with the ones in there. I.e can I Make a booking for then or is there dates overlapping with it. StartDate and EndDate are 2 other columns like BookingDate above
just check that in where condition likewhere (StartDate < @StartDateand EndDate >@startdate)or (StartDate <@Endateand EndDate >@EndDate) |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-14 : 05:32:27
|
| My Query is as follows :SELECT user_id, form_data_id, BookingDate, roomId, CONVERT(varchar(24), startDate, 113) AS StartDate, CONVERT(varchar(24), endDate, 113) AS EndDate, RoomNameFROM (SELECT TOP (100) PERCENT form_data_id, MAX(CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS BookingDate, MAX(CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS roomId, MAX(CASE WHEN form_field_name = 'roomName' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomName, MAX(CASE WHEN form_field_name = 'startDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS startDate, MAX(CASE WHEN form_field_name = 'endDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS endDate, user_id FROM form_data_tbl GROUP BY form_data_id, user_id ORDER BY startDate) AS XWHERE (startDate > GETDATE()) AND (roomId = 7214)ORDER BY startDateWhich Works finesBut when I have an entry in my DB as follows13/12/2010 11:45:00It Breaksif i have12/12/2010 11:45:0011/12/2010 11:45:00Its fineHow can I have it so that it will accept dates such as14/12/2010 as this is how I would like the format to be |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-14 : 05:51:51
|
| This is the Error I get by the waySystem.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-14 : 07:20:56
|
| I think the issue is CONVERT(varchar(24), startDate, 113) AS StartDate doesnt seem to be creating as a proper date.When I get rid of startDate > GETDATE(), the error System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value does not appear by orders the date by day value i.e maybe as a string ?Then when I put it back in , I get the errorSystem.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.And this is when I have startDate with values greater than 12 for day. i.e 19/12/2009.But i dont think I have US formats somewhere cuz when i inserted GETDATE() it had proper UK Format.Anyone around to help on this , im really stuck |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-14 : 08:06:57
|
"CONVERT(varchar(24), startDate, 113) AS StartDate doesnt seem to be creating as a proper date."Just to reiterate what I said above "Much better to leave as DATETIME datatype and handle formatting of the date to the application"Note that in your code:SELECT ... FROM ...( SELECT ... MAX(CASE WHEN form_field_name = 'startDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END ) AS startDate, ...)...WHERE (startDate > GETDATE()) ... So the StartDate used in the WHERE clause will be the VARCHAR(50) one from the inner select. SQL will attempt to convert the varchar data to a datetime (i.e. implicitly). The only reliable way to do this is if the data is in 'yyyymmdd' format. For anything else you will have to explicitly specify a convert method to indicate what format the date is in - e.g. 'dd/mm/yyyy' or 'mm/dd/yyyy'Moreover, if you fail to do an explicit conversion, things such as '01/02/10' could be interpreted as 01-Feb-2010, 02-Jan-2010, or even 10-Feb-2001 !! and if your data doesn't happen to have a Day Number higher than 12 where SQL is anticipating a month you won't get any error message.Store your dates in a datetime datatype columns; if you must store them as text (e.g. variant datatype) use yyyymmdd format. Anything else will be trouble, I'm afraid. |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-14 : 08:23:48
|
| The Data type must remain NTEXT im afraidI have this nowSELECT user_id, endDate, startDate, CONVERT(DATETIME, startDate, 113) AS BookingDateFROM (SELECT TOP (100) PERCENT form_data_id, MAX(CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS BookingDate, MAX(CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS roomId, MAX(CASE WHEN form_field_name = 'roomName' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomName, MAX(CASE WHEN form_field_name = 'startDate' THEN CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 103), 103) ELSE NULL END) AS startDate, MAX(CASE WHEN form_field_name = 'endDate' THEN CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 101), 103) ELSE NULL END) AS endDate, user_id FROM form_data_tbl GROUP BY form_data_id, user_id ORDER BY startDate) AS XWHERE (startDate > GETDATE())ORDER BY startDatewhich sorts the dates correctly...what I cannot yet do is compare the date i.e start Date with a manual datei.e AND (startDate > '13/12/2009') Then i get that out of range error message again |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2010-01-14 : 08:30:49
|
| But ye If I do AND (startDate > '20101201') its fine..Anyway to get the (startDate > '13/12/2009') working with what I have |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-14 : 08:49:54
|
I think thisCONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 103), 103) should beCONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value), 103) i.e. you only want to apply the "103" to the conversion to DateTime. It will probably be ignored when converting to Varchar though, so only important for "tidyness".Beware thatCONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value), 103) will raise error if ANY of your form_field_value are not in valid 'dd/mmm/yyyy' format . There is an IsDate() function, but that has other problems associated with it (ask me if you do have potentially invalid dates in your form_field_value column)"Anyway to get the (startDate > '13/12/2009') working with what I have"Same issue here too, I'm afraid:(startDate > CONVERT(datetime, '13/12/2009', 103)) obviously if you can input the string-date in yyyymmdd format then no conversion needed. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-14 : 08:50:48
|
| P.S. You could also use string manipulation to convert '13/12/2009' to '20091213' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 09:46:32
|
quote: Originally posted by velnias2010 The Data type must remain NTEXT im afraidI have this nowSELECT user_id, endDate, startDate, CONVERT(DATETIME, startDate, 113) AS BookingDateFROM (SELECT TOP (100) PERCENT form_data_id, MAX(CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS BookingDate, MAX(CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS roomId, MAX(CASE WHEN form_field_name = 'roomName' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomName, MAX(CASE WHEN form_field_name = 'startDate' THEN CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 103), 103) ELSE NULL END) AS startDate, MAX(CASE WHEN form_field_name = 'endDate' THEN CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 101), 103) ELSE NULL END) AS endDate, user_id FROM form_data_tbl GROUP BY form_data_id, user_id ORDER BY startDate) AS XWHERE (startDate > GETDATE())ORDER BY startDatewhich sorts the dates correctly...what I cannot yet do is compare the date i.e start Date with a manual datei.e AND (startDate > '13/12/2009') Then i get that out of range error message again
can i ask why? by making it ntext arent you making date manipulations difficult? |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-14 : 09:54:10
|
| I think form_field_value a "store anything" column, based on what what is in form_field_name (roomName, bookingDate, startDate, endDate)Clearly not designed to be used as a relational database, and thus the problems that velnias2010 is encountering trying to make useful reports from it |
 |
|
|
Next Page
|
|
|
|
|