How to extract the following using SQL Server - sql-server

Example: Front Office Manager 0105212000
Only need 10521200, so I am dropping the first and last characters.
Before:
0105212000
After:
10521200
Here's what I came up with so far.
SELECT DISTINCT NAME, right(DESCRIPTION, 9) as 'DESC', DESCRIPTION
FROM LABORLEVELENTRY
WHERE LABORLEVELDEFID = '201'
AND INACTIVE = '0'
ORDER BY NAME

SELECT DISTINCT NAME,
SUBSTRING(DESCRIPTION, 2, LEN(DESCRIPTION)-2) as 'DESC',
DESCRIPTION
FROM LABORLEVELENTRY
WHERE LABORLEVELDEFID = '201'
AND INACTIVE = '0'
ORDER BY NAME

SELECT SUBSTRING ( DESCRIPTION , 2 , LEN(DESCRIPTION)-2 )
Starting on the second character, select everything else but the last character

How about:
select left(right('Front Office Manager 0105212000', 9), 8)
So:
SELECT DISTINCT NAME,
left(right(DESCRIPTION, 9), 8) as 'DESC', DESCRIPTION
FROM LABORLEVELENTRY
WHERE LABORLEVELDEFID = '201'
AND INACTIVE = '0'
ORDER BY NAME

Related

create a calculated column from comparing two columns based off a substring in the columns in sql

I have the following dataset
Drawing Name
Line Number
Line Details
PL00XXX-0705-1300
2"-MSH-0513-16-C1-1 1/2"A
MATCH
PL00XXX-0705-1100
2"-MSH-0513-16-C1-2"AE
DUPLICATE / HEAT TRACE
PL00XXX-0705-1300
2"-WWS-0513-15-C1-0"
MATCH / NON ISO
PL00XXX-0705-1300
2"-WWS-0513-15-C1-2"AE
MATCH / HEAT TRACE
PL00XXX-0705-1100
2"-WWS-0513-15-C1-2"AE
DUPLICATE / HEAT TRACE
PL00XXX-0705-1300
2"-WWS-0513-17-C1-2"AE
DO NOTHING
PL00XXX-0705-1100
2"-WWS-0513-18-C1-2"AE
DO NOTHING
The new calculated column I want to create is Line Details based if there are at LEAST 2 of a line number up to the last dash in the line number. IF there is not at least 2 of the same up to the last dash - do nothing.
The Line Details column shows Match if the drawing number would have 05-13 in it and the line number would have 0513 in it.
The Line Details column would show Duplicate if the drawing number had 05-13 in it and the line number had 0511 in it.
The Line Details column could ALSO show Heat Trace if the line number ends with an E.
The Line Details column could ALSO show Non Iso if the line number ends with 0".
The drawing number up to PL00XXX-07 is always the same per customer. it's what comes after that is important and how it's tied or not tied to the line number. At least 2 of the same line number means up to the - after C1. The amount of characters prior to that could be different, there could be a 2" line or a 1/2" line, but as long as the line number matches up to the C1- part of the line that represents 2 of the same.
IF this makes send PLEASE help. Greatly appreciated.
How can a query be written to only find duplicates up to the last hypen? I have the following line numbers:
2"-MSH-0513-16-S1-**1 1/2"A
2"-MSH-0513-16-S1-**2"AE
2"-MSH-0513-17-S1-**1 1/2"A
2"-MSH-0513-18-S1-**1 1/2"A
2"-FLW-0521-18-S1-**1"A
2"-FLW-0521-18-S1-**1"A
So the line numbers that I want to be shown in the list after the query is as follows:
2"-MSH-0513-16-S1-**1 1/2"A
2"-MSH-0513-16-S1-**2"AE
2"-FLW-0521-18-S1-**1"A
2"-FLW-0521-18-S1-**1"A
I know how to query a specific character count ONLY when the data is the exact same character count in the column. As you can see the character count can be different up the the last hyphen.
I have tried the following script:
select SUBSTRING(LINE_NUM_CONCAT_,
1,
regexp_instr(LINE_NUM_CONCAT_,
'-',
1,
regexp_count(LINE_NUM_CONCAT_,
'-')
) - 1)
FROM PID_Components_PROCESS_LINES
but regex_count is not a recongnized built-in function name???
PLEASE help.
use CASE expression to evaluate your condition and return the string accordingly and concatenate all as one string
to check for duplicates use window function count(*) over (partition by ...)
select [Drawing Name], [Line Number],
[Line Details] =
case when count(*) over (partition by line) < 2
then 'DO NOTHING'
else
case when [Drawing Name] like '%05-13%'
and [Line Number] like '%0513%'
then 'MATCH'
else ''
end
+ case when [Drawing Name] like '%05-11%'
and [Line Number] like '%0513%'
then 'DUPLICATE'
else ''
end
+ case when right([Line Number], 1) = 'E'
then '/ HEAT TRACE'
when right([Line Number], 2) = '0"'
then '/ NON ISO'
else ''
end
end
from PID_Components_PROCESS_LINES p
cross apply
(
select line = left([Line Number],
len([Line Number]) - charindex('-', reverse([Line Number]))
)
) l
where p.[Drawing Name] like 'PL00528%'
I have reorganized the query and using APPLY() operator to compute the various value. You can add the WHERE condition to exclude the NO NOTHING lines.
select *,
case when line_count < 2
then 'DO NOTHING'
else
m.match
+ duplicate
+ case when right([Line Number], 1) = 'E'
then '/ HEAT TRACE'
when right([Line Number], 2) = '0"'
then '/ NON ISO'
else ''
end
end
from (
select *, line_count = count(*) over (partition by line)
from PID_Components_PROCESS_LINES
cross apply
(
select line = left([Line Number],
len([Line Number]) - charindex('-', reverse([Line Number])))
) l
where [Drawing Name] like 'PL00528%'
) p
cross apply
(
select match = case when [Drawing Name] like '%05-13%'
and [Line Number] like '%0513%'
then 'MATCH'
else ''
end
) m
cross apply
(
select duplicate = case when [Drawing Name] like '%05-11%'
and [Line Number] like '%0513%'
then 'DUPLICATE'
else ''
end
) d
where p.line_count >= 2 -- exclude the do nothing lines

String split based on condition

I have few string with numbers like this; and its around 3000 records.
Column
------------
Cell 233567-3455
Cell123-4567
Cell#123-7449
Local 456-0987
1 616 468-7796
1234567-5x2345
234/625-1234
(C)755-7442
5732878-2
5721899-23
6712909-3
7894200-234
2144-57238
5673893/588218
437-4737-5772
How can i find the records like below:
Column
-------------
5732878-2
5721899-23
6712909-3
7894200-234
Once I find this, I need to split those into two parts
1st Column. | 2nd column
------------- |
5732878 | 5732872
5721899 | 5721823
6712909 | 6712903
7894200 | 7894234
I tried to fix This using PARINDEX and CHARINDEX
But somehow its not working.Please help.
I don't know your filtering logic to get to your intermediate set, but this should get your expected final result set. I assumed you only want records where the length of the string to the left of the hyphen is greater than the length on the right and also exclude records with more than 1 hyphen.
SELECT LEFT(telephone, CHARINDEX('-', telephone)-1) AS [1stTelephone],
STUFF(
--get the string before the hyphen
LEFT(telephone, CHARINDEX('-', telephone)-1),
--get the starting location of chars we are going to replace
LEN(LEFT(telephone, CHARINDEX('-', telephone)))-LEN(RIGHT(telephone, CHARINDEX('-', REVERSE(telephone))-1)),
--get the length of the section we are replacing
LEN(RIGHT(telephone, CHARINDEX('-', REVERSE(telephone))-1)),
--replace that section with the string after the hyphen
RIGHT(telephone, CHARINDEX('-', REVERSE(telephone))-1)
) AS [2nd telephone]
FROM your_table
WHERE LEN(LEFT(telephone, CHARINDEX('-', telephone))) > LEN(RIGHT(telephone, CHARINDEX('-', REVERSE(telephone))))
AND len(telephone) - len(REPLACE(telephone, '-', '')) = 1
Somewhat dirty method (looks specifically for 7 digits followed by hyphen followed by any number of digits):
SELECT BasePhone AS Phone1, LEFT(BasePhone, 7-LEN(OtherPhoneEnd)) + OtherPhoneEnd AS Phone2
FROM (
SELECT LEFT(Telephone, 7) AS BasePhone, SUBSTRING(Telephone,9,7) AS OtherPhoneEnd
FROM Telephones
WHERE Telephone LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]-%'
)
I assumed based on information you given, that you want numbers with hyphen (-) at 8th position. Try this:
create table #TelNo (
Tel varchar(30)
)
insert #TelNo(Tel)
values ('5732878-2'),
('5721899-23'),
('6712909-3'),
('7894200-234'),
('2144-57238'),
('5673893/588218'),
('437-4737-5772')
select Tel, LEFT(Tel, Len(tel) - len(suffix)) + suffix [SecondTel] from (
select substring(Tel, 1, 7) [Tel], substring(Tel, 9, 10) [suffix] from #TelNo
where CHARINDEX('-', Tel) = 8
)a
You could use something like this:
DDL
use tempdb
create table TelNo (
Tel varchar(30)
)
insert TelNo(Tel)
values ('5732878-2'),
('5721899-23'),
('6712909-3'),
('7894200-234'),
('2144-57238'),
('5673893/588218'),
('437-4737-5772')
Code
select Tel,
case
when Tel like '%_-[0-9]' then left(Tel, len(Tel)-2)
when Tel like '%__-[0-9][0-9]' then left(Tel, len(Tel)-3)
when Tel like '%___-[0-9][0-9][0-9]' then left(Tel, len(Tel)-4)
else Tel
end Tel1,
case
when Tel like '%_-[0-9]' then left(Tel, len(Tel)-3) + right(Tel, 1)
when Tel like '%__-[0-9][0-9]' then left(Tel, len(Tel)-5) + right(Tel, 2)
when Tel like '%___-[0-9][0-9][0-9]' then left(Tel, len(Tel)-7) + right(Tel, 3)
else NULL
end Tel2
from TelNo

Find gender of employees from their SSN

I should find gender of employees from their (SSN).and theory is , if the last number of SSN is even (0,2,4,6,8) gender is woman or/else if the last number of SSN is odd (1,3,5,7,9) gender is man.
SSN of employees look like this in my DataBase ,for ex : 1111112020 or 22222231 - and column name is XX and datatype is nvarchar(30).
The sql i wrote look like this to find out gender of employees and when i executed query i get NULL. Can someone please point me in the right direction. Thanks.
DECLARE #Mand char(5) = '1,3,5,9,7'
DECLARE #Woman char(5) = '0,2,4,6,8'
DECLARE #Gender char (1)
SELECT (
CASE
WHEN right(rtrim(SSN),1) = #Mand THEN 'MAN'
WHEN right(rtrim(SSN),1) = #Woman THEN 'Woman'
ELSE NULL
END) as gender
FROM U
WHERE I = XXX
You cannot use strings and in that way. Just use the explicit list in the query:
SELECT (CASE WHEN right(rtrim(SSN), 1) IN ('1', '3', '5', '7', '9') THEN 'MALE'
WHEN right(rtrim(SSN), 1) IN ('2', '4', '6', '8', '0') THEN 'FEMALE'
END) as gender
FROM dbo.Users
WHERE CustomerId = 214;
Alternatively, you could use LIKE:
SELECT (CASE WHEN rtrim(SSN) LIKE '%[13579]' THEN 'MALE'
WHEN rtrim(SSN) LIKE '%[24680]' THEN 'FEMALE'
END) as gender
FROM dbo.Users
WHERE CustomerId = 214;
I should note that I am not aware that gender is assigned this way for US social security numbers.
Assuming that your SSNs are guaranteed valid (i.e. only contain digits), then the expression RIGHT(SSN,1)%2 (or RIGHT(RTRIM(SSN),1)%2 if there's extra space at the end) will return 0 for females and 1 for males. There's no need for complex string searching when things have been set up to make maths easy.
You can then optionally put that in a CASE if you want to, e.g.
CASE RIGHT(SSN,1)%2
WHEN 0 THEN 'Female'
ELSE 'Male'
END
DECLARE #ie nvarchar(30)
SET #ie = '1111112020'
SELECT (
CASE
WHEN right(rtrim(#ie),1) IN ('1','3','5','9','7') THEN 'MAN'
WHEN right(rtrim(#ie),1) IN ('0','2','4','6','8') THEN 'Woman'
END ) as gender

instead of insert Subquery returned more than 1 value

I am having a problem with the Trigger below. I believe that the issue is when multiple rows are added to the database at once.
It needs to work for n* rows being inserted.
What the code is meant to be doing is upon an insert check the ESHID column vs another table the ID starts after 4 characters
so if there is a match and isPersonalised is 0 then I need to set status as 3 otherwise it should remain as whatever the original FulfilmentStatus was.
ALTER TRIGGER [dbo].[setStatus]
ON [dbo].[ESH_OrderLine]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO ESH_OrderLine(
OrderID,
ESHID,
ESHVersion,
GBPPrice,
Currency,
FulfilmentStatus,
FulfilmentDate
)SELECT
OrderID,
ESHID,
ESHVersion,
GBPPrice,
Currency,
(
SELECT "Status" =
CASE
WHEN isPersonalised = 1 THEN '0' --PERSONALISED CD
ELSE '3' -- NON PERSONALISED CD
END
FROM KSAT_CDs
WHERE ID= SUBSTRING( (
SELECT ESHID FROM INSERTED
WHERE ESHID like 'PHZB%'
),5,10)
UNION
SELECT "Status" =
CASE
WHEN ESHID = 'PNP' THEN '0' --POSTAGE AND PACKING
ELSE '4' --DOWNLOAD
END
FROM INSERTED
WHERE ESHID NOT LIKE 'PHZB%'
) as FulfilmentStatus,
FulfilmentDate
FROM
INSERTED
END
Does changing the first part of your union to this help?
SELECT "Status" =
CASE
WHEN k.isPersonalised = 1 THEN '0' --PERSONALISED CD
ELSE '3' -- NON PERSONALISED CD
END
FROM KSAT_CDs k
inner join inserted i on k.ID = substring(i.ESHID, 5, 10)
where i.ESHID like 'PHZB%'

Order by more than clause in a case statement

I'm working a query that will retrieve various personal information, including first and last names. I want to change the order that the data is returned by based on a passed parameter, called #p_Code. When #p_Code is 4, I want to order by first name, then last name. If it's not 4, then I want to order by last name, then first name.
I'm working with MS Sql.
Here's the query as it stands:
Select Last,
First,
Phone,
Email
From Master.dbo.Cust
Order by
case #p_Code
when '4' then
([First], [Last])
else
([Last], [First])
end
This should do it:
DECLARE #p_Code VARCHAR(10)
SET #p_Code = '4'
Select [Last],
[First],
Phone,
Email
From Master.dbo.Cust
Order by
case WHEN #p_Code = '4' THEN (RANK() OVER (ORDER BY [First], [Last]))
ELSE (RANK() OVER (ORDER BY [Last],[First] )) END
I don't have a test environment handy, but how about this:
Select Last,
First,
Phone,
Email,
(case #p_Code when '4' then [first] else [last] end) as key1,
(case #p_Code when '4' then [last] else [first] end) as key2
From Master.dbo.Cust
Order by key1, key2

Resources