Friday, March 30, 2012
Inserting a 0
Im having trouble with the money data type, for instance I have a column that calculates a price but it will output the price as 470.2 instead of 470.20 which is how I want it displayed on a web page.
Anyone know how to automatically insert a zero on the end of the price?
THanksNoone knows how to insert zeros on the end of numbers??|||I'm gettin '470.2000'
from this simple query I ran from query analyser
declare @.dollar as money
set @.dollar=470.2
select @.dollar
I can't understand why your only getting 470.2. Maybe you can write your calculation query for us to figure?|||I figured it out but thanks anyhow : )sql
Monday, March 12, 2012
INSERT statement - autonumbering
Hi All,
I am having trouble with a simplet INSERT statement. I want to insert a record. The field "dbid" is the primary key and should be autonumbering. What do I need to add to my code?
Dim myconnectionAs SqlConnection myconnection =New SqlConnection() myconnection.ConnectionString = _ ConfigurationManager.ConnectionStrings("infoNoticeDBConnectionString").ConnectionStringDim strSQLAs String ="INSERT INTO users " & _"(dbid, infoid) VALUES (@.dbid, @.infoid)"Dim dbCommAs New SqlCommand(strSQL, myconnection) dbComm.Parameters.Add("infoid", SqlDbType.NVarChar, 50,"@.infoid") dbComm.Parameters.Add("dbid", SqlDbType.UniqueIdentifier,"dbid") dbComm.Parameters("infoid").Value = gv2.SelectedValueTry myconnection.Open() dbComm.ExecuteNonQuery()Catch exAs Exception Response.Write(ex.Message) Response.End()Finally If myconnection.State = ConnectionState.OpenThen myconnection.Close()End If End Try Response.Write("A new record has been added") Response.End()End SubThanks,
If that field has been configured in your database as an autonumber column, then you wouldnot include it in the insert statement.
The database will automatically populate it with the next value.
|||
Got it..Thanks. also had to make field Indentity "yes"
Thanks!
Friday, March 9, 2012
Insert small time into SQL Server 2000 table
The real problem seems to be when i try to send a time value to that column with my ASP.NET application. Because it inserts the time value and todays date. So that if i send:
12:30 PM
It will be stored as:
15/11/2003 12:30:00 PM
I only want to store the short time, not the date especially not the date that row was created on because thats useless for the purposes of what my application is trying to achieve and just creates problems down the track when selecting rows.
How can i correct this?Both DateTime and SmallDateTime always have date and time components. Both store the value as a decimal number with the whole number part being the date and the fractional part the time. If you only want the time component set the Date part to 1/1/1900.
When I ran '12:30PM' using datetime and smalldatetime it created the value as '1/1/1900 12:30 PM'. Not sure why you're getting today's date or why it complains entering a smalldatetime with '12:30 PM'
returns
Select Top 1
Cast('12:30 PM' as datetime) as DateTime1230,
Cast('12:30 PM' as smalldatetime) as SmallDateTime1230,
Cast(Cast( '12:30 PM' as datetime) as integer) as IntOfDateTime1230,
Cast(Cast( '12:30 PM' as smalldatetime) as integer) as IntOfSmallDateTime1230,
Cast(Cast( '12:30 PM' as datetime) as float) as FracOfDateTime1230,
Cast(Cast( '12:30 PM' as smalldatetime) as float) as FracOfSmallDateTime1230
From SomeTable
|||Ok no worries thats a good explanation. Thanks very much.
DateTime1230SmallDateTime1230IntOfDateTime1230IntOfSmallDateTime1230FracOfDateTime1230FracOfSmallDateTime1230
1900-01-01 12:30:00.0001900-01-01 12:30:00110.520833333333333370.52083333333333337