Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting between SQL char and C#

If I want to insert into a database column of type char(2) from C#. What data type would I use in C#?

If using the following code:

private static SqlParameter addParameterValue(string parameterName, ? parameterValue, SqlCommand command)
    {
        SqlParameter parameter = new SqlParameter(parameterName, SqlDbType.Char);
        parameter.Value = parameterValue;
        command.Parameters.Add(parameter);
        return parameter;
    }

What type would I give to parameterValue?

I already have method like this when the parameterValue is of type string, so this could be a problem when telling the difference between SqlDbType.Char and SqlDbType.Varchar

like image 386
ediblecode Avatar asked Nov 02 '11 10:11

ediblecode


People also ask

What is the equivalent of To_char in SQL Server?

In Oracle, TO_CHAR function converts a datetime value (DATE, TIMESTAMP data types i.e.) to a string using the specified format. In SQL Server, you can use CONVERT or CAST functions to convert a datetime value (DATETIME, DATETIME2 data types i.e.) to a string.

How do I convert one data type to another in SQL?

SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds. GETDATE() implicitly converts to date style 0. SYSDATETIME() implicitly converts to date style 21.

What is CHR 39 in SQL?

Chr(39) means single quote. If you use single quote directly in the LET statement it will not evaluate properly and that is the reason you use Chr(39).

Can we convert number to CHAR in SQL?

The following SQL statement converts integer data to characters using CAST(): SELECT item_name, CAST(item_quantity AS CHAR(8)) FROM items; As with CONVERT(), CAST() can use any data type which receives characters: VARCHAR, NCHAR and NVARCHAR.


3 Answers

char, varchar, nchar, nvarchar are actually strings

the size helps to determine how long the string is...

by the way

char has a fixed length, so if you want to have "1" in a char(2) the contents will be actual "1 "

varchar(2) will be "1"

the n part stands for unicode, so everything inside those fields will be in Unicode.


normally we use nvarchar to save some space on the data, as if you have a char(250) the database will always save the full length, as an empty varchar(250) will be nothing.

In our programming language we then use padding to do what char does, for example, in C#

"1".PadLeft(2);
"1".PadRight(2);

will output " 1" and "1 " respectively.

like image 145
balexandre Avatar answered Sep 30 '22 10:09

balexandre


string will work fine if it is 2 characters or shorter

like image 37
flipchart Avatar answered Sep 30 '22 11:09

flipchart


try using AddWithValue method and parce it as string,

it is only a one line. no need to have a seperate method.

command.Parameters.AddWithValue(parameterName, parameterValue);
like image 21
Damith Avatar answered Sep 30 '22 09:09

Damith