I need to add specific date ('0001-01-01') into Default Value or Binding for date data type in sql 2008 server. So, when I adding the new record I'd like to sql set 0001-01-01 date automatically without setting this value inside sql statement from aspx script. Any help will be appreciated.
ALTER TABLE your_table
ADD CONSTRAINT [date_def_constraint] DEFAULT ('0001-01-01') FOR [your_column]
Related
I have migrated a Sybase database to SQL server 2008.
The main application that using the database trying to set some of dateTime2 column with data like 1986-12-24 16:56:57:81000 which is giving this error:
Conversion failed when converting date and/or time from character string.
Running the same query using dot(.) instead of colon(:) as millisecond separator like 1986-12-24 16:56:57.81000 or limiting the milliseconds to 3 digits like 1986-12-24 16:56:57:810 will solve the problem.
NOTE:
1- I don't have access to the source of application to fix this issue and there are lots of table with the same problem.
2. Application connect to database using ODBC connection.
Is there any fast forwarding solution or should i write lots of triggers on all tables to fix it using the above solutions?
Thanks in advance
AS Gordon Linoff said
A trigger on the current table is not going to help because the type
conversion happens before the trigger is called. Think of how the
trigger works: the data is available in a "protorow".
But There is a simple answer!
Using SQL Server Native Client Connection instead of basic SQL Server ODBC connection handle everything.
Note:
1. As i used SQL Server 2008 version 10 of SQL server native client works fine but not the version 11 (it's for SQL Server 2012).
2. Use Regional Settings make some other conversion problem so don't use it if you don't need it.
Select REPLACE(getdate(), ':', '.')
But it will Give String Formate to datetime Which is not covert into DateTime formate
Why would you need triggers? You can use update to change the last ':' to '.':
update t
set col = stuff(col, 20, 1, '.');
You also mistakenly describe the column as datetime2. That uses an internal date/time format. Your column is clearly a string.
EDIT:
I think I misinterpreted the question (assuming the data is already in a table). Bring the data into staging tables and do the conversion in another step.
A trigger on the current table is not going to help because the type conversion happens before the trigger is called. Think of how the trigger works: the data is available in a "protorow".
You could get a trigger to work by creating views and building a trigger on a view, but that is even worse. Perhaps the simplest solution would be:
Change the name and data type of the column so it contains a string.
Add a computed column that converts the value to datetime2.
I have in a table a nullubule timestamp that tracks when the entry got called from a client. Sometimes something goes wrong on the client side and I need to set the timestamp back to null. I tried directly in SQL management studio to execute the query:
USE [MyDB]
GO
UPDATE [dbo].[MyTable]
SET [MyTimestamp]=null
WHERE ID=SomeInt;
I get the message that one row got altered but when I refresh my select * on the table there is no change on the timestamp.
PS: The whole DB runs on an azure server but I can also not get it to work on my test DB on local host in SQL Server 2014.
Would be grateful for input
The answer is you cannot change the timestamp column to NULL. It is like a row version number.
Also
The timestamp data type is just an incrementing number and does not
preserve a date or a time.
There are some workarounds which you can use as the one which is used here in the related thread but now Timestamp datatype is rarely used.
I have a database in SQL Server 2008. I am unable to set Identity specification for a column CartId as shown in figure
I deleted the column and tried creating a new column and set isIdentity property but it's not working, also I deleted the table and tried creating a new table (not using query) but not able to set isIdentity property
How to fix this issue??
Data Type of column must be set to INTEGER or other numeric type.
identity property available only on numeric columns
I'm using SQL Server 2008 R2 and I can't seem to be able to use the geography column type it's meant to have. It doesn't show up in the data type column when I create a new column and when I run this code:
CREATE TABLE [Core].[Address2](
[Geo_Id] [geography] NULL);
I get
Column, parameter, or variable #2: Cannot find data type geography.
Am I doing something wrong or do I need to enable the new data types somehow?
At the risk of stating the obvious, I'd double check to make sure the database you're using is indeed on a SQL 2008 instance.
I have found that when you add a geography column you need to restart the Management Studio in order for it to appear in intellisense. This might be the issue you are having perhaps?
i have some experience with MySQL but none with SQL Server, i have a computer who host a database (SQL Server 2000) and i have local access to it. The think is that one column of the table NomAntiguedad need to be change from a given date period; I need to multiply by 0.01 all the values of the column between the values of mesano (05/2007 to actual date, format: MM/YYYY).
How can i have VISUAL access to the data base, like when i administrate my data bases on PhpMyAdmin in order to see the tables format?
What would be the syntax of the command to do what i need?
How can i have VISUAL access to the data base, like when i administrate my data bases on PhpMyAdmin in order to see the tables format?
There's the defacto SQL Server Management Studio (Express Edition is free), or Toad for SQL Server (free if <5 people use it in-house) applications...
The think is that one column of the table NomAntiguedad need to be change from a given date period; I need to multipli for 0.01 all the values of the column between the values of mesano (05/2007 to actual date, format: MM/YYYY).
You need to use an UPDATE statement:
UPDATE NomAntiguedad
SET your_column = .01 * your_column
WHERE mesano = '05/2007'
Mind that you might have to use CAST/CONVERT to explicitly handle the data type returned from the ".01 * your_column" calculation because you could be loosing precision depending on the column's data type.
UPDATE NomAntiguedad
SET <Column You're Updating> = .01*<Column You're Updating>
WHERE REPLACE(mesano, '/', '/01/') BETWEEN '5/1/2007' AND getDate()
I'm assuming you're date format is accurate and never changes.