473,786 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datatype-convertion in TSQL

Dear all,

Tables: COMPANY: COM_ID, COM_NAME, .....
PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID , PRP_DEFAULT_VAL UE
( nvarchar)
COMPANY_PROPERT Y: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE
(nvarchar)

Use: Without adding new field the user can add new properties to the
companies just by adding a new property in table PROPERTY and mapping the
new property to a companie. CPRP_VALUE contains all kind of datatypes but is
stored as text.

Problem: when I query the database ( SP, views, etc) I have problems with
floats and date bacause in the interface ( Access2000.adp) :
the float-format is 0,11 and in TSQL is 0.11
the date-format is DD/MM/YYYY and in TSQL it is YYYY-MM-DD


Can I convert the data within the Stored Procedure for selecting and sorting
on those fields.

for example: How to sort on CPRP_VALUE containing floatvalues stored as
nvarchar ??

The client wants to stay with the comma-format because it is common-used
here.
The format can be TSQL-format in the resultset of a Stored Procedure without
changing the format in the database because this is used for the output to
the clients.
Same problem with dates !!!


thanks,

Filip

Jun 20 '06 #1
7 9386
Filips Benoit (be***********@ pandora.be) writes:
Tables: COMPANY: COM_ID, COM_NAME, .....
PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID , PRP_DEFAULT_VAL UE
( nvarchar)
COMPANY_PROPERT Y: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE
(nvarchar)

Use: Without adding new field the user can add new properties to the
companies just by adding a new property in table PROPERTY and mapping
the new property to a companie. CPRP_VALUE contains all kind of
datatypes but is stored as text.

Problem: when I query the database ( SP, views, etc) I have problems with
floats and date bacause in the interface ( Access2000.adp) :
the float-format is 0,11 and in TSQL is 0.11
the date-format is DD/MM/YYYY and in TSQL it is YYYY-MM-DD
Can I convert the data within the Stored Procedure for selecting and
sorting on those fields.

for example: How to sort on CPRP_VALUE containing floatvalues stored as
nvarchar ??


convert(float, replace(CPRP_VA LUE, ',', '.')

For datetime conversion, look at the topic for CAST and CONVERT in
Books Online. You need to use one of the format codes, but I don't
know them by heart.
The client wants to stay with the comma-format because it is common-used
here.


An alternative would be to use the data type sql_variant for CPRP_VALUE.
In that case, all values are stored in native format.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 20 '06 #2
Thanks,

Float-convertions is OK

Can't find a code for datetime-conversion from nvarchar format DD/MM/YYYY
http://doc.ddart.net/mssql/sql70/ca-co_1.htm

What is the max lenght of sql_variant? Now CPRP_VALUE = nvarchar 1024
Is it possible to change the field-datatype having existing data?

Filips

Sorry, I first repied to you iso to the group.

"Erland Sommarskog" <es****@sommars kog.se> wrote in message
news:Xn******** *************@1 27.0.0.1...
Filips Benoit (be***********@ pandora.be) writes:
Tables: COMPANY: COM_ID, COM_NAME, .....
PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID , PRP_DEFAULT_VAL UE ( nvarchar)
COMPANY_PROPERT Y: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE
(nvarchar)

Use: Without adding new field the user can add new properties to the
companies just by adding a new property in table PROPERTY and mapping
the new property to a companie. CPRP_VALUE contains all kind of
datatypes but is stored as text.

Problem: when I query the database ( SP, views, etc) I have problems with floats and date bacause in the interface ( Access2000.adp) :
the float-format is 0,11 and in TSQL is 0.11
the date-format is DD/MM/YYYY and in TSQL it is YYYY-MM-DD


Can I convert the data within the Stored Procedure for selecting and
sorting on those fields.

for example: How to sort on CPRP_VALUE containing floatvalues stored as
nvarchar ??


convert(float, replace(CPRP_VA LUE, ',', '.')

For datetime conversion, look at the topic for CAST and CONVERT in
Books Online. You need to use one of the format codes, but I don't
know them by heart.
The client wants to stay with the comma-format because it is common-used
here.


An alternative would be to use the data type sql_variant for CPRP_VALUE.
In that case, all values are stored in native format.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Jun 21 '06 #3
labrys
1 New Member
I think the 5th one at the bottom of this post might help.
But I usually find it easier to convert date and stuff at the software end? .. I don't care what format the date is when using asp.. when i do db updates i enter it in as YYYY-MM-DD [ year(now())&"-"&month(now())& "-"&day(now() ) ] (the db will then convert it itself to whatever it uses) and when reading back out i convert it again to the format i want [ day( rs.Fields.Item( "theDATE").Valu e )&"/"&month( rs.Fields.Item( "theDATE").Valu e )&"/"&year( rs.Fields.Item( "theDATE").Valu e ) ]

or you could look up the following command in google to find the right end number for the conversion for you

CONVERT(DATETIM E,'02-28-04', 110)
The 110 denotes the format as mm-dd-yy

or look at this site http://www.databasejournal.com/featu...le.php/2197931



CONVERT ( data_type [ ( length) ] , expression [ , style ] )

PRINT '1) HERE IS MON DD YYYY HH:MIAM (OR PM) FORMAT ==>' +
CONVERT(CHAR(19 ),GETDATE())
PRINT '2) HERE IS MM-DD-YY FORMAT ==>' +
CONVERT(CHAR(8) ,GETDATE(),10)
PRINT '3) HERE IS MM-DD-YYYY FORMAT ==>' +
CONVERT(CHAR(10 ),GETDATE(),110 )
PRINT '4) HERE IS DD MON YYYY FORMAT ==>' +
CONVERT(CHAR(11 ),GETDATE(),106 )
PRINT '5) HERE IS DD MON YY FORMAT ==>' +
CONVERT(CHAR(9) ,GETDATE(),6)
PRINT '6) HERE IS DD MON YYYY HH:MM:SS:MMM(24 H) FORMAT ==>' +
CONVERT(CHAR(24 ),GETDATE(),113 )

Here is the output from the above script:

1) HERE IS MON DD YYYY HH:MIAM (OR PM) FORMAT ==>Feb 5 2003 5:54AM
2) HERE IS MM-DD-YY FORMAT ==>02-05-03
3) HERE IS MM-DD-YYYY FORMAT ==>02-05-2003
4) HERE IS DD MON YYYY FORMAT ==>05 Feb 2003
5) HERE IS DD MON YY FORMAT ==>05 Feb 03
6) HERE IS DD MON YYYY HH:MM:SS:MMM(24 H) FORMAT ==>05 Feb 2003 05:54:39:567
Jun 21 '06 #4
>>Can't find a code for datetime-conversion from nvarchar format DD/MM/YYYY

Do the formation at client application or read about Convert function
in sql server help file

Madhivanan
Filips Benoit wrote:
Thanks,

Float-convertions is OK

Can't find a code for datetime-conversion from nvarchar format DD/MM/YYYY
http://doc.ddart.net/mssql/sql70/ca-co_1.htm

What is the max lenght of sql_variant? Now CPRP_VALUE = nvarchar 1024
Is it possible to change the field-datatype having existing data?

Filips

Sorry, I first repied to you iso to the group.

"Erland Sommarskog" <es****@sommars kog.se> wrote in message
news:Xn******** *************@1 27.0.0.1...
Filips Benoit (be***********@ pandora.be) writes:
Tables: COMPANY: COM_ID, COM_NAME, .....
PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID , PRP_DEFAULT_VAL UE ( nvarchar)
COMPANY_PROPERT Y: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE
(nvarchar)

Use: Without adding new field the user can add new properties to the
companies just by adding a new property in table PROPERTY and mapping
the new property to a companie. CPRP_VALUE contains all kind of
datatypes but is stored as text.

Problem: when I query the database ( SP, views, etc) I have problems with floats and date bacause in the interface ( Access2000.adp) :
> the float-format is 0,11 and in TSQL is 0.11
>the date-format is DD/MM/YYYY and in TSQL it is YYYY-MM-DD

Can I convert the data within the Stored Procedure for selecting and
sorting on those fields.

for example: How to sort on CPRP_VALUE containing floatvalues stored as
nvarchar ??


convert(float, replace(CPRP_VA LUE, ',', '.')

For datetime conversion, look at the topic for CAST and CONVERT in
Books Online. You need to use one of the format codes, but I don't
know them by heart.
The client wants to stay with the comma-format because it is common-used
here.


An alternative would be to use the data type sql_variant for CPRP_VALUE.
In that case, all values are stored in native format.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Jun 21 '06 #5
Filips Benoit (be***********@ pandora.be) writes:
Float-convertions is OK

Can't find a code for datetime-conversion from nvarchar format DD/MM/YYYY
http://doc.ddart.net/mssql/sql70/ca-co_1.htm
It's there on that page. You need to read the table headers a little more
closely.
What is the max lenght of sql_variant?
8000 bytes.
Now CPRP_VALUE = nvarchar 1024 Is it possible to change the
field-datatype having existing data?


One way or another it is. I would guess that ALTER TABLE ALTER COLUMN works,
but I have not actually tried it. But obviously if you do that, all
data will be converted with a basetype of nvarchar, so you would have to
write some script to repopulate the column.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 21 '06 #6
Thank for following me,

I'm still not done with that date-convertion-problem

I want to be able to run the SP below but it triggers error = 'Aritmitic
overflow error converting expression to data type datetime'

What am i missing.

Thanks²,

Filip

Alter Procedure StoredProcedure 1

-- dates in CPROP_VALUE ( nvarchar) as '1/1/2000' or'01/01/200' or
'1/06/2000' tested in access with function IsDate()

As

SELECT CPROP_PRP_ID,

CPROP_VALUE
FROM dbo.COMPANY_PRO PERTY

where CPROP_PRP_ID = 370 and

convert(datetim e, CPROP_VALUE, 103 ) = getdate()

"Erland Sommarskog" <es****@sommars kog.se> wrote in message
news:Xn******** *************@1 27.0.0.1...
Filips Benoit (be***********@ pandora.be) writes:
Float-convertions is OK

Can't find a code for datetime-conversion from nvarchar format DD/MM/YYYY http://doc.ddart.net/mssql/sql70/ca-co_1.htm
It's there on that page. You need to read the table headers a little more
closely.
What is the max lenght of sql_variant?


8000 bytes.
Now CPRP_VALUE = nvarchar 1024 Is it possible to change the
field-datatype having existing data?


One way or another it is. I would guess that ALTER TABLE ALTER COLUMN

works, but I have not actually tried it. But obviously if you do that, all
data will be converted with a basetype of nvarchar, so you would have to
write some script to repopulate the column.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Jun 21 '06 #7
Filips Benoit (be***********@ pandora.be) writes:
-- dates in CPROP_VALUE ( nvarchar) as '1/1/2000' or'01/01/200' or
'1/06/2000' tested in access with function IsDate()

As

SELECT CPROP_PRP_ID,
CPROP_VALUE
FROM dbo.COMPANY_PRO PERTY
where CPROP_PRP_ID = 370 and
convert(datetim e, CPROP_VALUE, 103 ) = getdate()


As I understood it, CPROP_VALUE can include all sorts of values, including
stringified floats. Not all these values will convert to datetime,
and will give you an error of some sort.

Yes, I can guess that if CPROP_PRP_ID = 370, then all values are good
date strings, but there is no operator shortcutting in T-SQL. The
only way to avoid undesired conversion attempts is to use CASE:

WHERE CASE CPROP_PRP_ID
WHEN 370 THEN convert(datetim e, CPROP_VALUE, 103 )
END = getdate()
AND CPROP_PRP_ID = 370

The last condition is logically redudant, but if you have an index on
CPROP_PRP_ID, it's a good thing to add it.

Note the above query is not extremely meaningful. getdate() returns
the current date and time with a precisions on 3.33 ms. The odds that
the SELECT will return any rows appear slim.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
13454
by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as strings) at least 12 digits (characters) long - all positions will be occupied (no leading 0's) - Tables may have upto 1m+ rows
1
4379
by: thepercival | last post by:
Hello, I have a stored procedure and the return data type is number(16) as you can see. but I get it back in the code as a var_numeric and then the precision depends on the value of the returndata. So 0 fits into a short so I have to convert to a short, although I say that my stored procedure has to return a NUMBER(16), thus int. Can anyone tell me how I can determine my return datatype beforehand?
8
1952
by: Eternally | last post by:
Hi folks, I've got a program which has a function which uses templates to accept parameters of any type. Works well, but there's one certain datatype which I want to special case and do an extra thing to. The datatype is a class I made. Is there anyway for me to test a parameters datatype in a template using function?
0
1688
by: SoYouKnowBrig | last post by:
Hi All, I am using Microsoft.ApplicationBlocks.Cache.CacheManager to persist a System.Data.Dataset object. This Dataset object has a DataTable that is created from an existing DataTable using the Clone() method. Before I add the new DataTable to the DataSet, I change the DataType of a DataColumn from System.String to System.Int64. I then add data to the new table and then add it to the DataSet. Then DataSet is then added to the Cache....
10
4030
by: andrewcw | last post by:
I read in a earlier post that I can get the column of a grid to sort by datetime if the column type was set as Date I deserialize my XML and one attribute of the XSD has type as dateTime but upon inspection and behavior in the gri the data type is string. If I preview the dataset with the GUI in VISSTUDIO it shows my datatype to be dateTime .. If I try to rest the datatype I get this erro Additional information: Cannot change DataType...
3
3343
by: Sri | last post by:
In VB, to know the field type of a column stored in a recordset the command I use is If rsQuery.Fields(k).Type = adCurrency Then How will I achieve the same in ASP.net. I could not find a currency data type in asp.net and the type is idenfied as decimal. I have two fields defined in Sql-Server, one is money and other is decimal. In asp.net both are identified as decimal.
1
1182
by: Bryan | last post by:
I have a class called "Prop". I want that class to have a property called "DataType" where the user can select and store a datatype. How can I store a DataType value in a class property. how the code would work in my mind: dim TempProp as new Prop TempProp.DataType = String
4
11968
by: Orchid | last post by:
How can I change a Date datatype to a Number datatype? For example, I want a date 10/31/2006 to show 1031 as Number datatype. But I don't want it becomes 39021. What formula should I use? Thanks for your help!!
0
1220
by: graju80 | last post by:
I am kind of new to Db2... Question: What are the rules that DB2 uses to determine the right datatype for a particular column for on-the-fly SQL generation? For example...
11
1891
by: BD | last post by:
Hi, all. I'm running 8.2 on Windows. This is a development platform for a project whose production environment is running on a mainframe. I believe that the RI compilation process is not quite as robust on Windows as it is in other environments. Here's what I'm finding:
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.