September 21, 2007 at 1:49 pm
Is there a function that would return the data type of a field?
For example:
select get_datatype(myfield) from my table
September 21, 2007 at 2:12 pm
Just turn this into a sproc, and use in the desired DB. This will work in Northwind
USE Northwind
GO
DECLARE @Table varchar(35),
@Column varchar(35)
SELECT @Table = 'Employees',
@Column = 'EmployeeID'
SELECT type_name(st.xtype)
FROM systypes st
JOIN syscolumns sc
ON st.xtype = sc.xtype
JOIN sysobjects so
ON sc.id = so.id
WHERE so.name = @Table
AND sc.name = @Column
Although - just using sp_help [Table] would be faster even though it gives you more info than you may want.
September 21, 2007 at 2:25 pm
Thanks for the tip, but not exactly what I was looking for.
What about xp's? Or If I say select @@servername? I know that is a nvarchar...but is there an easy way to find this aside from docs?
I'm wondering how 2005 does it in SSIS. It is able to alert you to problems with your data types during design time.
September 21, 2007 at 2:34 pm
What do you mean by xp'x and @@severname? I thought you were looking for data types of specific columns. What is "docs?" ????
September 21, 2007 at 4:01 pm
Not exactly - a little more general. I said a field in a select statement, not a column in a table - I guess I could have said 'item'. Another example of what I'm looking for.
Select get_dataType(getdate()) as my_dataType;
my_dataType
------------------
DateTime
>>What do you mean by xp'x and @@severname? I thought you were looking for data types of specific columns. What is "docs?" ????
Try "Select @@servername" - it's a global variable. Docs are documentation.
September 21, 2007 at 4:46 pm
Based on what I have read, I think the answer to your question about there being a function that will return the datatype of an input; a table column in a select statement, or a system variable, etc., is no.
September 21, 2007 at 5:17 pm
No, pretty sure there's nothing built in... you'd have to do a SELECT INTO a temp table and then read the datatype from the columns something like this...
IF OBJECT_ID('TempDB..#MyHead') IS NOT NULL
DROP TABLE #MyHead
SELECT TOP 1
GETDATE() AS ColA,
NEWID() AS ColB,
@@SERVERNAME AS ColC,
au.*
INTO #MyHead
FROM Pubs.dbo.Authors au --Just an example table...
SELECT sc.Name,TYPE_NAME(sc.XType)
FROM TempDB.dbo.SysColumns sc
WHERE sc.ID = OBJECT_ID('TempDB.dbo.#MyHead')
But, normally you know what the datatype of "fields" in the SELECT list are at design time... so, I'm curious... why would you need such a function? In other words, what are you trying to do (other than the obvious)? Someone might already have it
--Jeff Moden
Change is inevitable... Change for the better is not.
September 22, 2007 at 11:10 am
this is one of those situations where the client side would have more functionality than server side.
once placed in an ADODB recordset, the recordset has the datatype and defined size for every field.
data table would have similar information, but when looking at it in query analyzer, it's not obvious, but it's there behind the scenes.
Lowell
September 22, 2007 at 1:28 pm
I just thought it might come in handy, day to day, to get a quick return type instead of jumping to the manual. There is always some little trick I don't know - thought this might be one. Thanks for your help guys!:D
September 22, 2007 at 11:26 pm
I understand your point of view... a shortcut like this could be usefull. But can you give me a single production context where this comes in handy??? I'm sure there are a few exceptions... but I've never encountered them.
September 23, 2007 at 8:51 am
Ya know... I actually get this one... there's been a couple of times that I've had to lookup the datatype for SysName and @@ServerName and the like just because I forget or want to make double sure...
But, it's easy to do... I have a couple of "Favorites" selections in Books Online... DataTypes and System Functions are a couple of those...
--Jeff Moden
Change is inevitable... Change for the better is not.
September 23, 2007 at 11:00 am
While it doesn't help for this problem, it can certainly help in a similar situation : COLUMNPROPERTY() has a lot of usefull info.
September 24, 2007 at 8:21 am
Which property of COLUMN_PROPERTY do you ask for to determine the datatype of a column and how do you use it on a system function like @@ServerName?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 24, 2007 at 8:40 am
Honestly I don't understand how much faster do you get when BOL has the "return type" and is very easy to bookmark/use 😉
* Noel
September 24, 2007 at 8:52 am
As I said, while IT DOESN'T help for this case... that function has a lot of good info...
:).
Viewing 15 posts - 1 through 15 (of 25 total)
You must be logged in to reply to this topic. Login to reply