Selecting from a column with Ampersand(&) - sql-server

The data type of the column is nvarchar. I can insert to the column properly using parameters. I see the ampersand character in the column when I view the table in sql server management studio.
My problem is when i'm selecting from the column in my application the ampersand character disappears.
Once I select the column I assign the value to a label like, label1.Text = reader("column")
The column in my table has a value of "Foo&Bar" but in my application it just shows as "FooBar".

Looks like the ampersand was turning the next letter to a shortcut.
I just did a replace on the ampersand to 2 ampersands so it escapes.
label1.Text = reader("column").ToString().Replace("&", "&&")

It is a property of the Label called UseMnemonic, set it to False to show the text & properly

Related

VBA empty string for insert into SQL Server

Here is the situation:
There's a SQL Server database with a Microsoft Access front end. There is a table in the SQL Server database with a column called PackID which is defined as VARCHAR(6) NOT NULL (it has to be NOT NULL because it's part of a composite primary key for this table). Empty strings are legitimate in the PackID column. In fact, they occur quite often. When the user enters data in the UI for this table, and tabs through the PackID field, an error message appears: "Cannot insert the value NULL into column 'PackID'; column does not allow nulls. INSERT fails."
Things that have been tried that do not work:
adding a default constraint on this column in the SQL Server database with a value of '' - apparently the default is only used if the column is omitted from the INSERT statement
setting the Default Value of the PackID field in Access to "" - it behaves as though "" is a NULL
calling the following VBA code when user moves off row in UI (Lost Focus event)
If IsNull(PackID.Value) Then
PackID.Value = ""
End If
Does anyone know how to force an empty string in Access so it's interpreted as an empty string for SQL Server and not a NULL?
Setting the Default Value in the text box Properties for PackID in Access to this
=" "
worked. The space between the double quotes is very important. Leaving the space out causes the insert to fail. In SQL Server LEN(PackID) returns 0. For instance:
SELECT LEN(''), LEN(' ');
both return 0. It appears as though SQL Server treats both of these as empty strings.
An alternative solution to this problem (no Default Value in text box Properties for PackID).
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(PackID.Value) Or PackID.Value = "" Then
PackID.Value = " "
End If
End Sub
This still uses the concept of passing SQL Server " " (read this as quote space quote) as the field's value. On the UI side, this is an alternative to setting a default value for the field.

Handling column names with spaces in SQL Server

I have created a SQL Query to update various columns, where the column names contain spaces. It works if I run it manually as a query:
UPDATE dbo.Survey
SET PhotoPathQ1='(null)'
WHERE "Q1 Photo Taken"='0'
UPDATE dbo.Survey
SET PhotoPathQ2='(null)'
WHERE "Q2 Photo Taken"='0'
UPDATE dbo.Survey
SET PhotoPathQ3='(null)'
WHERE "Q3 Photo Taken"='0'
... and further similar updates
However if I try to automate this using SQL Server Agent as a Transact-SQL script (T-SQL) it does not actually do anything to my table, the job says that it has run successfully but the data has not been updated.
Any help would be appreciated.
Am I missing something obvious with this?
Looks like an error in your syntax, try this as an example:
UPDATE dbo.Survey
SET PhotoPathQ1 = null
WHERE [Q1 Photo Taken] = 0
This assumes that the field PhotoPathQ1 is nullable and you actually want to insert a true null value in to it rather than a string '(null)'.
It also assumes that [Q1 Photo Taken] is a bit or int field, although SQL Server will handle the conversion happily if you have it in quotes. If it's a string data type, then you should leave the quotes there.
You should use square brackets on field names that contain spaces instead of double quotes:
[Q1 Photo Taken]

How to set a function as a default value in SQL Server?

I am in trouble please help.
After searching for my question I decided to post here.
Look at my code below and tell me if I can set default value as a function.
What I want to do is to show first three characters of my first column's data which is Product_Name.
For example my first column has a name Xperia and and I want its first three characters to be shown in the last column which I have created using alter code.
I hope you understand what I say.
Xperia ----> Xpe
Code is :
alter table dummy
add new_col varchar(250) default substring(first_column, 1, 3)
returns an error:
Msg 128, Level 15, State 1, Line 2
The name "first_column" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
However I can set getdate() function as a default value.
Thanks
Why are you trying to alter the table? It sounds like you need a VIEW.
A view can add an additional field, selecting only the first three characters, and would not need to be updated each time you UPDATE or INSERT data. For example:
CREATE VIEW Table_With_Truncated_Product_Name
AS
SELECT *, LEFT(Product_Name, 3) AS truncated_product_name
FROM Your_Table
However, if you don't want to use a VIEW it may be possible to use a Computed Column.
In which case you would alter your table and add the field:
ALTER TABLE Your_Table ADD truncated_product_name AS LEFT(Product_Name, 3)
Although I don't have access to SQL Server at the moment to check that would work.

How to enter 'NULL' into SSMS cell?

In SQL Server Management Studio 2012, I was typing/pasting data into a table (via Edit Top 200 Rows).
Whenever I typed/pasted NULL in a cell, a NULL value was inserted. Apparently it thought I meant the NULL value instead of the 'NULL' text. Which didn't work, as my column wasn't nullable...
Now how do I enter the 'NULL' text into a cell?
If I wanted to insert the NULL value, I would have pressed Ctrl+0...
Just press CTRL + 0
(why should the answer body longer than 30 cars?)
Just enter 'NULL' (with single quote mark) into cell, SSMS will trim leading and ending single quote mark and save it as string 'NULL'.
Without single quote mark, SSMS will treat input as NULL.

Assigning a SQL NULL to a SSIS string - A SSIS Flaw?

I fetch a result set with an execute SQL task. It has only one column NullTime varchar. It has three rows, first one is NULL. I want to simply iterate and display the value of these rows. If I do it only by C# script, then there is no problem. The NULL is displayed as a blank. The same thing can also be done with a foreach loop.
How to do it with foreach - use that loop to read each row and set the value of each row to SSIS string User::STR_WORD. Then, simply display User::STR_WORD with a C# script task.
In the pure C# method, I can even assign the blank value (actually a NULL) to the SSIS string. But with foreach loop method, I get an error because of the NULL value.
The error is -
Error: The type of the value being assigned to variable "User::STR_WORD" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
How do I fix this error ? Is script the only alternative to what seems to be a flawed for
loop ?
A workaround for this is the Coalesce function which will convert NULLs to the value you specify.
SELECT
COALESCE([YourColumn], 'EnterValueHere') AS [YourColumn]
FROM YourTable
So, this will replace a null with the value you want to use instead. It will
prevent the weird FOR loop from suddenly crashing.
Create a temporary table to see this working -
create table ##tester
(names varchar(25))
insert into ##tester
values(Null)
insert into ##tester
values('Not a null value')
select names from ##tester
select coalesce(names, 'Null has been obliterated!') from ##tester

Resources