Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server check resultant data type of expression

Normally I would put the data type into a temp table and inspect the table column type, e.g.

select 1.0 N into tbl  sp_help tbl 

Column N then reveals the data type of the expression 1.0. (This is a only simple example)

There is a SQL function to inspect the data type of an expression directly, but the name escapes me right now.

What is the name of this function?

like image 746
RichardTheKiwi Avatar asked Feb 15 '11 18:02

RichardTheKiwi


People also ask

How can someone determine the type of a value in SQL?

Use TYPE_NAME() to Get the Name of a Data Type in SQL Server In SQL Server, you can use the TYPE_NAME() function to return the name of a data type, based on its ID. This can be useful when querying a system view such as sys. columns that returns the type's ID but not its name.

How do I find the data type of a field in SQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.

What is Sql_variant_property?

Returns the base data type and other information about a sql_variant value. We can Store different data like int,string,float in the single column. Mutiples data types in the single column. Helps to identify the base type of stored value.

What are expressions in SQL?

SQL expression is a combination of one or more values, operators and SQL functions that results in to a value. These SQL EXPRESSIONs are similar to a formula and they are written in query language. You can also use them to query the database for a specific set of data.


2 Answers

SQL_VARIANT_PROPERTY is close

DECLARE @what sql_variant; DECLARE @foo decimal(19,3) = 1, @bar decimal(11,7) = 2;  SELECT @what = @foo / @bar; SELECT     SQL_VARIANT_PROPERTY(@what, 'BaseType'),     SQL_VARIANT_PROPERTY(@what, 'Precision'),     SQL_VARIANT_PROPERTY(@what, 'Scale'),     SQL_VARIANT_PROPERTY(@what, 'MaxLength');  SELECT @what = @foo + @bar; SELECT     SQL_VARIANT_PROPERTY(@what, 'BaseType'),     SQL_VARIANT_PROPERTY(@what, 'Precision'),     SQL_VARIANT_PROPERTY(@what, 'Scale'),     SQL_VARIANT_PROPERTY(@what, 'MaxLength');   SELECT @what = @foo * @bar; SELECT     SQL_VARIANT_PROPERTY(@what, 'BaseType'),     SQL_VARIANT_PROPERTY(@what, 'Precision'),     SQL_VARIANT_PROPERTY(@what, 'Scale'),     SQL_VARIANT_PROPERTY(@what, 'MaxLength'); 

Or temp table/SELECT..INTO.. as an extension of what you've already done

Edit: Remus' answer?

like image 105
gbn Avatar answered Sep 21 '22 22:09

gbn


All that I can think of are the ISNUMERIC and ISDATE functions.

These will return a 1/0 when passed an expression. I can't think of anything that will return the type if given an expression unfortunately.

UPDATE:

Try SQL_VARIANT_PROPERTY! I think this is what you are looking for. Hard to track down...

like image 29
Abe Miessler Avatar answered Sep 19 '22 22:09

Abe Miessler