I have query to find out the duplicates in my data but that query also encountering the data which are not duplicates but my query sees it as a duplicate because my query reading them same. For example 'AABWcFABmAAAyWJAAb' and 'AABWcFABmAAAyWJAAB' but in actual they are unique since this both id holds different data. I have used Collate function but it didn't help. Please let me know if there is any built in function I can use or any logic.
Thank you in advance for the help.
select distinct
npa
,npanxx_row_id
,count()
from kjm.audit_309
where npanxx_row_id
COLLATE Latin1_General_CS_AS in (npanxx_row_id) --and NPANXX_ROW_ID = 'AABWcFABmAAAyWJAAB'
group by npa,npanxx_row_id
having count() >1
order by npa
Another option in SQL Server would potentially be BINARY_CHECKSUM(). This will detect differences in case.
select
your_column
, BINARY_CHECKSUM(your_column)
, COUNT(*)
FROM your_table
GROUP BY
your_column
, BINARY_CHECKSUM(your_column)
HAVING count(*) >1
If you're looking for duplicates, then this should work:
CREATE TABLE #case_sensitivity_training (my_str VARCHAR(40) NOT NULL)
INSERT INTO #case_sensitivity_training (my_str)
VALUES ('AABWcFABmAAAyWJAAb'), ('AABWcFABmAAAyWJAAB')
SELECT
my_str COLLATE SQL_Latin1_General_CP1_CS_AS,
COUNT(*)
FROM
#case_sensitivity_training
GROUP BY
my_str COLLATE SQL_Latin1_General_CP1_CS_AS
HAVING
COUNT(*) > 1
You've put the collation in the wrong spot if you want to use DISTINCT.
The following should work on SQL Server:
SELECT DISTINCT
(ColumnName) COLLATE sql_latin1_general_cp1_cs_as
From TableName
But that collation may vary from RDBMS to RDBMS.
Related
The results of my SQL query include null values. How do I filter out null values?
The syntax may vary depending on the database you are using but you can explicitly exclude nulls in the where clause. For example, the following will exclude null values in the primary_author field:
SELECT
date,
primary_author,
ISBN
FROM
books
WHERE
primary_author IS NOT NULL;
My example works on every database I know, so it should work for you =)
SELECT *
FROM TABLE_NAME
WHERE COLUMN_NAME IS NOT NULL
Here you can find a simple explanation and some examples: https://www.w3schools.com/sql/sql_null_values.asp
But some times you want to replace null values for a default value, like 'X', in this case, we should know the database for correct syntax, here some examples:
Oracle:
SELECT nvl(column_name,'X')
FROM TABLE_NAME
Sqlite:
SELECT ifnull(column_name,'X')
FROM TABLE_NAME
SqlServer:
SELECT coalesce(column_name,'X')
FROM TABLE_NAME
I need to compare table counts for an Oracle schema to a SQL Server database. However, when I make my query, the results are always off because of the way each handles the underscore ('_') in terms of ordering. I've included an example of what I'm seeing below.
In Oracle:
SELECT FIELD1 FROM ORACLE_ORDER ORDER BY FIELD1 ASC;
Result:
'ABC'
'ABCD'
'ABC_D'
In SQL Server:
SELECT FIELD1 FROM SQL_ORDER ORDER BY FIELD1 ASC;
Result:
'ABC'
'ABC_D'
'ABCD'
As you can see from above, oracle and sql server both treat the underscore differently when it comes to ordering. How can I modify either of the queries (or environments) to make them order the same as the other?
In the SQL Server Side use the following
Select * from SQL_ORDER
ORDER BY FIELD1 Collate SQL_Latin1_General_CP850_BIN
The collation SQL_Latin1_General_CP850_BIN makes it to be used with ASCII values. In this case ASCII of underscore is 95, A being 65, and Z being 90. Remember lower case "a" will have a higher value than upper case "A" and so on.
Here is the fiddle
Simple way is to use Collate SQL_Latin1_General_CP850_BIN function in ORDER BY to achieve this
SELECT * FROM (
SELECT 'ABC' AS TAB UNION
SELECT'ABC_D'UNION
SELECT'ABCD'UNION
SELECT'ABC_'UNION
SELECT 'ABC' UNION
SELECT'A_C' UNION
SELECT'ABC_DE_FGH'UNION
SELECT'ABCXDEYFGH') AS X
ORDER BY X.Tab Collate SQL_Latin1_General_CP850_BIN
When I use following condition in SQL Query
[FieldName] like N'%өө%'
here the field name can be replaced with any column in any table you have..collation is SQL_Latin1_General_CP1_CI_AS
I tested on SQLServer express,SQLServer 2012,2016 for any type of field.
Any ideas on why this is happening ?
Can anybody please confirm if it is same for you?
below is a simple repro:
create table dbo.test111
(
id int
)
insert into dbo.test111
select ROW_NUMBER() over (order by (Select null))
from
sys.objects s
cross join
sys.objects s1
select * from dbo.test111 where id like
N'%өө%'
In your case, you need to use a collation for Supplementary Characters
[FieldName] like N'%өө%' collate Latin1_General_100_CI_AS_SC
This should yield the expected result.
Explore the demo and see that removing the collation causes all records to be returned.
Demo
how to
select name,family from student where name="X"
without its column name.
for example :
select "column1","column2" from student where "column1"="x"
or for example
select "1","2" from student where "1"="x"
"1" is column1
"2" is column2
i dont want to say columns name. i want to say just its number or other....
idont tired from select *. but it just for that i dont know the columns name but i know where they are. my columns name are change every i want to read the file but its data are same, and the data are in the same columns in each file.
Although you can not use field positions specifiers in the SELECT statement, the SQL standard includes the INFORMATION_SCHEMA where the dictionary of your tables is defined. This includes the COLUMNS view where all the fields of all tables are defined. And in this view, there is a field called ORDINAL_POSITION which you can use to assist in this problem.
If you query
SELECT ORDINAL_POSITION, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TABLE'
ORDER BY ORDINAL_POSITION
then you can obtain the column names for the ordinal positions you want. From there you can prepare a SQL statement.
You could use temp table as:
DECLARE #TB TABLE(Column1 NVARCHAR(50),...)
INSERT #TB
SELECT * FROM student
Then use it:
SELECT Column1 FROM #TB WHERE Column1='aa'
If it's a string you can do this :
Select Column1 + '' From Table
If it's a number you can do this :
Select Column1 + 0 From Table
If it's a datetime you can do this :
Select dateadd(d, 0, Column1) From Table
And similarly for other data types..
No, you can not use the ordinal (numeric) position in the SELECT clause. Only in Order by you can.
however you can make your own column alias...
Select Column1 as [1] From Table
You can use alias:
SELECT name AS [1], family AS [2] FROM student WHERE name="X"
It's just not possible. Unfortunately, they didn't think about table-valued functions, for which information_schema is not available, so good luck with that.
I currently have a sql statement that outputs data into an excel document. However, every time there is a cell that doesn't have data, it outputs NULL into the cell. I was wondering if there was a way that I could replace NULL with just a blank space? Someone suggested using coalesce to do this, however I haven't ever had the chance to use it so I will have to do some reading on it. Anyone have any other suggestions?
In your select you can put an IsNull/IfNull round the column. Not particularly efficient but does what you want.
MS SQL
Select IsNull(ColName, '') As ColName From TableName
MySQL
Select IfNull(ColName, '') As ColName From TableName
IFNULL is an Oracle extension (adopted by many other platforms). The "standard" SQL function is coalesce():
SELECT COALESCE(columnName, ' ') AS ColumnName
FROM tableName;
Microsoft SQL
UPDATE TABLE_NAME
SET COLUMN_NAME = ''
WHERE COLUMN_NAME IS NULL
NOTE: this will SET, not select the result in your TABLE
MySQL
select IFNULL(columnName, '') from tableName
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.
SELECT teacher.name, IfNull(dept.name, '') as Dept
FROM teacher left outer JOIN dept
ON teacher.dept=dept.id