How to Convert Date to String in SQL

Save

Structured Query Language (SQL) provides you with the tools to update, delete, select and edit your table's data. SQL is similar to other programming languages. SQL has internal functions that allow you to manipulate and calculate data and execute them on your SQL server. One such internal function is the "Convert" statement. The "Convert" function changes the data type for a variable such as the conversion from a date to a string.

  • Create SQL variables to hold your values. The first variable contains your date. After the date is converted, you need to assign it to a string variable. The following code shows you how to define variables in SQL:

    declare @date datetime
    declare @string varchar(30)

  • Assign a date value to your datetime variable. The following code assigns a date in SQL:

    set @date = '5/7/2010'

  • Convert the date to a string and assign it to the string variable created in step one. The following code shows you how to convert a date to a string:

    set @string = convert(varchar(30), @date)

    The first parameter in the "convert" function is the "varchar(30)" value. "Varchar(30)" is a string in SQL. The "30" value indicates that the string can contain no more than 30 characters. The second parameter is the variable that is converted.

  • Press the "F5" key to execute the statement. You can also use the following code to print the results to your SQL console, which verifies the conversion's success"

    print @string

References

  • Photo Credit Calendar image by Sergey Yakovenko from Fotolia.com
Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!