MySql AES-Decrypted Field empty - mysql

I was trying to en- and decypt data with the corresponding MySQL-functions.
This is what I did:
INSERT INTO dbsec.tbl_credent (U_Password) VALUES (AES_ENCRYPT('secretText', SHA2('pwd123',512)));
the Primary-Key (id) was 6. So I used
SELECT dbsec.tbl_credent.U_Password FROM dbsec.tbl_credent WHERE dbsec.tbl_credent.id = '6';
I got something like this:
Ž4•ý/2Ÿ½üyÙ¤Ý'
So encryption seems to work so far.
When I start the following query:
SELECT AES_DECRYPT(dbsec.tbl_credent.U_Password, 'pwd123') FROM dbsec.tbl_credent WHERE dbsec.tbl_credent.id = '6';
Result is NULL
I used hashing for the password so I tried
SELECT AES_DECRYPT(dbsec.tbl_credent.U_Password, SHA2('pwd123',512)) FROM dbsec.tbl_credent WHERE dbsec.tbl_credent.id = '6';
Result is 73656372657454657874
As all this didn't work I tested this directly:
SELECT AES_DECRYPT(AES_ENCRYPT('secretText', SHA2('pwd123',512)), 'pwd123');
Again the Result was NULL and
SELECT AES_DECRYPT(AES_ENCRYPT('secretText', 'pwd123'), 'pwd123');
returned 73656372657454657874 again.
What do I have to do to get back the 'secretText' I have encrypted?
The Type of U_Password is text (latin1_swedish_ci), btw.

Related

Modifying existing field in json column in Postgres

So I am trying to update some entries in my postgres database. In particular I am trying to modify a field value using the existing value. For example, say I have the following json
{"var1": 10, "var2": 0.003, "var3": null}
and I want to update var2 to var2*100. I have updated values using an update statemnt, e.g.
UPDATE my_table SET json_column = jsonb_set(my_column, '{var2}', '0.003', true) WHERE (my_column->'var2') is null;
so I am trying to use an equivalent statement
UPDATE my_table SET my_column = jsonb_set(json_column, '{var2}', '(json_column->'var2)::double precision*100', true) WHERE id = 12;
however I am facing syntax errors. Has anyone tried something like this?
This can get really tricky but you can get there with some casts. It worked for me like this:
UPDATE my_table
SET my_column = jsonb_set(json_column, '{var2}', to_jsonb((json_column->'var2')::double precision*100), true)
WHERE id = 12;

Issue of trigger for datetime column

I am using an Insert trigger on a table where I copy one record from one table to another.
Everything is ok. It works fine except on datetime columns. When I add a datetime column into the inserted values, then I get an error:
Invalid column name
My code is
ALTER TRIGGER [dbo].[Insert_MoinitoringBasic]
ON [dbo].[M0_BasicInfo]
FOR INSERT
AS
BEGIN
MERGE [MonitoringROSCII].[dbo].[MonitorBasicInfo] AS d
USING (SELECT DistrictID, upazilaID, LC_ID, AcademicYear, Trimester, RepID,
CASE VisitType
WHEN 'Initial validation' THEN 1
WHEN 'Full validation' THEN 2
WHEN 'Compliance monitoring' THEN 3
END AS VisitTp
FROM INSERTED) AS s ON s.DistrictID = d.DistrictID
AND s.upazilaID = d.upazilaID
AND s.LC_ID = d.LCID
AND s.AcademicYear = d.LCVisitYr
AND s.Trimester = d.Trimister
AND s.RepID = d.MOID
AND s.VisitTp = d.VisitType
WHEN MATCHED THEN
UPDATE
SET DistrictID = S.DistrictID
WHEN NOT MATCHED THEN
INSERT (DistrictID, UpazilaID, LCID, VisitType, LCVisitYr, Trimister, MOID,
LCStatus, IfCloseWhy, OthersSpecify,LC1stVstDt)
VALUES (DistrictID, UpazilaID, Lc_ID, VisitTp, AcademicYear, Trimester, RepId,
2, 'No', 'No', FirstVisitDate);
END
Here the last line FirstVisitDate which is a datetime column. Without this column, it worked nice but when I include this column, it shows the error mentioned above.
Can anybody help me with this?
Thanks
As the error text suggests, you are refering to a column that does not exist.
In the SELECT part of your MERGE statement, there is no output column named FirstVisitDate, which is why you're getting the error...

Comparing dates in iif() in SQL Server

I am trying to use the following query in SQL Server
SELECT [AL].[Subscriptions].Id,
[AL].[Subscriptions].name,
[AL].[Subscriptions].description,
[AL].[Subscriptions].price,
[AL].[Subscriptions].iconFileName,
IIf(a.expiryDate > Now(), 'TRUE', 'FALSE') AS isSubsByUser
FROM [AL].[Subscriptions]
LEFT JOIN (SELECT *
FROM [AL].[UserSubscriptions]
WHERE userId = 13259) AS a
ON Subscriptions.Id = a.itemid;
but always get the error
Error in list of function arguments: '>' not recognized.
Unable to parse query text.
How do I resolve it?
Like Martin Smith said you need to use a case statement. Also it looks like you are only using a couple of fields in the derived table therefor I would suggest not using *. I put a example below.
SELECT [AL].[Subscriptions].Id,
[AL].[Subscriptions].name,
[AL].[Subscriptions].description,
[AL].[Subscriptions].price,
[AL].[Subscriptions].iconFileName,
case when a.expiryDate > GetDate() then 'TRUE' else 'FALSE' end AS isSubsByUser
FROM [AL].[Subscriptions]
LEFT JOIN (SELECT expiryDate, itemid
FROM [AL].[UserSubscriptions]
WHERE userId = 13259) AS a
ON Subscriptions.Id = a.itemid;

REPLACE and IF in expression causes concatenation from previous row

The following sql statement:
SELECT
profile_pic
FROM
(`member`)
WHERE
`active` = 1
produces the following result:
profile_pic
1_1345694557.jpg
<blank_value>
<blank_value>
<blank_value>
I want the "blank values" to default to "no_prof_thumb.jpg".
So I created this statement:
SELECT
REPLACE(IF (CHAR_LENGTH(profile_pic) > 0, profile_pic, 'no_prof.jpg' ), '.jpg', '_thumb.jpg') AS profile_pic
FROM
(`member`)
WHERE
`active` = 1
Here is the result:
profile_pic
1_1345694557_thumb.jpg
no_prof_thumb.jpg
no_prof_thumb_thumb.jpg
no_prof_thumb_thumb_thumb.jpg
Why does _thumb keep appending itself to the value in the previous row?
How should I fix my expression?
Update
This issue occurs on a GoDaddy mySQL database. I reproduce when it connecting remotely through SQLyog and when I log into GoDaddy and run it through phpMyAdmin.
However, I cannot reproduce locally nor on sqlFiddle.
So it must be some kind of configuration issue.
You needed to use IF outside the REPLACE statement like this.
SELECT
IF (profile_pic = '', 'no_prof.jpg',
REPLACE(profile_pic, '.jpg', '_thumb.jpg')) AS profile_pic
FROM
(`member`)
WHERE
`active` = 1
You can also use CASE statement instead of IF like this:
SELECT
CASE profile_pic
WHEN '' THEN 'no_prof.jpg'
ELSE REPLACE(profile_pic, '.jpg', '_thumb.jpg')
END AS profile_pic
FROM member
WHERE active = 1;
See this fiddle
The nested logic is a little weird. You can simplify it with a CASE statement. If the value is an empty string, just return no_prof_thumb.jpg. Otherwise, replace .jpg with _thumb.jpg.
SELECT
CASE
WHEN profile_pic = '' THEN 'no_prof_thumb.jpg'
ELSE REPLACE(profile_pic, '.jpg', '_thumb.jpg')
END AS profile_pic
FROM member
WHERE active = 1
I can't, explain why you're getting the weird result you're getting without further testing though.

Null value matching in mySql

I have three tables in a mysql database . Deseasetype(DTID,TypeName) , Symptom(SID, SymptomName, DTID) , Result(RID, SID1, SID2, SID3, result).1st two table, i think is clear enough.
In result table: there will be combination's of symtoms and any values of SymID1/ SymID2/ SymID3 can be null. here i send a picture of the table result.
I want to input some symptom and output will be the result from the 'Result' table.
For that i wrote this query:
$query = "select Result from result where (result .SID1= '$symptom1') AND (result.SID2= '$symptom2' ) AND (result.SID3 = '$symptom3')";
This work only when three symptom's have value. but if any of the symptom's are null, then no result found. May be the query should be more perfect.
**please avoid any syntax error in my writing.
That's because you are comparing NULL to an empty string, and they aren't equal. You could try this instead:
SELECT Result
FROM symptom
WHERE IFNULL(symptom.SID1, '') = '$symptom1'
AND IFNULL(symptom.SID2, '') = '$symptom2'
AND IFNULL(symptom.SID3, '') = '$symptom3'
Notes:
You need to correctly escape the values of $symptom1, $symptom2 and $symptom3.
This won't efficiently use indexes.
As mark pointed out, the query is eventually falling down to compare with null if you are not escaping the null.
Or you can slightly change your logic to show a empty symptom with value '0' and then using the coalesce function you can easily build your query.
Does this work?
$query = "select Result from result
where (result.SID1 = '$symptom1' OR result.SID1 IS NULL) AND
(result.SID2 = '$symptom2' OR result.SID2 IS NULL) AND
(result.SID3 = '$symptom3' OR result.SID3 IS NULL)";