How to use the Char function from mssql

Find comprehensive JavaScript mssql.Char code examples handpicked from public code repositorys.

mssql.Char is a class in the mssql library that represents a fixed-length character string data type in SQL Server.

120
121
122
123
124
125
126
127
128
129
table.create = false
table.columns.add('foreName', sql.NVarChar, { length: 128 })
table.columns.add('lastName', sql.NVarChar, { length: 128 })
table.columns.add('gender', sql.Char, { length: 1, nullable: false })
table.columns.add('dateOfBirth', sql.DateTimeOffset(3), { nullable: false })
table.columns.add('upn', sql.Char(13), { nullable: false })

for (let schoolIdx = 0; schoolIdx < schoolCount; schoolIdx++) {
  for (let pupilIndex = 0; pupilIndex < pupilCountPerSchool; pupilIndex++) {
    table.rows.add(schoolId, `bulk pupil ${pupilIndex + 1}`, `pupil ${pupilIndex + 1}`,
fork icon14
star icon12
watch icon11

+ 5 other calls in file

39
40
41
42
43
44
45
46
47
48

new sql.Request()
    .input('deptId', sql.Int, req.body.deptId)
    .input('Name', sql.VarChar(50), req.body.name)
    .input('userName', sql.VarChar(50), username)
    .input('type', sql.Char(1), req.body.type)
    .input('password', sql.NVarChar(sql.MAX), password)
    .execute('addUser')
    .then(result => {
        res.status(200).json({ message:"added successfully" });
fork icon2
star icon0
watch icon0

How does mssql.Char work?

mssql.Char is a class in the mssql library that represents a fixed-length character string data type in SQL Server. When creating a new mssql.Char object, you can specify the maximum length of the string as a parameter. The resulting mssql.Char object can be used as a parameter in a SQL query or stored procedure call to represent a fixed-length character string value. When the query or procedure is executed, the mssql.Char object is automatically converted to the appropriate SQL Server data type. Note that because mssql.Char is a fixed-length data type, any string value that is assigned to it must be exactly the length specified when the mssql.Char object was created. If the assigned string is shorter than the maximum length, it will be padded with spaces to fill the remaining length. If the assigned string is longer than the maximum length, it will be truncated to fit. In summary, mssql.Char is a class in the mssql library that represents a fixed-length character string data type in SQL Server. It provides a convenient way to pass fixed-length string values as parameters in SQL queries or stored procedure calls.

203
204
205
206
207
208
209
210
211
212
.input('default_currency', sql.NVarChar(3), opportunity.defaultCurrency)
.input('source', sql.NVarChar(255), opportunity.source)
.input('request_type', sql.NVarChar(255), opportunity.requestType)
.input('submission_state', sql.NVarChar(255), opportunity.submissionState)
.input('workflow_bucket', sql.NVarChar(255), opportunity.workflowBucket)
.input('parent_id', sql.Char(24), opportunity.parentId)
.input('owning_office_id', sql.Char(24), opportunity.owningOfficeId)
.input('due_at', sql.DateTime2, opportunity.dueAt)
.input('job_walk_at', sql.DateTime2, opportunity.jobWalkAt)
.input('rfis_due_at', sql.DateTime2, opportunity.rfisDueAt)
fork icon1
star icon0
watch icon0

+ 7 other calls in file

234
235
236
237
238
239
240
241
if (dbtype.name == 'VarChar') {
  if (dbtype.length == types.MAX) return mssql.NVarChar(mssql.MAX);
  return mssql.NVarChar(dbtype.length);
}
else if (dbtype.name == 'Char') {
  if (dbtype.length == types.MAX) return mssql.Char(mssql.MAX);
  return mssql.Char(dbtype.length);
}
fork icon1
star icon0
watch icon1

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const sql = require("mssql");

const config = {
  user: "myusername",
  password: "mypassword",
  server: "localhost",
  database: "mydatabase",
};

const pool = new sql.ConnectionPool(config);

const charParam = new sql.Char(10, "abcdef");

pool
  .connect()
  .then(() => {
    return pool
      .request()
      .input("myCharParam", charParam)
      .query("SELECT * FROM mytable WHERE mycolumn = @myCharParam");
  })
  .then((result) => {
    console.log(result.recordset);
  })
  .catch((err) => {
    console.error(err);
  })
  .finally(() => {
    pool.close();
  });

In this example, we first create a new mssql.Char object charParam with a maximum length of 10 and a value of 'abcdef'. This represents a fixed-length string value with a maximum length of 10 characters. We then create a new sql.ConnectionPool object and connect to the database using the pool.connect() method. We then create a new SQL query using the pool.request() method and pass the charParam as an input parameter using the .input() method. The @myCharParam placeholder in the query string represents the input parameter. We then execute the query using the .query() method and log the results to the console. Note that in this example, we're assuming that the mytable table and mycolumn column exist in the database and can be queried with the provided SQL statement. You'll need to adjust the query to match the structure of your own database.

4
5
6
7
8
9
10
11
12
const addQRCode = async (code, pumpNo, product) => {
    try {
        if(code !== undefined || '' ) {
        var serve = await dbConnection;
        let insQRCode = await serve.request()
            .input( 'code', sql.Char(6), code)
            .input( 'pumpNo', sql.Int, pumpNo)
            .input( 'Product', sql.NVarChar(50), product)
            .execute('cf_AddQRCode')
fork icon0
star icon0
watch icon1

25
26
27
28
29
30
31
32
33
34
35
module.exports.getLatestStockTrade = async stockCode => {
    try {
        let pool = await new sql.ConnectionPool(config.db).connect();


        let result = await pool.request()
            .input('stockCode', sql.Char(6), stockCode)
            .execute('sp_get_latest_stock_trade');
        if (result.returnValue === -1) {
            throw result.result;
        }
fork icon0
star icon0
watch icon0

+ 2 other calls in file

107
108
109
110
111
112
113
114
115
116
    'SELECT @numeroOrden as ID';

let result = await new sql.Request(pool)
    .input('idEfector', sql.Int, dtoOrden.idEfector)
    .input('numero', sql.Int, dtoOrden.numero)
    .input('periodo', sql.Char(10), dtoOrden.periodo)
    .input('idServicio', sql.Int, dtoOrden.idServicio)
    .input('idPaciente', sql.Int, dtoOrden.idPaciente)
    .input('idProfesional', sql.Int, dtoOrden.idProfesional)
    .input('fecha', sql.DateTime, new Date(dtoOrden.fecha))
fork icon0
star icon0
watch icon3

97
98
99
100
101
102
103
104
105
106
  break;
case 'bit':
  type = mssql.Bit();
  break;
case 'char':
  type = mssql.Char(columnProperty.CHARACTER_MAXIMUM_LENGTH);
  break;
case 'date':
  type = mssql.Date();
  break;
fork icon0
star icon0
watch icon1

+ 3 other calls in file