Summary of convert functions in SQL Server

Source: Internet
Author: User

Usage:
1. varchar type -----> int
For example, if the database table has the ID (INT) and name (varchar (10) columns, you want to output the value of ID + name:

Select *, convert (varchar, ID) + name from table_3;

Or select *, convert (varchar (10), ID) + name from table_3;

2. Convert (varchar (8), getdate (), 112) indicates the output of 20120808 characters
Instance: Add a growth issue number to the database table, and generate a self-growth issue number for the current day based on different law enforcement officers.
Question No.: By Time + userid + id = 2012080200101

-- Set the first data id value of the day to 2012080800001 set @ temp_pid = convert (varchar (8), getdate (), 112) + @ in_police + '01 '; -- obtain the total number of reported problems of this executor. Select @ temp3 = count (*) from problem where police = @ in_police; If (@ temp3 <1) set @ temp1 = 0; elsebegin -- Obtain the question number of the last reported problem from the law enforcement officer. Select @ temp1 = max (PID) from problem where police = @ in_police; end -- question number + + If (@ temp_pid> @ temp1) set @ pid = @ temp_pid; else begin set @ pid = substring (@ temp1,) + 1; if (@ PID/10 = 0) set @ pid = convert (varchar (8), getdate (), 112) + @ in_police + '0' + @ PID; else set @ pid = convert (varchar (8), getdate (), 112) + @ in_police + @ PID; endbegin try insert into problem values (@ PID, @ in_maintype, @ in_subtype, @ in_carid, @ response, @ in_driver, @ in_coordinate, @ response, @ in_fare, @ response, @ in_police, @ in_status, @ in_datetime); Set @ out_parameter = 0; end trybegin catchset @ out_parameter = 2; end catch

Note:
This stored procedure is intended to insert a new problematic data.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.