I'm trying to compare the results of two queries, one acquiring call IDs for calls made to my Asterisk server externally (10 digits) and the other acquiring call IDs connected to FROM the server (11 digits). The outbound calls are prepended a '1' before their number. Currently I'm using a statement like the following:
select data2, from_unixtime(time_id) day from queuemetrics.queue_log
where time_id > '1346475600' and (data2, time_id) in
(select dst, unix_timestamp(calldate) from asteriskcdrdb.cdr
where calldate > '2012-09-01' and lastdata like <blocked for privacy>)
order by day;
data2 is the column holding the 10 digit numbers, dst holds the 11 digit numbers. Is there a way I can pattern match the 2-11th characters of a column ONLY? To just skip over the first one? Obviously a LIKE or RLIKE would be useful, but I really need to maintain the nested query for this to work. Any help would be great. Also, pay no attention to my weird use of from_unixtime and unix_timestamp. I was experimenting with figuring if I needed my times in the same format for the search to work. Not important.
You may use RIGHT to extract the rightmost characters of a string:
RIGHT(your_field_here, 10);
If there are some characters you want to ignore at the beginning AND at the end of the string, then you may use SUBSTR:
SUBSTR(your_field_here, 2, 10);
Your query would then be:
SELECT data2, FROM_UNIXTIME(time_id) day FROM queuemetrics.queue_log
WHERE time_id > '1346475600' AND (data2, time_id) IN
(SELECT SUBSTR(dst, 2, 10), UNIX_TIMESTAMP(calldate) FROM asteriskcdrdb.cdr
WHERE calldate > '2012-09-01' AND lastdata LIKE <blocked for privacy>)
ORDER BY day;
Why not trim the leading digit from your dst field?
Related
As title, I'm trying to convert a VARCHAR column in a DATE column, and data is populated in that format "DDMMYYYY" ex. XMAS is "25122022" and in this case the correct formula should be STR_TO_DATE(column, '%d%m%Y')Well, when I execute this query I get an error since in some cases I have values with a "missing" char, I mean, for example, "1012023" when the day is <10 the query fails, cause it checks for "01122023" instead.I could solve this easily by adding a 0 to all fields having length 7, but I'd like to make it more clean.Reading better the usage of STR_TO_DATE I noticed that I could replace %d with %e since the second choice should theorically consider days from 0 to 31 instead of 01 to 31.Unexpectedly the query didn't work and gave me the same erorr at the first instance of a length 7 string.Am I doing something wrong?Thanks in advance.
We can try left padding your date string with zero to a length of 8:
WITH yourTable AS (
SELECT '1012023' AS dt
)
SELECT STR_TO_DATE(LPAD(dt, 8, '0'), '%d%m%Y') AS dt_out -- 2023-01-01
FROM yourTable;
Demo
i have a table name expected expense in which i have 4 columns name Expense_title, Amount, expense_category, date and all the 4 columns have var char type. When I try to find expense between two dates it work fine for same year, e.g. 11/27/2018 and 12/27/2018, but it doesn't get any result when I try to find expense between two years, e.g. 12/27/2018 And 01/27/2019. please help
I am trying this query
SELECT *
from expected_expense
WHERE Date BETWEEN '$start_date' AND '$end_date'
As per the comments, this is because of the varchar type.
The between operator is nothing different than doing two closed inequalities for its range limits. In your example,
between 12/27/2018 And 01/27/2019
will be changed internally to
>= 12/27/2018 and <= 01/27/2019
but these are not dates, they are text. And the second one is less than the first, so nothing will be returned. It's like asking the question: which letter comes after q but before b? None.
Either change the fields to datetime, or use conversion functions in your query.
The table contains a column that contain strings. I would like to extract numbers from the string.
The code: http://sqlfiddle.com/#!9/2af45
Eg:
1 Day = 1
2 Day anytime = 2
7 Days = 7
SameDay = 1
As for string that does not contain any number, I would like to assign a number to it.
What I have in mind:
What will be the best solution such that the query will not affect the loading speed greatly on MySQL?
Additional challenge: If the number appears randomly in the string, is there any method/algorithm that can potentially pull the number?
E.g. every 2 day, promotion in 7 days
If the string starts with a number, you can simply CAST it to an UNSIGNED. Any text after the number is ignored
CAST("2 Day anytime" AS UNSIGNED);
CAST("20 fishes" AS UNSIGNED);
Any string that doesn't start will a number, will become 0. You can turn that into some other value with the combination of NULLIF and IFNULL.
IFNULL(NULLIF(CAST("SameDay" AS UNSIGNED), 0), 1);
You can create a new column with ALTER TABLE and than do an UPDATE query.
UPDATE foo (days_int)
SELECT IFNULL(NULLIF(CAST(days_string AS UNSIGNED), 0), 1)
FROM foo;
Note that the update and select is from the same table.
I have a table.
And it has two fields id and datetime.
What I need to do is, for any two given datetimes, I need to divide the time range into 10 equal intervals and give row count of each interval.
Please let me know whether this is possible without using any external support from languages like java or php.
select ((UNIX_TIMESTAMP(date_col) / CAST((time2 - time1)/10) AS INT) + time1), count(id) from my_table where date_col >= time1 AND date_col <= time2 GROUP BY ((UNIX_TIMESTAMP(date_col) / CAST((time2 - time1)/10) AS INT) + time1)
I haven't tested it. But something like this should work.
The easiest way to divide date intervals is if you store them as longs (ie #of ticks from the "beginning of time"). As far as I know, there is no way to do it using MySQL's datetime.
How you decide to do it ultimately depends on your application. I would store them as longs and have whatever front end you are using handle to conversion to a more readable format.
What exactly do you mean by giving the row count of each interval? That part doesn't make sense to me.
I need to update the values from a VARCHAR column in a MySQL database from YYMMDDSXXXXX to YYMMDDSXXXX, where YY is year (i.e. 11 for 2011), MM is month (i.e. 09 for September), DD is day (i.e. 15), S is an one-digit order number (1 to 0) and XXXXX is a sequential number from 00001 to 99999.
I need to reduce ten-fold the sequential number, which should go from 0001 to 9999.
I thought about something like:
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);
But I'm not very good with MySQL, so I'm not sure how to do it. Can someone help?
Thanks in advance!
Regex replace can be implemented easily using this:
https://launchpad.net/mysql-udf-regexp
The regex you will need (in perl/posix std notation e.g. with sed)
s/([0-9]{7})0-9/$1$2/g
That is assuming you want to shorten the counter from the left (most significant) to keep the counts you have already unique (and obviously if you don't need that many places you want to remove the useless - most significant - bit).
That should get you well on your way, have fun :)