piheshpi90 Posted February 8, 2011 Share Posted February 8, 2011 i want to select month from date.i try using datepart but it produce errors say -->FUNCTION book.DATEPART does not exist my sql is like below: "SELECT * FROM aktiviti WHERE DATEPART(MONTH,date_n) = 's' "; what is wrong? Quote Link to comment https://forums.phpfreaks.com/topic/227023-select-month-from-date/ Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2011 Share Posted February 8, 2011 SELECT * FROM aktiviti WHERE MONTH(`date_n`) = Quote Link to comment https://forums.phpfreaks.com/topic/227023-select-month-from-date/#findComment-1171278 Share on other sites More sharing options...
piheshpi90 Posted February 8, 2011 Author Share Posted February 8, 2011 that does not help. it show all data. i try SELECT * FROM aktiviti WHERE month(`date_n`) = s; month(`date_n`) will return month value right? Quote Link to comment https://forums.phpfreaks.com/topic/227023-select-month-from-date/#findComment-1171333 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2011 Share Posted February 8, 2011 Yes, it should. Can you explain exactly what result you're trying to get? Quote Link to comment https://forums.phpfreaks.com/topic/227023-select-month-from-date/#findComment-1171442 Share on other sites More sharing options...
piheshpi90 Posted February 9, 2011 Author Share Posted February 9, 2011 date_n in my table is save as date data type.. so it is save as YYYY-MM-DD format. i want my sql to extract MM value from YYYY-MM-DD so that if i key in 09, it will display data where MM value is equal to 09. :-\ :-\ Quote Link to comment https://forums.phpfreaks.com/topic/227023-select-month-from-date/#findComment-1171611 Share on other sites More sharing options...
Pikachu2000 Posted February 9, 2011 Share Posted February 9, 2011 This works. If it doesn't, you need to echo your query and see what the problem is with it. I suspect it's the 's' on the end that's causing you problems, since 's' will never equal 9. SELECT * FROM aktiviti WHERE MONTH(`date_n`) = 9 Quote Link to comment https://forums.phpfreaks.com/topic/227023-select-month-from-date/#findComment-1171613 Share on other sites More sharing options...
piheshpi90 Posted February 9, 2011 Author Share Posted February 9, 2011 i thought it will represent the value i insert in the form $colname_Recordset3 = "-1"; if (isset($_POST['s'])) { $colname_Recordset3 = (get_magic_quotes_gpc()) ? $_POST['s'] : addslashes($_POST['s']); } $query_Recordset3 =sprintf( "SELECT * FROM aktiviti WHERE month(`date_n`) = s ", $colname_Recordset3); i try SELECT * FROM aktiviti WHERE MONTH(`date_n`) = 9 and it work.but when i change it to s, it doesnt. error say--> Unknown column 's' in 'where clause'. Quote Link to comment https://forums.phpfreaks.com/topic/227023-select-month-from-date/#findComment-1171625 Share on other sites More sharing options...
Pikachu2000 Posted February 9, 2011 Share Posted February 9, 2011 That whole thing is a bit off. Since $_POST['s'] is being used to retrieve the month, and it's numeric, you should be validating it and casting it as an integer. Then in the sprintf(), you can use %d, since the value is expected to be an intger. if( isset($_POST['s']) && ctype_digit($_POST['s']) ) { $colname_Recordset3 = (int) $_POST['s']; } else { // you can trigger an error, set a default value, etc here if needed. } $query_Recordset3 =sprintf( "SELECT * FROM aktiviti WHERE month(`date_n`) = %d ", $colname_Recordset3); Quote Link to comment https://forums.phpfreaks.com/topic/227023-select-month-from-date/#findComment-1171640 Share on other sites More sharing options...
piheshpi90 Posted February 9, 2011 Author Share Posted February 9, 2011 thank you very much. it works now!! i guess the problem is casting it into integer. i'm realy glad you help me. thank you for your help. :D :D :D :D Quote Link to comment https://forums.phpfreaks.com/topic/227023-select-month-from-date/#findComment-1171696 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.