472,968 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,968 software developers and data experts.

Datatype-convertion in TSQL

Dear all,

Tables: COMPANY: COM_ID, COM_NAME, .....
PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE
( nvarchar)
COMPANY_PROPERTY: 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 9343
Filips Benoit (be***********@pandora.be) writes:
Tables: COMPANY: COM_ID, COM_NAME, .....
PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE
( nvarchar)
COMPANY_PROPERTY: 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_VALUE, ',', '.')

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****@sommarskog.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****@sommarskog.se> wrote in message
news:Xn*********************@127.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_VALUE ( nvarchar)
COMPANY_PROPERTY: 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_VALUE, ',', '.')

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****@sommarskog.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
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").Value )&"/"&month( rs.Fields.Item("theDATE").Value )&"/"&year( rs.Fields.Item("theDATE").Value ) ]

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

CONVERT(DATETIME,'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(24H) 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(24H) 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****@sommarskog.se> wrote in message
news:Xn*********************@127.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_VALUE ( nvarchar)
COMPANY_PROPERTY: 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_VALUE, ',', '.')

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****@sommarskog.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****@sommarskog.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 StoredProcedure1

-- 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_PROPERTY

where CPROP_PRP_ID = 370 and

convert(datetime, CPROP_VALUE, 103 ) = getdate()

"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn*********************@127.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****@sommarskog.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_PROPERTY
where CPROP_PRP_ID = 370 and
convert(datetime, 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(datetime, 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****@sommarskog.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
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...
1
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...
8
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...
0
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...
10
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...
3
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...
1
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...
4
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? ...
0
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
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.