How should I add text to my column's select statement - sql

This is my sql code
SELECT sessionname, left(comment,4))
FROM moma_reporting.comments where name like '%_2016_02_%'
and comment = '1200'
My output will be :
"WE247JP_2016_02_07__14_48_18";"1200"
"FORD49_2016_02_03__12_42_24";"1200"
"1-GRB-804_2016_02_06__08_20_15";"1200"
What i want to do is to add text to column -comment so it will look like this:
"WE247JP_2016_02_07__14_48_18";"1200-QC"
"FORD49_2016_02_03__12_42_24";"1200-QC"
"1-GRB-804_2016_02_06__08_20_15";"1200-QC"
How i can do this ?

Just concat it:
SELECT sessionname, left(comment,4))||'-QC'
FROM moma_reporting.comments
where name like '%_2016_02_%'
and comment = '1200'
Unrelated, but: left(comment,4)) is useless. The condition and comment = '1200' will never return comments that are longer then 4 characters.

Related

Postgresql , updating existing table row with another tables data

I am trying to update a null column using another tables value but it doesn't seems to work right. below codes were tried
SET
"Test name "= "Test"(
SELECT Transformertest.Test,Transformertest.TestID
FROM public.Transformertest WHERE TestID='Tes3')
WHERE test2table.Type='Oil Immersed Transformers'
UPDATE
public.test2table
SET
"Test name" = subquery."Test"
FROM
(
SELECT
"Test"
FROM Transformertest WHERE "TestID"='Tes2'
) AS subquery
WHERE
"Type"='Auto Transformer' AND "Phase"='3' AND "Rated Frequency"='60';
enter image description here
don't use space in column name.
Integers don't need to be quoted
See the result here (enter link description here)
what you need to do here (assuming Phase and Rated Frequency are integers)
remove unnecessary "" and spaces on column names
UPDATE
public.test2table
SET
test_name = subquery.Test
FROM
(
SELECT
test
FROM Transformertest WHERE Test_ID='Tes2'
) AS subquery
WHERE
Type='Auto Transformer' AND Phase=3 AND Rated_Frequency=60;
this should be working now

How to make LIKE behave case-insensitive?

I have an importedParameter which I want to search inside of a column in a SELECT.
But for now it is case-sensitive, how can I make it case-insensitive ?
I've tried multiple things: AND LOWER(columnName) LIKE LOWER(#lv_string) or AND columnName LIKE #tst_string COLLATE utf8_general_ci and some other stuff but got this error:
A Boolean expression is required in positions starting with LOWER(Q.
Sample code:
DATA(tst_string) = '%' && importedParamter && '%'.
IF anotherParameter IS NOT INITIAL.
IF importedParamter IS NOT INITIAL.
SELECT * FROM <table1> as p
INNER JOIN <table2> as q on q~column1 = p~column1
WHERE p~column2 = #anotherParameter
AND q~column2 LIKE #tst_string
INTO CORRESPONDING FIELDS OF TABLE #anotherName
ENDIF.
ENDIF.
I believe Regex is your preferred choice: LIKE_REGEXPR:
SELECT *
FROM <table1> as p
INNER JOIN <table2> as q on q~column1 = p~column1
WHERE p~column2 = #anotherParameter
AND like_regexpr( pcre = '\bparam\b', value = q~column2, CASE_SENSITIVE = 'X' ) = '1'
INTO TABLE DATA(#anotherName).
It has CASE_SENSITIVE predicate which respects (or not) the case.
Though this is available only since ABAP 7.55, so on lower releases you are out of the luck.
This code works fine for me:
SELECT *
FROM adrp
WHERE LOWER( name_first ) LIKE 'phi%'
INTO TABLE #DATA(results).
It finds my personal data entry (as well as those of another "Philipp" and of a "Philip"), even though we are all spelled with a capital P.
LIKE LOWER( 'Phi%' ) does not work, but when you can't control the input, then you can convert it to lower case before the select:
DATA(tst_string) = 'Phi%'.
TRANSLATE tst_string TO LOWER CASE.
SELECT *
FROM adrp
WHERE LOWER( name_first ) LIKE #tst_string
INTO TABLE #DATA(results).
Release: 7.54
I am not sure which release specifically allowed functions like LOWER within the WHERE clause. According to the comments, it should work since 7.51.

use a regex capture in a pgSQL select

I've never used a regex in SQL. If I capture something like this in javascript, how can I refer back to the capture in pgSQL like I can with the 2nd element in javascript match() method?
var str = 'thomas-var1="SOME VAL1" thomas=var2="SOME VAL2" thomas-var-3="the value i want" thomas-var-4="SOME_VAL4"';
var re = /thomas-var-3="(.+?)"/i;
var new_str = str.match(re);
console.log(new_str[1]);
How can I put that into a SELECT statement so that I can say something like, retrieving the value "the value i want" from thomas-var-3?
SELECT * FROM forms WHERE name LIKE '%bill%' AND category = MY REGEX CAPTURE
rendering so something like"
SELECT * FROM forms WHERE name LIKE '%bill%' AND category ='the value i want'
you can use not regex, but hstore extension for this, like:
str := 'thomas-var1="SOME VAL1" thomas-var2="SOME VAL2" thomas-var-3="the value i want"'
str := replace(replace(str, '=', '=>'), '" ', '", ')
select *
from forms
where name like '%bill%' and category = hstore(str)->'thomas-var-3'
Using a subselect and the substring method you should be able to achieve what you want like so:
SELECT *
FROM firstTable
WHERE parentCat = (SELECT cat
FROM secondTable
WHERE cat in substring(column_to_capture_from from 'thomas-var-3="(.+?)' ));
from http://www.regular-expressions.info/postgresql.html
If there is a match, and the regex has one or more capturing groups, the text matched by the first capturing group is returned.

Dynamic appending constraints in SQL query

I have this SQL
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
and <<either bdvalue or string value based on user selected value datatype>>
Here in the Query based on the devudp1.valueType I want to append below attribute
If the valueType is 3 then I want to append my above select clause with devudp1.bdvalue ='10', else it should be appended by devudp1.bdvalue = 'Hello'
So the above query when valueType is 3 will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.bdvalue = '10'
else it will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.stringValue = 'Hello'
Can anyone suggest me how to put this logic in place
Try this:
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND (
(<USER-SELECTED-VALUE> = 3 AND devudp1.bdvalue ='10') OR
(<USER-SELECTED-VALUE> <> 3 AND devudp1.stringvalue ='Hello')
)

No Result Returned From SQL Query

I am running the following query, but no rows are returned even though a record exists that should match the query.
SELECT
*
FROM
tblsignup
WHERE
usr_email='amir#gmail.com'
AND
(status=1 or status=2)
You should try by simplifying the query (yeah...even if it's so simple)
try this
Select * from tblsignup
then
Select * from tblsignup where
usr_email = 'amir#gmail.com'
then
Select * from tblsignup where
usr_email='amir#gmail.com' and
status > 0
//I know you won't use > 0 at the end, but we want to eliminate the most cause of error we simplify by > 0 only to be easier to read
Tell us from where you start getting 0 line, this could lead us to the problem, I know I already had a problem like that with a field named "date", because date is already used by MySQL, funny MySQL still let me use that fieldname tho.
Try this:
select * from `tblsignup` where `usr_email`='amir#gmail.com' and (`status`=1 or `status`=2)
I have a feeling "status" might be reserved for something special. It might be worth a shot changing it to `status`.
Try wrapping brackets around the status column name:
SELECT *
FROM tblsignup
WHERE usr_email = 'amir#gmail.com'
AND ([status] = 1
OR [status] = 2);
EDIT
After reading your comment, why not use:
SELECT *
FROM tblsignup
WHERE usr_email = 'amir#gmail.com'
AND [status] > 0;
May it be that your column or table has case sensitive collation and the address is typed different ('Amir...')? As your query is correct SQL. You can find that with:
EXEC sp_help DatabaseName