SQL Server Wildcard Search - sql-server

I'm using SQL Server 2014 and I'm getting incorrect results back from a LIKE expression. I'm trying to match this type of string A-ABC123-AABB where ABC123 and AABB can be any length strings.
Here is the LIKE expression:
ID.name LIKE 'a-%-'+STR.STR
If the iteration has STR as a string with a prefixed dash and there exists an STR with no P, it will return the value with no dash.
How can I do strict matching so that it shows only the result with the dash, IF STR has a string?
Example:
STR table:
col STR Val
----------------
1 test 10
2 -test 12
ID table:
col name
-----------------------
1 a-blah-test
2 a-blah--test
3 a-tst-test
Results:
name val
--------------------
a-blah-test 10
a-blah--test 10
a-blah--test 12

This might fix your problem here although I'm not sure it will handle all the general cases correctly.
ID.names LIKE 'P-%[^-]-'+ STR.STR

Related

How to extract somephone number from 1 string in SQL Server?

I have a string like :
XLNTT018 : XXX 13 xxx, 0355500000 Note: xxx, 0367000000 ,xxxxx 0768500000
in one of my columns. I want to extract the number with 10 digits length. For example, in the above case, I want to extract 0355500000 | 0367000000 | 0768500000 in SQL Server.

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

Adding the value in column of SQL Server 2012

I need to add two zero in POS data to make it 14th character numbers because TV UPC has 14th character numbers UPC.
For example
Table 1
POS UPC
------------
123456789012
Table 2
TV UPC
--------------
00123456789012
I have to make it POS UPC 14 character number by adding two zero at the beginning of the number to compare exact match.
Prefix the number with 14 0s and select 14 characters from right which will give you desired output
DECLARE #Number varchar(100)='123456789012'
SELECT RIGHT('00000000000000'+#Number,14)
Output
00123456789012
Replace variable with your column name.
Fiddle here : http://sqlfiddle.com/#!3/9f1d32/1

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 §

Sum Function and Changing the type of Variable

I'm using SQL Server Express for my project. I have a table like:
Number Name Surname Point Position
------ ---- ------- ----- --------
1 John Black 10000 True
2 Jane Lincoln 8800 True
3 Edward Payne 17000 False
ETC...
I would like to prepare a query that will sum the Point where the position is true
SELECT Sum(Point) AS Exp1
FROM DataTable
WHERE Position = True
My problem is the type of Position is Nvarchar. So the query doesn't sum the Point s. I tried the change the type Nvarchar to int, but I have a big project and it give several errors. Is there any way to make a query to sum the Point ?
(I've tried to use Sum(Var(Point)) but not work)
use
select sum(CAST(Point AS INT)) as Exp1 from DataTable where position = 'True'
or
select sum(CONVERT(int, Point)) as Exp1 from DataTable where position = 'True'

Resources