?
Solved

Replacing the ACCESS function 'Format' with SQL server equivalent

Posted on 2003-03-06
5
Medium Priority
?
314 Views
Last Modified: 2006-11-17
I am porting some Access sql to SQL Server. I am running into problems with code of the format “FORMAT(ORG_UNIT_CD,'0000') as orgunitcd” which is in the Select clause.

In SQLSERVER the ORG_UNIT_CD has a datatype of varchar and length 4.

In Access the datatype is Text and FieldSize is 4.

Can somebody suggest the equivalent for it?

Thanks,
Square One
0
Comment
Question by:SquareOne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 80 total points
ID: 8082819
If the format means use leading zeros, you can do this:

SELECT RIGHT('0000' + org_unit_cd, 4)
FROM ...


If you will need this in more than one or two queries, I suggest making it a computed column that can be directly referenced in the query; for example:

ALTER TABLE tableName
ADD org_unit_cd_0 AS RIGHT('0000' + org_unit_cd, 4)
--or whatever col name you want to use


Then:

SELECT org_unit_cd_0
0
 
LVL 1

Expert Comment

by:wdalton
ID: 8083525
basically the solution of ScottPletcher is correct, but just 1 remark:
it will probably return an error if org_unit_id is a numeric datatype
So the correct solution would be : SELECT RIGHT('0000' + convert(varchar(4),org_unit_cd), 4)

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 8083557
Yes, it would return an error if it were INT/NUMERIC, but the original post stated this:

"In SQLSERVER the ORG_UNIT_CD has a datatype of varchar"
0
 
LVL 1

Expert Comment

by:wdalton
ID: 8083616
You're right Scott.
in this case, your solution is 100% correct.
0
 

Author Comment

by:SquareOne
ID: 8083886
Thanks Guys ! I am giving the points to Scott, your solution worked for me.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question