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')
Related
I have a table and the values like this
000001U;000002;000003U;000004;000005U;000006U
and I want display the field is like
000002;000004;
Try This
DECLARE #Table AS TABLE (Data nvarchar(1000))
INSERT INTO #Table
SELECT '000001U;000002;000003U;000004;000005U;000006U'
SELECT STUFF((SELECT '; '+Data
FROM
(
SELECT Split.a.value('.','nvarchar(1000)') AS Data
FROM
(
SELECT
CAST('<S>'+REPLACE(Data,';','</S><S>') +'</S>' AS XML ) AS Data
FROM #Table
)AS A
CROSS APPLY Data.nodes('S') AS Split(a)
)dt
WHERE CHARINDEX('U',Data)=0 FOR XML PATH('')),1,1,'') AS Data
Result
Data
---------
000002; 000004
As mentioned in the comments, SQL Server does not have any native regex replacement support. But, if you can get a dump of your entire table/column, then you can easily do a regex replacement in another tool, such as Notepad++.
Do a find on this pattern:
[0-9]+U;?
And then just replace with empty string. This should leave each row with the data you want to see. Here is a demo showing that this works in Java.
Demo
for SQL Server 2016 and later.
select stuff (
(select ',' + value
from STRING_SPLIT ('000001U;000002;000003U;000004;000005U;000006U', ';')
where right(value, 1) <> 'U'
for xml path('')),
1, 1, '')
for earlier version, you may use any CSV Spliter like this from Jeff Moden http://www.sqlservercentral.com/articles/Tally+Table/72993/
Simple way is to determine the value by IsNumeric function.
DECLARE #GIVEN VARCHAR(MAX)='000001U;000002;000003U;000004;000005U;000006U';
DECLARE #FINAL VARCHAR(MAX)='';
SELECT #FINAL =#FINAL+ case when ISNUMERIC(val)=1 then val+';' else '' end FROM (
SELECT split.x.value('.','varchar(max)') VAL FROM(
SELECT CAST('<M>'+REPLACE(#GIVEN,';','</M><M>')+'</M>' AS XML) AS VAL
)A
CROSS APPLY a.VAL.nodes('/M') as split(x)
)AA
PRINT #FINAL
Result: 000002;000004;
I have to get the count of specific word from the column in the table.
Example : assume this value is in the column:
uid-234,uid-342,uid-345
I need to retrieve the count as 3 by using T-SQL in SQL Server.
Try this, It should work
SELECT SUM(len(YourColumn) - len(replace(YourColumn, ',', '')) +1)
AS TotalCount
FROM YourTable
Try this,
DECLARE #Column VARCHAR(100) = 'uid-234,uid-342,uid-345'
SELECT len(#Column) - len(replace(#Column, ',', '')) + 1 AS TotalCount
You can try following code
select
*, (select count(*) from dbo.Split(concatenatedColumn,',')) cnt
from myTable
But you need to create the user defined function SPLIT string on your database first
I have T-SQL code and am researching how to split
Aruba\abc
Spain\defg
New Zealand\qwerty
Antartica\sporty
Such that the column outputs
abc
defg
qwerty
sporty
So far, I found something like this,
http://www.aspsnippets.com/Articles/Split-function-in-SQL-Server-Example-Function-to-Split-Comma-separated-Delimited-string-in-SQL-Server-2005-2008-and-2012.aspx
But it splits column based on delimiters into new columns.
I wish to keep the information AFTER the delimiter \
Please advise
SELECT RIGHT(ColName , LEN(ColName) - CHARINDEX('\', ColName) )
FROM TABLEName
OR
SELECT PARSENAME(REPLACE(ColName , '\' , '.'),1)
FROM TableName
If you have it as a variable example:
DECLARE #str VARCHAR(50) = 'aruba\abc'
SELECT SUBSTRING(#str,CHARINDEX('\', #str)+1, LEN(#str) - CHARINDEX('\', #str) )
If you have it in a table example:
SELECT SUBSTRING(column1,CHARINDEX('\', column1)+1, LEN(column1) - CHARINDEX('\', column1) )
FROM table1
Here's a sqlfiddle of it working : http://sqlfiddle.com/#!6/85de5/1
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
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)