Counting Null records not working - sql

create table scoda(product varchar(25),price int,sale varchar(23))
insert into scoda values('watch',10,6)
insert into scoda values('socks',8,'NULL')
SELECT COUNT(*) AS GK FROM scoda WHERE sale IS NULL
Expected output:1
Actual output:0
help needed to continue i am beginner in sql

USE
insert into scoda values('socks',8,NULL)
(without quotes).

Problem is in insert statement:
insert into scoda values('socks',8,NULL)

Related

is it possible to insert data where null value is locating by using insert into keyword

Insert into table_name
values('hyd') where col1 isnull;
INSERT syntax can't have WHERE clause. You can use INSERT INTO command for this type of use case.

SQL Escape '#' Symbol

I have a SQL query that inserts data to the database.
Below is my sample data:
OrderID: 1
Email: #TestDistribution
However, when I am trying to insert the OrderID and Email values to the table but the '#' cannot be read by SQL Server.
So that, how would I escape it?
I have an idea of apostrophe's can be escaped using CHAR(29) and what would be the equivalent for #?
Below is my query:
INSERT INTO orders(orderid,email) VALUES(1,'#TestDistribution')
Below is the table structure:
orders
orderid: int
email: nchar(150)
There is no error raised by the server, but upon checking the orders table, the data was not inserted.
Works fine for me on SQL Server 2008 using:
Create/Insert:
CREATE TABLE yourtable
([OrderID] int, [Email] nvarchar(99))
;
INSERT INTO yourtable
([OrderID], [Email])
VALUES
(1, '#TestDistribution')
;
Select:
SELECT * FROM yourtable
Output:
OrderID Email
1 #TestDistribution
you'vve got two ' a the end
try
INSERT INTO orders(orderid,email) VALUES(1,'#TestDistribution')
Make sure that your column type should be varchar
create table orders(orderid int(30),email varchar(30));
insert into orders value(1,'#TestDistribution');
select * from orders;
Thank you for posting your answers here. The issue has been resolved already. I found out that there were some settings in my server which restricts special characters.
Apologies.
Thank you everyone!

Usage of REGEXP_SIMILAR in Teradata

I'm working on Teradata and trying to use REGEXP_SIMILAR function.
*** Teradata Database Release is 14.10.03.10
*** Teradata Database Version is 14.10.03.06
Here's my sample data.
create table test_table(
test_col varchar(20)
);
insert into test_table values('lorem');
insert into test_table values('984kd');
insert into test_table values('ier7j');
insert into test_table values('34535');
insert into test_table values('lore9');
insert into test_table values(' 09sd');
I want to see the records which start with a number.
select test_col, regexp_similar(test_col, '^\d+','i')
from test_table;
test_col regexp_similar(test_col,'^\d+','i')
-------------------- -----------------------------------
lore9 0
lorem 0
09sd 0
ier7j 0
984kd 0
34535 1
But, the above query shows a match only for '34535' row and not for '984kd'. Seems like ^ character(also $) don't have the desired effect.
Isn't REGEXP_SIMILAR similar to REGEXP_LIKE of Oracle?
Can someone explain why is this happening and how to solve this.
^\d+.*
Try this.This will give the result.

Insert rows based on insert statement (nested insert)

A common insert statement is this..
INSERT INTO tbl_name (ID) VALUES (1)
What I wanted to achieve is to Insert an ID using another insert statement from another table.. It would look like this
INSERT INTO tbl_name VALUES (INSERT INTO tbl_name2 (ID) VALUES (1))
I have tried it but it's giving me errors..
INSERT INTO tblReport_OPA (ID_Main) VALUES (INSERT INTO tblReport_OPF (ID_Main) VALUES (1))
I'm currently developing under vb.net 2010 and sql express 2005
You probably can use OUTPUT clause, like this:
INSERT INTO tblReport_OPF (ID_Main)
OUTPUT Inserted.Id_Main
INTO tblReport_OPA
SELECT 1 as Id_Main
Note you'll have to use SELECT instead of VALUES
Opyionally a merge can be used.
merge into #a T1
using (select -1 as ID)Q on Q.ID=T1.ID
WHEN NOT matched by target then
insert(id) values(1)
output
inserted.id
INTO #b;

Inserting a null value into the database

Good Morning,
Say I have an insert statement:
Insert INTO tblTest (fieldOne,FieldTwo,fieldThree) VALUES ('valueOne','valueTwo','null')
This statement doesn't seem to want to insert a null value into the database... I have also tried to insert the word "nothing".
Has anyone any ideas how to make this work? I am using SQL server 2005.
First of all, instead of 'null', try null (lose the quotes)
Then check that the fieldThree column on TblTest doesn't have any constraint prohibiting the use of null values...
Try
Insert INTO tblTest (fieldOne,FieldTwo,fieldThree) VALUES ('valueOne','valueTwo',NULL)
Check for fieldThree not to be NOT NULL.
If you're trying to INSERT 'NULL' string, then just check if fieldThree is varchar type.
Insert INTO tblTest (fieldOne,FieldTwo) VALUES ('valueOne','valueTwo').