Two Identical queries one with SQL parameter and other with hard code string value providing different result set [closed] - where-clause

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Two identical Queries are returning different results in where clause. The first query is with SQL parameter variable and the second query with a string value as mentioned below. The result of both queries should be the same as both have the same values but it is not happening. what could be the reason for that?
Declare #messageID AS INT=720987
Declare #replyfileType AS Varchar='Audio'
select VoiceMessageReply from MDBDetails where MessageID = #messageID and FileType=#replyfileType
select VoiceMessageReply from MDBDetails where MessageID = #messageID and FileType='Audio'
Result is as :-
Query results

This declaration:
Declare #replyfileType AS Varchar = 'Audio';
Is declaring a one character string, so it is equivalent to:
Declare #replyfileType AS Varchar(1) = 'A';
Always include the length when using strings in SQL Server!
Declare #replyfileType AS Varchar(255) = 'Audio';
I should note that if you printed out #replyfileType the error would be obvious.

Related

Using emails in like statements not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Do you need to escape characters when searching for emails? I have following value in a subject column:
XX Operation <operation#xxx.com>,Info XX<info#xxx.com>
Following does not return any results:
select top 10 * from messages where subject like '%operation#xxx.com%'
But I get no results.
Your sample data seems to work:
DECLARE #A VARCHAR(MAX) = 'XX Operation operation#xxx.com,Info XXinfo#xxx.com'
SELECT IIF(#A like '%operation#xxx.com%', 1, 0); -- give 1
The . is not a wildcard character and there is no need to escape it. I will first check the collation of your database and then your column. For example, the following is not a match:
DECLARE #A VARCHAR(MAX) = 'XX Operation operation#xxx.com,Info XXinfo#xxx.com'
SELECT IIF(#A like '%Operation#xxx.com%' COLLATE Latin1_General_CS_AS , 1, 0)
The issue was caused by hidden chars.

SQL query not working for changing null values [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I have a database table with over 18,000 rows in Access 2007.
I am trying to create a primary key but I have too many null values. Therefore, I would like to replace those null values with actual values. To do so, I wrote the following SQL statement:
UPDATE [File]
SET [Date] = '01/01/1000'
WHERE [Date] = NULL;
Unfortunately, this UPDATE statement is not working (none of the null values are changing). Can anyone tell me why?
Because nothing = NULL. You need to change this to use IS NULL. You also should use the ANSI dateformat.
UPDATE [File]
SET [Date] = '1000-01-01'
WHERE [Date] IS NULL;
This date is not valid in many DBMS. In sql server this would have to be a datetime2. I don't know about mysql or Access.
You could try to replace the = with IS and see if it fixes the issue
UPDATE [File]
SET [Date] = '01/01/1000'
WHERE [Date] IS NULL;
PS: Depending on your DBMS / configuration, '01/01/1000' could be a problem

How to store only date and only time in SQL Server 2008? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to store only date and only time in the table using SQL Server 2008.
Example My try
create table dt( sl int, dateonly date, timeonly time);
insert into dt values(1,11-08-2014,'12:04:00');
Error:
Operand type clash: int is incompatible with date
Expected Result:
sl dateonly timeonly
------------------------
1 11-08-2014 12:04:00
You missed '
INSERT INTO dt VALUES (1,'11-08-2014','12:04:00');
create table dt( sl int, dateonly date, timeonly time);
insert into dt values(1,'20140811','12:04:00');
It's your date string that is in error (well it should be string but isn't)
Please avoid using dd-mm-yyyy or mm-dd-yyyy, in sql server the safest of all formats is YYYYMMDD (no delimiters)
by the way, you might regret not having datetime as one field.

LIKE not working with NVARCHAR [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a SQL query like this:
SELECT *
FROM hr_companies
WHERE NameLang2 LIKE '111კომპანია'
but it returns no rows. I see that hr_companies table has a column named NameLang2 of varchar type. I also used ltrim rtrim but still nothing is returned.
Please suggest how to fix problem I am facing now.
Try to add %% to your search:
SELECT * FROM hr_companies
WHERE NameLang2 LIKE '%111კომპანია%'
Use wildcard
SELECT * FROM hr_companies
WHERE NameLang2 LIKE '111კომპანია%'
Try like this:
declare #tabe table(id int ,data nvarchar(max))
insert into #tabe
values (1,N'नाम'),(2,N'गणेश')
select * from #tabe where data like N'%गणे%'
Output:
id data
2 गणेश
You can try this,
SELECT *
FROM hr_companies
WHERE NameLang2 LIKE N'%111კომპანია%'

SQL Stored Procedures for data access [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have been asked to make a simple SQL Person table and to ensure all data access to the SQL table will be via stored procedures.
Can I please have a quick explanation of this statement? Instead of using an Insert statement for the table, do I call a function instead?
If possible, can I have a quick example of using a stored procedure to insert standard information into a database that has the following fields:
First Name
Last Name
Three Address Lines
Mobile Phone
Home Phone
Date Modified
Person Category
Comment
If you are using MS SQL Server. You could do something like:
CREATE TABLE Person
(
FirstName VARCHAR(25),
Surname VARCHAR(25)
)
Then your stored procedure (This could be improved by using a transaction, if multiple tables needed be added etc):
CREATE PROCEDURE usp_InsertNewPerson
(
#FirstName VARCHAR(25),
#Surname VARCHAR(25)
)
AS
BEGIN
INSERT INTO Person(FirstName, Surname)
VALUES (#FirstName, #Surname)
END
To call the stored procedure:
EXEC usp_InsertNewPerson 'Darren', 'Davies'