Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Replacing the ACCESS function 'Format' with SQL server equivalent

Posted on 2003-03-06
5
Medium Priority
?
320 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
  • 2
  • 2
5 Comments
 
LVL 70

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 70

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

572 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