MySQL to PostgreSQL query rewrite using "IN"? - mysql

I'm transferring over some queries from MySQL to PostgreSQL and I'm stumped on how to rewrite the following query to work in PostgreSQL:
SUM(phoneid IN (1, 2, 6, 8)) AS completedcalls
I originally thought I could just do SUM(SELECT phoneid FROM myTable WHERE phoneid = 1 OR phoneid = 2 etc etc, but I do not believe you can have a SELECT within a sum.
I also tried using a WITH query but had no luck getting that to work.

how about using CASE
SUM(CASE WHEN phoneid IN (1, 2, 6, 8) THEN 1 ELSE 0 END)

count(phoneid in (1,2,6,8) or null)

bool may be cast to integer:
SUM(CAST(phoneid IN (1, 2, 6, 8) AS INTEGER)) AS completedcalls

Related

Add hyphen in a string after every 2 characters using in mysql select statement

I have a mysql table with some data. In that i have a column(mobile_number) and that column value is like mobile number (ex:1234587920).
my expected out put is : 12-34-58-79-20
I want to add hyphen after every two numbers.
use concat() for joining strings and use substr() for split a string in mysql :
split (column,start,count) select column from start position for count charachter
concat(column1,"-",column2) will be column1-column2
SELECT CONCAT(SUBSTR(phone,1,2),'-',SUBSTR(phone,3,2),'-',SUBSTR(phone,5,2),'-
',SUBSTR(phone,7,2),'-',SUBSTR(phone,9,2)) FROM `table`
I would definitely use concat_ws() for this:
select concat_ws('-',
substr(mobile_number, 1, 2),
substr(mobile_number, 3, 2),
substr(mobile_number, 5, 2),
substr(mobile_number, 7, 2)
)
Here is a db<>fiddle.
this will work:
SELECT concat(SUBSTRING("1234587920", 1, 2),"-",SUBSTRING("1234587920",3,2),"-
",SUBSTRING("1234587920", 5, 2),"-",
SUBSTRING("1234587920", 7, 2),"-",
SUBSTRING("1234587920", 9, 2)) AS ExtractString;
for your table query is like :
SELECT concat(SUBSTRING(mobile_number, 1, 2),"-
",SUBSTRING(mobile_number,3,2),"-",SUBSTRING(mobile_number, 5, 2),"-",
SUBSTRING(mobile_number, 7, 2),"-",
SUBSTRING(mobile_number, 9, 2)) ExtractString from tablename;
you can use substr and contact_ws command in mysql
update tmp_pay SET date_fix = concat_ws('-',substr(date_at,1,4),substr(date_at,5,6),'01') ;
you can try this
select concat( right(phone_number,3) , "-" , mid(phone_number,4,3) , "-", right(phone_number,4)) from table

SQL how to add a leading 0

Any help would be greatly appreciated.. Im quite new to mySQL and figured there has to be an easier way to update my database.
So my issue is that I have a number of records that don't have a leading zero but I need the total number of characters in any record to be 5.
e.g. 6930 should in fact be 06930
I came across this code but I am unable to get it to work correctly, could anyone point me in the right direction.
UPDATE `it_asset_register`.`tab_id_master`
SET tab_id_master.ID = LEFT('00000', 5-LEN(tab_id_master.ID)) + tab_id_master.ID
WHERE ID = '8407' AND LEN(tab_id_master.ID)<5 AND Len(tab_id_master.ID)>0;
Thanks
LPAD should do the job here:
SELECT LPAD ('123', 5, '0');
00123
SELECT LPAD ('12345', 5, '0');
12345
Try:
UPDATE `it_asset_register`.`tab_id_master`
SET tab_id_master.ID = LPAD (tab_id_master.ID, 5, '0')
WHERE ID = '8407' AND LENGTH(tab_id_master.ID)<5 AND LENGTH(tab_id_master.ID)>0;
To concatenate two or more strings in MySQL, you must use the CONCAT() function.
A possible solution can be:
UPDATE tab_id_master
SET id = SUBSTRING(CONCAT('00000', id), -5)
WHERE LENGTH(id) > 0 AND LENGTH(id) < 5;
Alternatively you can use the LPAD() function:
UPDATE tab_id_master
SET id = LPAD(id, 5, '0')
WHERE LENGTH(id) > 0 AND LENGTH(id) < 5;
Regards
UPDATE
table_name
SET
column_name = RIGHT('00000' + CAST(column_name AS CHAR), 5)
This concatenates the 5 zeros and your ID to something like '000001234'. Then it selects the 5 rightmost characters.
You didn't specify tab_id_master.ID type. If it is of a numeric type you wouldn't be able to store a number with leading zeros but otherwise you could use LPAD():
SET tab_id_master.ID = LPAD(tab_id_master.ID, 5, 0)

Special sorting for MySQL result

Is it possible to get MySQL to put a row which matches some condition on the first position?
For instance, say that my result is [1, 3, 5, 6, 7, 1987] and I want it to become [6, 1, 3, 5, 7, 1987] (I want the number 6 to be on the first position)
I feel you are searching for this
SELECT * FROM `pctable`order by colName=6 desc, colName
so colName = 6 will be in top and then for others it will be id wise sort.
Try to use CASE expression in your Order by clause. Such as
ORDER BY CASE WHEN COL_VALUE = 'xxx' THEN '1'
WHEN COL_VALUE = 'xxx' THEN '2'
ELSE COL_VALUE END ASC;
Or with IF in the SELECT somehow:
SELECT IF(number = 6, number, ''), number FROM colname

mysql: write query with if in where part

i have this table:
(`id`, `name`, `type`, `price`)
(1, 'cats', 1, 12.25),
(2, 'dogs', 0, 11.35),
(3, 'house', 1, 7.25),
(4, 'cats2', 2, 5.26);
I need select all data, but if type is 1, i need get items witch price is more than 10.
I create this query:
SELECT *
FROM `items`
WHERE IF(TYPE = "1", price > 10, 1 = 1)
Works well, but maybe possible write much smarter or in other way?
Maybe don't need "1 = 1"?.
I would like to know your advice, thanks
Your 1=1 is senseless, but your IF is not. You can use just 1:
SELECT *
FROM `items`
WHERE IF(TYPE = "1", price > 10, 1)
-since MySQL evaluates expression as bool (actually, int) - and so 1 means 'true'.
But on the other hand, there's logic equivalent for your condition:
SELECT *
FROM `items`
WHERE `price`>10 OR `type`!="1"
However, I've faced such case in another question and, after some researching, I've discovered that IF is faster, even if it looks more complicated.

How to make string longer then 15 chars appeared with ".." in MySQL?

I'm seeking the most efficient way to make strings like that "abcdefghijklmnop"(longer then 15) to appear like that "abcdefghijklm.." - all of that inside MySQL query. I dont want to deal with it inside my app code.
select if(char_length(thefield) > 15, concat(substr(thefield, 1, 13), '..'), thefield)
SELECT
CASE WHEN LENGTH(str) > 15
THEN CONCAT(SUBSTRING(str, 1, 13), "..")
ELSE str
END
Try:
select if(length(string)> 15,concat(left(string, 15), '..'),string) from your_table