Teradata SQL - Syntax to spool varchar for CASE OF DATA - sql

I have a VARCHAR(10) column where values can be stored as 'Abxxx' and 'abxxx'
When grouping on this field (Column1) they are all returning as uppercase 'Abxxx' even when there are lower case values 'abxxx' in the data.
What syntax can I use to return unique data values in separate rows in the spool?
Read only access.
SELECT
Column1, COUNT(unique_id)
FROM
Table
GROUP BY
Column1
Desired result:
Abxxx 345 abxxx 5678

Using (CASESPECIFIC) will get the result you want:
Select Column1 (CASESPECIFIC), count(unique_id)
FROM Table
GROUP BY Column1 (CASESPECIFIC)

Be sure to make the table column case specific, see below.
-- Create a table with one column case-insensitive, another column case-sensitive
CREATE TABLE cities
(
name VARCHAR(80) NOT CASESPECIFIC,
name2 VARCHAR(80) CASESPECIFIC
);
-- Insert a row
INSERT INTO cities VALUES ('San Diego', 'San Diego');
-- Name column is case-insensitive
SELECT * FROM cities WHERE name = 'SAN DIEGO';
-- Output: San Diego
-- Name2 column is case-sensitive
SELECT * FROM cities WHERE name2 = 'SAN DIEGO';
-- No rows found

Related

presto create table from another table with double quotes with comma separated

How to create table in presto from a select query that has comma-separated values inside the column itself.
e.g
select statement has values look like
Column1
column2
column3
1
2
abc,xyz
below is the create statement for table
CREATE TABLE resultTable
WITH (format = 'TEXTFILE',textfile_field_separator = ',')
AS
select Column1 ,column2 column3 from sourceTable
but this ends up with missing placing fields which have comma separated values

How to split the column into multiple column and insert new column into same table in snowflake

I am trying to split column into mulitple column and insert into same table using snowflake query.
EMP Table
ID NAME AGE
1 Ravi#Kumar 25
2 Virat#Singh 26
3 Rohit#Sharma 27
EMP Table after split
ID NAME F_NAME L_NAME AGE
1 Ravi#Kumar Ravi Kumar 25
2 Virat#Singh Viart Singh 26
3 Rohit#Sharma Rohit Sharma 27
I am able to select the data and spilt but I wanted to alter the existing table only.
Create source data.
create or replace table emp_source as
select ID, NAME, AGE
from (values
(1, 'Ravi#Kumar' , 25),
(2, 'Virat#Singh', 26),
(3, 'Rohit#Sharma', 27) t (ID, NAME, AGE)
);
We can split NAME using STRTOK as a slightly simpler alternative to REGEX_SUBSTR suggested by Tim Biegeleisen.
Select
ID,
strtok(NAME, '#',1) f_name,
strtok(NAME, '#',2) l_name,
AGE
from emp_source;
You could use computed/derived columns for this as the source column NAME contains the data you need per row:
computed/derived columns
create or replace table emp
(id bigint,
name varchar,
f_name varchar as strtok(NAME, '#',1),
l_name varchar as strtok(NAME, '#',2),
age integer
)
;
Insert the 3 source columns from source data into EMP table.
Insert into emp
Select id,name,age from emp_source;
Query EMP and the computed/Derived columns are calculated on the fly
Select * from emp;
If your EMP table is already created you could use alter table to switch them to derived/computed columns.
# Drop current columns if they already exist in table
alter table emp drop column f_name;
alter table emp drop column l_name;
# Add derived/computed columns
alter table emp add column f_name varchar as strtok(NAME, '#',1);
alter table emp add column l_name varchar as strtok(NAME, '#',2);
Now when you query the table the column values will be computed on the fly and returned, and you don't need to Update the table.
Note: No metadata is gathered on those columns, so if queries are likely to filter on them you may be better deriving them at Insert time or updating them. Also those columns will be computed for every query that selects them, so if they are used a lot it may also be better to UPDATE the table with the physical values
We can use REGEXP_SUBSTR() here:
SELECT
ID,
NAME,
REGEXP_SUBSTR(NAME, '([^#]+)', 1, 1, 'e', 1) AS F_NAME,
REGEXP_SUBSTR(NAME, '([^#]+)', 1, 2, 'e', 1) AS L_NAME,
AGE
FROM yourTable;
If you actually want to update your table, then add the F_NAME and L_NAME columns, and then use:
UPDATE yourTable
SET F_NAME = REGEXP_SUBSTR(NAME, '([^#]+)', 1, 1, 'e', 1),
L_NAME = REGEXP_SUBSTR(NAME, '([^#]+)', 1, 2, 'e', 1);

Concatenate column names in sql select statement if certain condition is met

I have a SQL SELECT statement pulling 4 columns:
SELECT cmm_residence, cmm_packing, cmm_furniture, cmm_overage
These columns are simple no/yes values (0,1). I need an additional column Description to concatenate any 'Yes' values for that row.
For example: if the query returned 'Yes' for cmm_residence and cmm_furniture, the Description column should be cmm_residence,cmm_furniture.
Schema and insert statements:
create table mytable (cmm_residence varchar(5), cmm_packing varchar(5), cmm_furniture varchar(5), cmm_overage varchar(5));
insert into mytable values('yes','no','no','yes');
insert into mytable values('no','no','no','yes');
insert into mytable values('yes','yes','no','yes');
Query:
select cmm_residence, cmm_packing, cmm_furniture, cmm_overage,
concat(case when cmm_residence='yes' then ' cmm_residence ,'end,
case when cmm_packing='yes' then ' cmm_packing ,'end,
case when cmm_furniture='yes' then ' cmm_furniture ,'end,
case when cmm_overage='yes' then ' cmm_overage 'end)Description
from mytable
Output:
cmm_residence
cmm_packing
cmm_furniture
cmm_overage
Description
yes
no
no
yes
cmm_residence , cmm_overage
no
no
no
yes
cmm_overage
yes
yes
no
yes
cmm_residence , cmm_packing , cmm_overage
db<fiddle here
You can use a CASE into your SELECT statement adding whatever logic you need like this:
SELECT cmm_residence, cmm_packing, cmm_furniture, cmm_overage,
CASE
WHEN cmm_residence = 1 THEN 'cmm_residence'
WHEN cmm_packing = 1 THEN 'cmm_packing'
ELSE ''
END As 'Description'
FROM Table
Just add as much cases with logic as you want, take into account this isn't quite performant query.

How to search for a pattern (MRS.) in SQL Server 2008?

How to search for a pattern (MRS.) in SQL Server 2008? I have used escape operator and [], but it was not working
use next wild card, and if the answer is not accurate, provide us with sample and desired data.
ColumnName like '%(MRS.)%'
Demo:-
Create table MyTable (id int , name nvarchar(200))
insert into MyTable values (1 , 'aaaaaa (MRS.)bbbbb')
insert into MyTable values (2 , '1111 (MRS.)2222')
insert into MyTable values (3 , 'qqqqqqqq')
insert into MyTable values (4 , 'ffffffffff')
select * from MyTable
where name like '%(MRS.)%'
Result:-
id name
1 aaaaaa (MRS.)bbbbb
2 1111 (MRS.)2222

How do I return the column name in table where a null value exists?

I have a table of more than 2 million rows and over 100 columns. I need to run a query that checks if there are any null values in any row or column of the table and return an ID number where there is a null. I've thought about doing the following, but I was wondering if there is a more concise way of checking this?
SELECT [ID]
from [TABLE_NAME]
where
[COLUMN_1] is null
or [COLUMN_2] is null
or [COLUMN_3] is null or etc.
Your method is fine. If your challenge is writing out the where statement, then you can run a query like this:
select column_name+' is null or '
from information_schema.columns c
where c.table_name = 'table_name'
Then copy the results into a query window and use them for building the query.
I used SQL Server syntax for the query, because it looks like you are using SQL Server. Most databases support the INFORMATION_SCHEMA tables, but the syntax for string concatenation varies among databases. Remember to remove the final or at the end of the last comparison.
You can also copy the column list into Excel and use Excel formulas to create the list.
You can use something similar to the following:
declare #T table
(
ID int,
Name varchar(10),
Age int,
City varchar(10),
Zip varchar(10)
)
insert into #T values
(1, 'Alex', 32, 'Miami', NULL),
(2, NULL, 24, NULL, NULL)
;with xmlnamespaces('http://www.w3.org/2001/XMLSchema-instance' as ns)
select ID,
(
select *
from #T as T2
where T1.ID = T2.ID
for xml path('row'), elements xsinil, type
).value('count(/row/*[#ns:nil = "true"])', 'int') as NullCount
from #T as T1