Is there a way to just cut the last three digits off? Or to index a substring that ends at the colon rather than being a fixed number of digits?įor context, I'm trying to write a query that sorts the occurrences of events by the time that they happened, but grouping if they happened before 9am, between 9am and 5pm, and after 5pm, if that makes it clearer. The only thing that is consistent about the formatting is that there is always a colon followed by two digits to indicate the number of minutes, and I'm thinking that if I can cut those out, I'll be left with the hour. The time stamps are (annoyingly) not formatted consistently either (so instead of 09:00 it's 9:00), which means that I can't just index the first digit or first two digits and get the hour to cast as an int. What I really want however is access the hour from the time stamp. They're not consistent in length, and I've used SELECT SUBSTR(start_date, INSTR(start_date, ' ') + 1) AS newcol to isolate out the times and get rid of the dates, as the dates are not a consistent length (printed as or, for example, rather than ), but they are always followed by a space, so I can index from there. New to SQL, and I'm trying to get the hour out of some badly-formatted datetime strings. Thanks to -sniff- for the awesome header image! However, in the function SUBSTR(StudentName, 2) we didn’t specify a number for the characters to be returned, that’s why SQLite returned all the remaining characters in the StudentName starting from the second position. You should post these to /r/learnsql instead. SUBSTR(StudentName, 2, 4) substring the string StudentName starting from the second character, and return the next 4 characters. Note /r/SQL does not allow links to basic tutorials to be posted here. Please view the Wiki for online resources. Learning SQLĪ common question is how to learn SQL. SELECT count(a.field1), a.field2, SUM(b.field4) FROM a INNER JOIN b ON a.key1 = b.key1 WHERE a.field8 = 'test' GROUP by a.field1, a.field2 HAVING SUM(b.field4) > 5 ORDER by a.field.3įor those with SQL questions we recommend using SQLFiddle to provide a useful development and testing environment for those who wish to fully understand your problem and help devise a solution. Something as simple as line breaks and using reddit's built in code formatting (4 spaces at the start of each line) can turn this: This will greatly increase your chances of receiving the help you desire. If you are including actual code in a post or comment, please attempt to format it in a way that is readable for other users. ![]() We will gladly help where we can as long as you post the work you have already done or show that you have attempted to figure it out on your own. ![]() If you are a student or just looking for help on your code please do not just post your questions and expect the community to do all the work for you. While naturally we should endeavor to work as platform neutrally as possible many questions and answers require tailoring to the feature set of a specific platform. When requesting help or asking questions please prefix your title with the SQL variant/platform you are using within square brackets like so: The goal of /r/SQL is to provide a place for interesting and informative SQL content and discussions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |