I need to locate rows in a database table that have a field with an upper case value.
E.g.: select * from Cust where Surname like 'COnTAiNS UpPERcASE VaLUeS'
Any assistance would be appreciated.
AJ
You can do a binary comparison using:
select *
from Cust
where cast(Surname as varbinary(120)) != cast(lower(Surname) as varbinary(120))
Another way
SELECT *
FROM Cust
WHERE Surname NOT LIKE '%[^A-Z]%' COLLATE Latin1_General_BIN
This is work for me
SELECT * FROM agents
WHERE email REGEXP BINARY '[A-Z]'
You could do something like this:
SELECT
CASE WHEN BINARY_CHECKSUM('yourStriNg') = BINARY_CHECKSUM(LOWER('yourStriNg'))
THEN 0
ELSE 1
END
....
Rest of SQL statement
Try this to extract only capital letter from given string value:
Declare #Val Varchar(100)
Set #Val='MicrosoftAsp.NeT4You'
--Return Val
Declare #RetCapWord varchar(100)
Set #RetCapWord=''
;WITH CTE AS
(
Select #Val As oldVal,1 As TotalLen,SUBSTRING(#Val,1,1) As newVal,
ASCII(SUBSTRING(#Val,1,1)) As AsciVal
UNION ALL
Select oldVal,TotalLen+1 As TotalLen,
substring(#Val,TotalLen+1,1) As newVal,
ASCII(SUBSTRING(#Val,TotalLen+1,1)) As AsciVal
From CTE
where CTE.TotalLen<=LEN(#Val)
)
Select #RetCapWord=#RetCapWord+newVal
From CTE
Inner Join master..spt_values as m on CTE.AsciVal=m.number and CTE.AsciVal between 65 and 90
Select #RetCapWord
This may be oversimplifying, but works for my use cases:
SELECT *
FROM Cust
WHERE Surname != LOWER(Surname)
Related
My table has a field with data formatted like this:
Term 1~Term 2~Term 3~Term 4~Term 5~Term 6~
All non-blank values contain 6 tilde-separated strings, which may be several words long.
I need to extract the last 2 substrings from this field as part of a query.I'm not interested in splitting the data into multiple records, and I don't have permissions to create a stored procedure.
Thanks in advance for any advice.
DECLARE #Term VARCHAR(100)
SELECT #Term = 'abc~def~ghi~jkl~mno~pqr~'
SELECT RIGHT(#Term, CHARINDEX('~',REVERSE(#Term),CHARINDEX('~',REVERSE(#Term),2)+1)-1)
That will give the last two terms with ~ intact. Note you can wrap REPLACE() around that to put something other than the tilde in there.
another way to do is this.. use string_split (in 2016) or an equivalent UDF that can be found elsewhere.. to split the string
declare #term varchar(100) = 'abc~def~ghi~jkl~mno~pqr~'
; with mycte as (
select
value as string_value
, row_number() over (order by (select 1000) )as row_num
from string_split(#term,'~'))
select top 2 string_value
from mycte
where string_value<>''
order by row_num desc
If I have a table named dbo.cls_members in SQL server and there is a column named meb_refs containing reference numbers like the following
"A03LV4COOD17JE-SN1AM"
How do I find the records of the DISTINCT first character after the "-" symbol. ?
For example in the reference above the bold "S" after the "-".
Perhaps...
SELECT DISTINCT LEFT(STUFF([Column],1,CHARINDEX('-',[Column]),''),1)
FROM YourTable;
Run the following query:
SELECT DISTINCT
[Letter]
FROM
(
SELECT
column_name [Code] --Step One
,RIGHT(column_name, CHARINDEX('-', REVERSE(column_name))-1) [Part after -]
,LEFT(RIGHT(column_name, CHARINDEX('-', REVERSE(column_name))-1), 1) [Letter]
FROM table_name
) T
It's a simple as:
SELECT DISTINCT SUBSTRING(t.col, CHARINDEX('-',t.col)+1, 1)
FROM <yourtable> t;
Try this:
DECLARE #nar NVARCHAR(max) = 'A03LV4COOD17JE-SN1AM'
SELECT RIGHT(left(REVERSE(#nar), CHARINDEX('-',REVERSE(#nar))-1),1)
Alternatively, you can try:
SELECT * from table
where RIGHT(left(REVERSE(Columnname), CHARINDEX('-',REVERSE(Columnname))-1),1)
in ('S')
How can I extract and show only the characters from a string on a column I am searching in SQL Server if the position of the characters varies on the string
Example input:
Mich%ael#
#Scott
Ran%dy
A#nder%son
Output:
%#
#
%
#%
I only able to think of a query like
select
columnname
from
dbo.tablename with (noLock)
where
columnname like '%[%#]%'
but this would not strip and show only the characters I want. I looked at substring() function but this requires knowing the position of the character to be stripped.
If you don't want or can't use a UDF, consider the following:
Declare #YourTable table (SomeField varchar(50))
Insert Into #YourTable values
('Mich%ael#'),
('#Scott'),
('Ran%dy'),
('A#nder%son')
Select A.*
,Stripped = max(B.Value)
From #YourTable A
Cross Apply (
Select Value=Stuff((Select '' + String
From (
Select String= Substring(a.b, v.number+1, 1) From (select A.SomeField b) a
Join master..spt_values v on v.number < len(a.b)
Where v.type = 'P'
) A
Where String in ('%','#') --<<<< This is Your Key Filter
For XML Path ('')),1,0,'')
) B
Group By SomeField
Returns
SomeField Stripped
#Scott #
A#nder%son #%
Mich%ael# %#
Ran%dy %
In this sql server stored procedure query
SELECT HireResponseID,
HireResponse,
DateResponse,
Comments,
YearFileOpened,
file_number,
isCaseOpen,
last_update,
isConfidential,
date_created,
OurClient,
TheirClient,
ProjectName,
description,
lawyer_lastname,
lawyer_firstname,
Conflicts.ConflictID
FROM Hire_Response,
Conflicts,
Lawyers
WHERE Hire_Response.ConflictID=Conflicts.ConflictID
AND Lawyers.lawyerID=Conflicts.lawyerID
AND firmID = #FirmID
AND HireID IN #HireID
AND isStillaConflict = 1
ORDER BY file_number,
TheirClient,
OurClient,
lawyer_lastname,
lawyer_firstname
The parameter #HireID is a string of comma delimited integers (it doesn't have brackets around it). I want to check if the HireID integer is in the #HireID string. But I don't know how to parse this.
Can anyone help please?
Thanks
If I understand your question, you want to find rows where HireID is in the list #HireID. If the HireID is a consistent length, and the list is delimited, then you could use this:
AND #HireID LIKE '%'+CAST(HireID AS VARCHAR(5))+'%'
You could also use CHARINDEX:
AND CHARINDEX(HireID,#HireID) > 0
Edit: To account for inconsistent length, you could use:
AND (#HireID LIKE '%'+CAST(HireID AS VARCHAR(5))+',%'
OR #HireID LIKE '%,'+CAST(HireID AS VARCHAR(5))+'%')
Try this one -
DECLARE #HireID VARCHAR(100)
SELECT #HireID = '2,18'
;WITH cte AS
(
SELECT ID = t.c.value('.', 'INT')
FROM (
SELECT txml = CAST('<t>' + REPLACE(#HireID, ',', '</t><t>') + '</t>' AS XML)
) a
CROSS APPLY txml.nodes('/t') AS t(c)
)
SELECT *
FROM Hire_Response
JOIN Conflicts ON Hire_Response.ConflictID = Conflicts.ConflictID
JOIN Lawyers ON Lawyers.lawyerID = Conflicts.lawyerID
WHERE firmID = #FirmID
AND isStillaConflict = 1
AND HireID in (SELECT ID FROM cte)
ORDER BY file_number, TheirClient, OurClient, lawyer_lastname, lawyer_firstname
I have table with data 1/1 to 1/20 in one column. I want the value 1 to 20 i.e value after '/'(front slash) is updated into other column in same table in SQL Server.
Example:
Column has value 1/1,1/2,1/3...1/20
new Column value 1,2,3,..20
That is, I want to update this new column.
Try this:
UPDATE YourTable
SET Col2 = RIGHT(Col1,LEN(Col1)-CHARINDEX('/',Col1))
Please find the below query also split the string with delimeter.
Select Substring(#String1,0,CharIndex(#delimeter,#String1))
From: http://www.sql-server-helper.com/error-messages/msg-536.aspx
To use function LEFT if not all data is in the form '1/12' you need this in the second line above:
Set Col2 = LEFT(Col1, ISNULL(NULLIF(CHARINDEX('/', Col1) - 1, -1), LEN(Col1)))
SELECT SUBSTRING(ParentBGBU,0,CHARINDEX('-',ParentBGBU,0)) FROM dbo.tblHCMMaster;
I know this question is specific to sql server, but I'm using postgresql and came across this question, so for anybody else in a similar situation, there is the split_part(string text, delimiter text, field int) function.
Maybe something like this:
First some test data:
DECLARE #tbl TABLE(Column1 VARCHAR(100))
INSERT INTO #tbl
SELECT '1/1' UNION ALL
SELECT '1/20' UNION ALL
SELECT '1/2'
Then like this:
SELECT
SUBSTRING(tbl.Column1,CHARINDEX('/',tbl.Column1)+1,LEN(tbl.Column1))
FROM
#tbl AS tbl
SELECT emp.LoginID, emp.JobTitle, emp.BirthDate, emp.ModifiedDate ,
CASE WHEN emp.JobTitle NOT LIKE '%Document Control%' THEN emp.JobTitle
ELSE SUBSTRING(emp.JobTitle,CHARINDEX('Document Control',emp.JobTitle),LEN('Document Control'))
END
,emp.gender,emp.MaritalStatus
FROM HumanResources.Employee [emp]
WHERE JobTitle LIKE '[C-F]%'
Use CHARINDEX. Perhaps make user function. If you use this split often.
I would create this function:
CREATE FUNCTION [dbo].[Split]
(
#String VARCHAR(max),
#Delimiter varCHAR(1)
)
RETURNS TABLE
AS
RETURN
(
WITH Split(stpos,endpos)
AS(
SELECT 0 AS stpos, CHARINDEX(#Delimiter,#String) AS endpos
UNION ALL
SELECT endpos+1, CHARINDEX(#Delimiter,#String,endpos+1)
FROM Split
WHERE endpos > 0
)
SELECT 'INT_COLUMN' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
'STRING_COLUMN' = SUBSTRING(#String,stpos,COALESCE(NULLIF(endpos,0),LEN(#String)+1)-stpos)
FROM Split
)
GO