PostgreSQL "&" symbol and number after column name - database

I have psql version 9.2.4.
I am vivewing log file of our database and i found something weird for me.
There are WHERE statements where:
te_flag&1024 = 0
te_flag&5120 <> 1024
I don't know what part "&1024" after column name does.
Can someone explain it?
Thanks!

A single & is the bitwise and operator. E.g.:
db=> SELECT 4 & 2 AS bitwise_4_and_2;
bitwise_4_and_2
-----------------
0
(1 row)

Related

How to load a csv into a table in Q?

Very new to Q and I am having some issues loading my data into a table following the examples on the documentation.
I am running the following code:
table1: get `:pathname.csv
While it doesn't throw an error, when I run the following command nothing comes up:
select * from table1
Or when selecting a specific column:
select col1 from table1
If anyone could guide me in the right direction, that would be great!
Edit: This seems to work and retain all my columns:
table1: (9#"S";enlist csv) 0: `:data.CSV
You're going to need to use 0: https://code.kx.com/q/ref/filenumbers/#load-csv
The exact usage will depend on your csv, as you need to define the datatypes to load each column as.
As an example, here I have a CSV with a long, char & float column:
(kdb) chronos#localhost ~/Downloads $ more example.csv
abc,def,ghi
1,a,3.4
2,b,7.5
3,c,88
(kdb) chronos#localhost ~/Downloads $ q
KDB+ 3.6 2018.10.23 Copyright (C) 1993-2018 Kx Systems
l64/ 4()core 3894MB chronos localhost 127.0.0.1 EXPIRE 2019.06.15 jonathon.mcmurray#aquaq.co.uk KOD #5000078
q)("JCF";enlist",")0:`:example.csv
abc def ghi
-----------
1 a 3.4
2 b 7.5
3 c 88
q)meta ("JCF";enlist",")0:`:example.csv
c | t f a
---| -----
abc| j
def| c
ghi| f
q)
I use the chars "JCF" to define the datatypes long, character & float respectively.
I enlist the delimiter (",") to indicate that the first row of the CSV contains the headers for the columns. (Otherwise, these can be supplied in your code & the table constructed)
On a side note, note that in q-sql, the * is not necessary as in standard SQL; you can simply do select from table1 to query all columns

Derived column expression only capture 10 characters

I am new to SSIS and I have searched to find the solution to this question. Any help is most appreciated!
I have a flat file with data defined as dt_wstr, to change the datatype I am using a data conversion to set the [column] to dt_str(50)
I am also using a derived column - to add as a new column: The goal is write an expression
I have a [column] which is defined as 11 characters
My question is how do I write an expression to only capture 10 characters, and anything greater than 10 I want to change the [column] to -1 else (dt_I8) [column]
I've tried:
FINDSTRING([Column],"9999999999",1) == 10 ? -1 : (DT_I8)TRIM([Column])
FINDSTRING([Column],"9999999999",1) > 10 ? -1 : (DT_I8)TRIM([Column])
LEN([Column]) == 10 ? -1 : (DT_I8)[column]
SUBSTRING( [Copy of Member ID] ,1,10)
The package runs without errors however the results in the table are not correct, the column with more than 10 characters are not showing up in the table
I am using visual studio 2012
Thank you Dawana
I don't know why your substring attempt didn't work, but this would return the first 10 characters of column:
LEFT(column,10)
https://msdn.microsoft.com/en-us/library/hh231081(v=sql.110).aspx

Query to find zip codes with ending '0000'

I need to find all zip codes in the database that end with '0000'. Using the below query, I was able to return all zip codes that are 9 digits in length. But how do I add return back only those zipcodes that have are 9 digits in length AND have 0000? I'm sure it's simple but I'm still really new to querying. :)
example: 922340000
Select Addresszipcode
from dbo.CR_MEMBER_AllMemberDetails
where len(Addresszipcode) = 9
Select Addresszipcode
from dbo.CR_MEMBER_AllMemberDetails
where len(Addresszipcode) = 9
and Addresszipcode like '%0000'
SELECT Addresszipcode
FROM dbo.CR_MEMBER_AllMemberDetails
WHERE LEN(AddressZipCode)= 9 AND RIGHT(AddressZipCode, 4) = '0000'
You need to use a combination of the LEN() function and the LIKE operator.
SELECT Addresszipcode FROM dbo.CR_MEMBER_AllMemberDetails
WHERE LEN(AddressZipCode)=9 AND AddressZipCode LIKE '%0000'
The LEN(AddressZipCode) part of the WHERE clause will only return rows with a length of 9. The AddressZipCode LIKE '%0000' will only return rows that end with 0000. When using the LIKE operator, the percent sign % acts as a wildcard. In essence, you're saying "WHERE AddressZipCode starts with anything and ends with 0000".
EDIT
In response to your comment, here is the SQL to trim the 0's:
SELECT CASE WHEN LEN(AddressZipCode)=9 AND AddressZipCode LIKE '%0000'
THEN LEFT(AddressZipCode,5) ELSE AddressZipCode END AS AddressZipCode

TSQL search exact match into a string

I stumbling on an issue with string parsing; what I'm trying to achieve is substitute a marker string with a value but the string match needs to be perfect.
Keep in mind that before the compare I split the entire string in a table (rowID int, segment nvarchar(max)) wherever i find a space so, a thing like 'The delta_s is §delta_s' will look like:
rowID | segment
1 | the
2 | deltaT_s
3 | is
4 | §deltaT_s
After this i cycle each row with my table of "replacements" (idString nvarchar(max), val float); example:
Marker string (#segment): '§deltaT_s'
String to replace (#idString): '§deltaT_s'
The instruction I am using (since "like" is a lost cause as far I can see):
SELECT STUFF(#segment, PATINDEX('%'+#idString+'[^a-z]%', #segment), LEN(#idString), CAST(#val AS NVARCHAR(MAX)))
with #val being the number to substitute taken from the "replacements" table.
Now, in my table of "replacements" i have 2 delta like markers
1) §deltaT_s
2) §deltaT
My issue is that when the cycle start comparing the segments with the markers and the §deltaT comes up it will substitute the first part of the string in this way
'§deltaT_s' -> '10_s'
I don't understand what I am doing wrong with the REGEX anyone can give me and hand on this matter?
I am available in case more info are required.
Thank you,
F.
If possible you should change the marking style putting a paragraph symbol (§) at both side of the token, making one of the example in your comment
the deltaT_s is §deltaT_s§, see ya!
doing that the sentence will be split as
rowID | segment
--------------------
1 | the
2 | deltaT_s
3 | is
4 | §deltaT_s§,
5 | see
6 | ya!
if the replace values are stored in a fact table you will have something like
token | value
------------------
§deltaT§ | foo
§deltaT_s§ | 10
or you can fake it putting the symbol at the end of the token in you query.
Than it's possible to search for the substitution with a LIKE and a LEFT JOIN between the two tables
SELECT COALESCE(REPLACE(segment, t.token, t.value), segment) Replaced
FROM Sentence s
LEFT JOIN Token t ON s.segment LIKE '%' + t.token + '%'
SQLFiddle demo
If you cannot change the fact table you can fake the change adding the symbol after the token
SELECT COALESCE(REPLACE(segment, t.token, t.value), segment) Replaced
FROM Sentence s
LEFT JOIN Token t ON s.segment LIKE '%' + t.token + '§%'
Maybe it is not an option, but for me helped ones.
If you can use Regex in sql or create CLR functions, look at this link http://www.sqllion.com/2010/12/pattern-matching-regex-in-t-sql/ last 2 options.
For you the best will be to take last choice using CLR function.
Then you will can do like this:
Text: the deltaT_s is §delta, see ya!
Regex: (?<=[^a-z])§delta(?![a-z_]) - this (?<=[^a-z]) means that will not take to match and (?![a-z_]) is not followed by letters and underline.
Replace to : 10
I also have tried regex \b§delta\b (\b :Start or End of word), but it seems it doesn't like §

Ampersand (&) operator in a SQL Server WHERE Clause

Sorry for the very basic question. What does the & operator do in this SQL
WHERE (sc.Attributes & 1) = 0
sc is an alias for a table which contains a column attributes.
I'm trying to understand some SQL in a report and that line is making it return 0 entries. If I comment it out it works. I have limited SQL knowledge and I'm not sure what the & 1 is doing.
& is the bitwise logical and operator - It performs the operation on 2 integer values.
WHERE (sc.Attributes & 1) = 0
The above code checks to see if sc.Attributes is an even number. Which is the same as saying that the first bit is not set.
Because of the name of the column though: "Attributes", then the "1" value is probably just some flag that has some external meaning.
It is common to use 1 binary digit for each flag stored in a number for attributes. So to test for the first bit you use sc.Attributes&1, to test for the second you use sc.Attributes&2, to test for the third you use sc.Attributes&4, to test for the fourth you use sc.Attributes&8, ...
The = 0 part is testing to see if the first bit is NOT set.
Some binary examples: (== to show the result of the operation)
//Check if the first bit is set, same as sc.Attributes&1
11111111 & 00000001 == 1
11111110 & 00000001 == 0
00000001 & 00000001 == 1
//Check if the third bit is set, same as sc.Attributes&4
11111111 & 00000100 == 1
11111011 & 00000100 == 0
00000100 & 00000100 == 1
It is a bitwise logical AND operator.
It's a bitwise and.
Seeing as you tagged this as sql server, I thought I'd add something from a different angle as also ran into one of these this week.
These can hurt the performance of your queries if used in the predicate. Very easy to manufacture an example of your own. Here is the snippet from my query
WHERE
advertiserid = #advertiserid
AND (is_deleted & #dirty > 0)
WHERE
advertiserid = #advertiserid
AND (is_deleted > 0 AND #dirty > 0)
by simply defining each column with a proper value this allowed the optimizer to remove a bookmark lookup and performance stats showed a X10 performance increase.

Resources