Hi,
Below is a a very few basic SQL server questions related to datetime in sql
1.how do you get todays date in sql?
2.if you need to get yesterdays date how will you do in sql?
3.by default sql date time datatype stores values in YY:MM:DD: hh:mm:ss:mss
but if you want to display a date in MM/DD/YY how will you do it?
4.what does a datepart function do?
You can get all the answers from sql server books online.
anyway, the answers to the above are
1. GetDate()
2.
USE pubs
GO
DECLARE @altstartdate datetime
SET @altstartdate = GetDate()
SELECT @altstartdate - 1 AS 'Subtract Date'
3. In the front end if you want to display the date in MM/DD/YYYY format: all you have to do is: DateTime.Now.ToString("yyyy-MM-dd")
In the query if you need to display a date in mm/dd/yy you need to convert it.
Use pubs
go
SELECT CONVERT(char(12), GETDATE(), 1)
Char(12) indicates that you need the date to be displayed as a char datatype.
GetDate() gets the current date.
the third parameter is the style. To display the date in mm/dd/yy format it is 1.
to display the full year you need to say 101..
there is a whole list, just search for date in sqlserver books online.
4. DatePart splits the date to display how you want it.
DATEPART ( datepart , date ) is the syntax.
you specify Datepart(yy,Getdate()) it will give you the yy portion of current date.
Happy Coding :)
Thursday, February 15, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment