I'm having a problem with a SQL query that must match the username of a user out of a column that contains all the users usernames.
So the column will contain something like:
|USER1|USER2|USER3|USER11|USER22|
The user have pipes on the left and right to prevent "USER1" be matched even in "USER11".
My query is
SELECT * FROM myTable WHERE CONCATUSERS LIKE ('%|' || 'USER1' || '|%')
Note that the USER1 in the query is a variable generated from our code so I must keep the concatenation syntax and I must use a standard syntax too (the code will run in mySQL, SQLServer etc..
So what is the correct way of concatenating strings in a LIKE clause?
MySQL uses the double pipes for concat. SQL Server you can use +.
SELECT *
FROM myTable
WHERE CONCATUSERS LIKE ('%' + '|user1|' + '%')
Use CONCAT.
(available on SQL server 2012 and beyond)
It has the benefit that it implicitly converts types to add the value to the string. And it's not just available on Sql Server and MySql.
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT('%|','USER1','|%');
Do note that in MySQL the result will be NULL if one of the concatenated values is NULL. But not on Sql Server.
It's just Oracle that's being stubborn by only allowing 2 values to that function.
So if the SQL needs to run unchanged on MySQL, a recent Sql Server AND Oracle then this should work:
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT(CONCAT('%|','USER1'),'|%');
You can go with CONCAT function.
As it is supported in both SQL and MySQL
SELECT *
FROM xpat
WHERE plname LIKE concat('%' ,'|user1|' ,'%');
Related
I've been using the following format to insert into my mysql database previously, and would like to keep it uniform.
INSERT INTO OPENQUERY (ENET, 'SELECT * FROM ActiveDirectory.Computers') -- MYSQL database
SELECT c.[CommonName],
c.[DistinguishedName],
c.[SAMAccountName],
c.[DNSHostName],
c.[Location],
c.[Division],
c.[Department],
c.[ManagedBy],
c.[MachineRole],
CAST(CAST(c.[LastLogon] as timestamp) as datetime) AS LastLogon,
c.[OperatingSystem],
c.[OperatingSystemVersion],
c.[ServicePack],
c.[OU],
c.[CreatedOn],
c.[ChangedOn],
CASE WHEN c.[UserAccountControl] & 2 = 0 THEN 1 ELSE 0 END AS [Enabled] -- Check to see if disabled. Disabled = bitwise of 2; 4098 = 4096 + 2 = Trust Account + Disabled
FROM [IT_ActiveDirectory].[dbo].[ADComputerTable] c -- MSSQL database
My problem comes when inserting the fields that are datetimes in MSSQL (c.LastLogon, c.CreatedOn, c.ChangedOn) into mysql fields that are also datetimes. I have tried almost every combination of CAST() and CONVERT() I can think of, but I may have missed something. I have also tried changing mysql's field type to timestamp.
It returns: Conversion failed when converting date and/or time from character string.
It seems strange to me that MSSQL won't just send the data to MYSQL and let it do the conversion. Instead it looks like it is trying to convert and match the datatypes and data before it sends it to MYSQL.
If I can't insert it this way, I am open to another format, like if its possible to do the insert inside of the OPENQUERY() SELECT. Any suggestions? I'm dead in the water at the moment.
I would try to convert to ISO-8601 format string:
INSERT INTO OPENQUERY (ENET, 'SELECT * FROM ActiveDirectory.Computers')
SELECT
...,
CONVERT(VARCHAR, LastLogon, 126) AS LastLogon
FROM [IT_ActiveDirectory].[dbo].[ADComputerTable];
Also I don't like SELECT * in OPENQUERY. If columns are matched by position it is error-prone. Consider expanding start to columns.
I am trying to use the date part of a time-stamp in my where query in a db2 pass-through proc SQL code below. I tried using date and datepart functions but it wont work with this format. Does anyone know the name of the function to use in the same code below?
PROC SQL;
connect to db2(ssid=smtng);
select * from connection to db2
(select *
from ATable
where DATEPART(timestamp) > '12/01/2013'
FOR READ ONLY WITH UR
);
DISCONNECT FROM DB2;
QUIT;
If you use a function on the datetime field in DB2 then the database won't be able to use it's indexes (if that field is indexed). This is because indexes are (almost always) created on the field itself, not the result of the field after it has been processed by a function. This holds true for the majority of databases not just DB2.
Instead, what you want to do is supply datetime values for the beginning of the day and for the end of the day and get everything inbetween them. To simplify this process I created a format called mysqldt.. Originally this format was for a mySQL database, but SQL server and DB2 both use the same formats so it can be used on those as well:
proc format;
picture mysqldt low-high = '''%Y-%0m-%0d %0H:%0M:%0S''' (datatype = datetime) ;
run ;
Once this format is available I tend to use macro variables. At the top of my program I would create a macro variable where I specify the date to use throughout the report:
%let rpt_date = %sysfunc(mdy(1,12,2013));
I would then create two datetime fields representing the start of the day and the end of the day, and I would save them in the format that is needed for the SQL statement:
%let sql_start = %sysfunc(dhms(&rpt_date, 0, 0, 0), mysqldt.);
%let sql_end = %sysfunc(dhms(&rpt_date,23,59,59), mysqldt.);
%put &rpt_date &sql_start &sql_end;
You would then change your query to look like this:
proc sql;
connect to db2(ssid=smtng);
select * from connection to db2
(select *
from atable
where timestamp between &sql_start and &sql_end
for read only with ur
);
quit;
This way, not only are your indexes now used in your query, but the SQL looks cleaner and reads easier, and you only need to change the report date in a single place (at the top of your program) if you need to rerun your report.
In general, you need to use the correct DB2 syntax. I don't know DB2, but this paper covers this fairly well. Specifically:
PROC SQL;
CREATE TABLE ONE AS SELECT * FROM CONNECTION TO DB2
(SELECT A.ID, A.NAME, B.AMOUNT, B.POSTDATE
FROM IDS A
INNER JOIN BANK B
ON A.ID = B.ID
WHERE POSTDATE BETWEEN '2007-01-01-00.00.00.000000'
AND '2007-09-30-23.59.59.999999')
So it looks like your query would be
PROC SQL;
connect to db2(ssid=smtng);
select * from connection to db2
(select *
from ATable
where DATEPART(timestamp) > '2013-12-01-00.00.00.00000'
FOR READ ONLY WITH UR
);
DISCONNECT FROM DB2;
QUIT;
This article from IBM seems to suggest that there are other formats other than timestamp (which is what above is). So you may need to use a different one depending on the exact format.
I believe, you should use SAS-date literal. So:
where DATEPART(timestamp) > '12Jan2013'd
I have a problem with Aliased Columns in MySQL!
My Query:
SELECT Price AS Pr, (Pr*10/100) FROM MyTable;
MySQL WorkBench Error: UnKnown Column 'Pr' in Field List !!!
I tested my query in W3Schools with no error !
I tested my query in W3Schools with no error!
This doesn't prove that your query is valid.
You can only use aliases in GROUP BY, ORDER BY or HAVING clauses. Your usage variant is not allowed, because the value of alias is not known when MySQL is selecting the 2-nd column.
I've got a suspicion that W3Schools uses MS Access to run user queries, and MS Access does allow such atrocity as referencing column aliases in a SELECT clause that are defined in the same SELECT clause.
The standard doesn't allow this and MySQL does follow standard in this particular case.
As for solution to your problem, I can see two options.
The more generic solution, which would run in probably any SQL product, would be to use a derived table:
SELECT
Pr,
(Pr * 10 / 100) AS SomethingElse
FROM
(
SELECT
SomeComplexExpression AS Pr
FROM MyTable
) AS sub
;
The other option would be to use a variable, which is MySQL-specific:
SELECT
#Pr := SomeComplexExpression AS Pr,
(#Pr * 10 / 100) AS SomethingElse
FROM MyTable
;
Finally, if you need to test/demonstrate if something can/cannot work in MySQL, I'd recommend using SQL Fiddle.
The following query works fine with MySQL:
SELECT concat(title,'/') FROM `socials` WHERE 1
It Concat / to the selected title field.
However, when I try to do the following:
SELECT concat(*,'/') FROM `socials` WHERE 1
It returns the follwoing Error:
#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near '*,'/') FROM `socials` WHERE 1 LIMIT 0, 30' at line 1
So is there any way to make such sql query to work with MySql
You simply can't do that in SQL. You have to explicitly list the fields and concat each one:
SELECT CONCAT(field1, '/'), CONCAT(field2, '/'), ... FROM `socials` WHERE 1
If you are using an app, you can use SQL to read the column names, and then use your app to construct a query like above. See this stackoverflow question to find the column names: Get table column names in mysql?
If you want to concatenate the fields using / as a separator, you can use concat_ws:
select concat_ws('/', col1, col2, col3) from mytable
You cannot escape listing the columns in the query though. The *-syntax works only in "select * from". You can list the columns and construct the query dynamically though.
You cannot concatenate multiple fields with a string. You need to select a field instand of all (*).
You cannot do this on multiple fields. You can also look for this.
I am getting this error while I am trying to execute a simple SELECT statement in Toad
MySql.Data.Types.MySqlConversionException
Unable to convert MySQL date/time value to System.DateTime
What could be wrong?
That could mean one of these two common issues:
1) Zero dates, which are 0000-00-00 in MySQL. MySQL allows you to store them to mark 0 dates, you can even use 0001-01-01, but not all drivers or downstream programs can handle them. Add to the connection string
Allow Zero Datetime=true;
The other choice is explicitly removing them, something like
SELECT IF(DateCol='0000-00-00' OR DateCol<'1970-01-01', NULL, DateCol) as DateCol,
Othercol1, ID ....
FROM TBL
2) Date formatting. For some driver/program combination, the dates are handled as strings. Explicit conversion is necessary:
SELECT DATE_FORMAT(DateCol, '%m/%d/%Y') as DateCol,
Othercol1, ID ....
FROM TBL