Guru Ji
asked on
Default Date SQL Server 2008
I want the SQL Server default date to be 0000-00-00, as for some records I have following situations and not sure how can I tackle the default date for every record insert
1. Some records no date at all but I don't want to leave datetime field empty
2. Some records only year and month available
3. Some records only year available.
How can I default the missing information to just 0000 or 00 automatically whenever there is a records inserted.
Thanks
1. Some records no date at all but I don't want to leave datetime field empty
2. Some records only year and month available
3. Some records only year available.
How can I default the missing information to just 0000 or 00 automatically whenever there is a records inserted.
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just adding to my previous solution .. If you insert date..it will look like below -
INSERT INTO tstDefault(ID,dateCol) VALUES (2,GETDATE())
SELECT * FROM tstDefault
/*------------------------
SELECT * FROM tstDefault
------------------------*/
ID dateCol
----------- -----------------------
1 1900-01-01 00:00:00.000
2 2017-10-31 00:18:41.103
(2 row(s) affected)
If you have existing column just use below -
ALTER TABLE tstDefault ADD CONSTRAINT DF_DateDeafult DEFAULT '1900/01/01' FOR datecolumn;
ALTER TABLE tstDefault ADD CONSTRAINT DF_DateDeafult DEFAULT '1900/01/01' FOR datecolumn;
@Pawan, you really should know that: Use an unambiguous date format for literals like 19000101, which is every time correctly interpreted and not depended on the systems DATEFORMAT.
ASKER
How can I insert only year and month only when there is no day information available in datetime field
DATEFROMPARTS
You can create date using datefromparts.. If the day is not given then you can provide first day of the month like below ( 01 ) as day.
You can create date using datefromparts.. If the day is not given then you can provide first day of the month like below ( 01 ) as day.
/*------------------------
SELECT DATEFROMPARTS('2016','01','01')
------------------------*/
----------
2016-01-01
(1 row(s) affected)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry , I don't get datefromparts ?
Currently the field type available is date or datetime.
If I choose either or and try to enter just YYYY or YYYY-MM it gives me error.
It forces me to enter YYYY-MM-DD format only.
Currently the field type available is date or datetime.
If I choose either or and try to enter just YYYY or YYYY-MM it gives me error.
It forces me to enter YYYY-MM-DD format only.
Only year and month cannot be inserted in datetime column so we have to convert it to a date. Thats why in these cases we can use DateFromParts.
Or there are other methods also to create a date from year and month but for that we have to add day . without that it would not be possible to create a date.
Or there are other methods also to create a date from year and month but for that we have to add day . without that it would not be possible to create a date.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Vitor-
if you use that you will get below error
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Casting is required in this case.
This will work...
SELECT Id,ISNULL(CAST(dateCol AS VARCHAR),'0000-00-00') FROM tstDefault11
if you use that you will get below error
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Casting is required in this case.
This will work...
SELECT Id,ISNULL(CAST(dateCol AS VARCHAR),'0000-00-00') FROM tstDefault11
Vitor - I agree with you if the road you're going down (I'm guessing this is correct) is to store the absence of a value as NULL in the database, and let the presentation layer apply whatever business logic is needed to present/do whatever it needs to do..
Pawam, that was more to show the author that he don't need to store the value as he wants. If I wanted to be more precise I wouldn't use CAST but CONVERT or FORMAT functions to return the date in the YYYY-MM-DD format.
Jim, yes, that's the idea. NULL is absence of value and the author can workout what to present when found a NULL value.
Jim, yes, that's the idea. NULL is absence of value and the author can workout what to present when found a NULL value.
Vitor - Its Pawan not Pawam
Apologizes, Pawan.
:)
Btw, we are assuming that author is storing the dates in a proper date datatype column but if he's storing it in a varchar column then no conversion needed.
Went to read back the question and the 2nd and 3rd points are really scary if the idea is to use a single field to store it:
Went to read back the question and the 2nd and 3rd points are really scary if the idea is to use a single field to store it:
1. Some records no date at all but I don't want to leave datetime field empty
2. Some records only year and month available
3. Some records only year available.
2. Some records only year and month available
3. Some records only year available.
If some records do have incomplete date then you can store such values in char columns only. Thus the default 0000-00-00 is easily achievable.
Of course, incomplete date requires special processing in the code.
Of course, incomplete date requires special processing in the code.
ASKER
Thank you everyone for your valuable input.
Open in new window
OUTPUTOpen in new window