Technology Programming

How to Convert a Date in TSQL

Instructions

1

Connect to the default SQL Server instance using the SQLCMD utility by opening a DOS prompt and typing SQLCMD.
2

Declare a local variable, named "@mydate" of type "datetime" and hit the "Enter" key. The "@mydate" variable will hold the date to be converted. Add a return after the variable declaration. For example:

1>declare @mydate datetime
3

Retrieve the current date using the "getdate() Transact-SQL" function. Assign the current date value to the "@mydate" variable and add a return.

1>declare @mydate datetime

2>set @mydate=getdate()
4

Print "@mydate" to the screen using the print statement and type a hard return. Printing "@mydate" will display the variable's value before it is converted.

1>declare @mydate datetime

2>set @mydate=getdate()

3>print @mydate
5

Use the convert T-SQL statement to convert the value of "@mydate" to a "varchar(10)" data type. The "convert()" command takes three values: The data type to which the date should be converted, the variable to be converted and a style code. In this example, the style code "101" converts the "@mydate" variable to the data type" varchar(10)" in the format mm/dd/yy. Follow the convert statement with a hard return.

1>declare @mydate datetime

2>set @mydate=getdate()

3>print @mydate

4>select convert(varchar(10),@mydate,101)
6

Print "@mydate" to the screen using the print statement and type a hard return.

1>declare @mydate datetime

2>set @mydate=getdate()

3>print @mydate

4>select convert(varchar(10),@mydate,101)

5>print @mydate
7

Type the "GO" command and hit "Enter." The datetime value of "@mydate" will print to the screen in its original format (pre-conversion) and the "varchar(10)" value of "@mydate" will print to the screen in mm/dd/yy format (post-conversion).
SHARE
RELATED POSTS on "Technology"
WordPress - How to Set up a New Theme to WordPress 3.
WordPress - How to Set up a New Theme to WordPress 3.
Solution of Creative Web Design
Solution of Creative Web Design
The three disciplines of User Experience
The three disciplines of User Experience
Web Design Sheffield Options For Professional Enterprises
Web Design Sheffield Options For Professional Enterprises
Do you have what it takes?
Do you have what it takes?
Segway Cost
Segway Cost
Microsoft Access 2010: What's Coming with Office 2010?
Microsoft Access 2010: What's Coming with Office 2010?
Companies of Web Development in Ireland Provide Designs that Work
Companies of Web Development in Ireland Provide Designs that Work
Penguin Update to Put Red-Flags on Negative SEO
Penguin Update to Put Red-Flags on Negative SEO
Innovative web 2 design templates can make your business famous quickly
Innovative web 2 design templates can make your business famous quickly
Building A Search Engine Friendly Website
Building A Search Engine Friendly Website
Exceptional Advice To Build Up Your Internet Marketing
Exceptional Advice To Build Up Your Internet Marketing
The Benefits of Selecting The Right Hosting Company
The Benefits of Selecting The Right Hosting Company
Is There a Methodology for Making Successful Logos
Is There a Methodology for Making Successful Logos
Benefits of Ruby On Rails Development
Benefits of Ruby On Rails Development
The Power of Colour and Shapes in Your Infant's Life.
The Power of Colour and Shapes in Your Infant's Life.
Advantages of Hiring PSD To HTML Service Providers
Advantages of Hiring PSD To HTML Service Providers
How to Make Responsive Web Design Attractive?
How to Make Responsive Web Design Attractive?
Converting PSD to Responsive HTML
Converting PSD to Responsive HTML
Just a few realy really hints and tips when it comes to website design but look for.
Just a few realy really hints and tips when it comes to website design but look for.

Leave Your Reply

*