I am currently using Char to store my data as alphabet. Now i need to change it to store both Numbers and Alphabets
For Example;
EmployeeID:
1.1234
2.JHOO
CodePudding user response:
In this particular case you should use a special datatype called sql_variant.
If you stores char such a datatype the internal storage for this value will be a varchar or nvarchar.
If you stores a number the internal type will be a number or an integer...
Example :
CREATE TABLE T (C sql_variant)
INSERT INTO T VALUES (123);
INSERT INTO T VALUES ('hello');
SELECT *, CASE
WHEN TRY_CAST(C AS bigint) IS NOT NULL THEN 'INTEGER'
WHEN TRY_CAST(C AS NVARCHAR(max)) IS NOT NULL THEN 'STRING'
END AS DATATYPE
FROM T;
CodePudding user response:
CHAR
will store any characters in the encoding type you have set on the server.
This will normally include all ASCII letters and numbers, plus some more.
For Microsoft SQL server, documentation is here
https://docs.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql
https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql
The relevant parts of documentation for MySQL are