简体   繁体   中英

How to query an sql statement based on picker date

I am using picker view and customized it to display month and a year. I have a table in my database which stores the dates in the format "ddMMyyyy" Ex:12022013 . There will be no spaces or colons or any other characters in the date stored in the database.

The output of my picker view is in the format "MMyyyy" .

What I need to know is ,as my table contains the date stored in "ddMMyear" format, How can I query to select the data based on "MMyyyy" ?

the dates are stored in the field called 'stdate'

 NSDate *date =  self.datePicker.date;

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMyyyy"];
NSString * dateString = [dateFormat stringFromDate:date];

    NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM MYTABLE WHERE STDATE='%@'",dateString];

This query doesn't work because the "mmyyyy"format doesn't exists in the table. Could you please tell me how to get the data based on "mmyyyy" format where the table contains "ddmmyyyy" .

Since you want to ignore the leading dd in the stored value, try using like .

@"SELECT * FROM MYTABLE WHERE STDATE LIKE '%%%@'"

Note there are now three % signs. This will match the date if it ends with your MMyyyy value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM