473,503 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Format Function "0000"

Hello, i don't know how to format a string with an SQL select query
for my VB6 App.

I have a table like this :
Code - Name
1 - Jonathan
2 - Mike
....
9 - Claudia
10 - Robbie
11 - Sandy

But I would get code column result's with a particular format like
this :

0001 - Jonathan
0002 - Mike
....
0009 - Claudia
0010 - Robbie
0011 - Sandy

I use the Format(column, "#0000") function in my application ffor the
moment but nothing to do with the DB Engine side ???

I tried CONVERT function :
SELECT CONVERT(varchar(4), code, '0000') FROM Employes;
But the code result's stil 1,2,3 and not 0001,0002,0003 !!!

Anyone has the solution ?

Thanks

Jonathan

Feb 12 '07 #1
3 65551
Hi Jonathan,

Probably it is better to leave this formatting on the VB side as it is more
powerful there. But if you have to do it on the DB side, here are two ways:

SELECT REPLICATE('0', 4 - DATALENGTH(CAST(1 as varchar))) + CAST(1 as
varchar), RIGHT(CAST('0000' + CAST(1 as varchar) as varchar), 4)

In your case it will be like:

SELECT REPLICATE('0', 4 - DATALENGTH(CAST(code as varchar))) + CAST(code as
varchar), RIGHT(CAST('0000' + CAST(code as varchar) as varchar), 4)
FROM Employes

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Feb 12 '07 #2
....it has been a long day already :)

On the last one you do not need the extra CAST:

SELECT RIGHT('0000' + CAST(1 as varchar), 4)

or,

SELECT RIGHT('0000' + CAST(code as varchar), 4) FROM Employes

Plamne Ratchev
http://www.SQLStudio.com
Feb 12 '07 #3
On 12 fév, 22:17, "Plamen Ratchev" <Pla...@SQLStudio.comwrote:
...it has been a long day already :)

On the last one you do not need the extra CAST:

SELECT RIGHT('0000' + CAST(1 as varchar), 4)

or,

SELECT RIGHT('0000' + CAST(code as varchar), 4) FROM Employes

Plamne Ratchevhttp://www.SQLStudio.com
Thank you very much !!!!!!! Its works.

Jonathan

Feb 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
5042
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
17
19808
by: Nollie | last post by:
Say you have a struct: struct MYSTRUCT { int x; int y; int w; int h; };
5
3894
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
5
2846
by: Dan C Douglas | last post by:
I have just installed VS.NET 2003 on my computer. I have a project that I have been developing on VS.NET 2002. I haven't upgraded this project to VS.NET 2003 yet and I am still developing it in...
43
2680
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
4
14776
by: jason.awlt | last post by:
Greetings, I recently being bugged by the following error on my DB2. SQL0902C SQLSTATE = 58005 Reason Code = 14 This error comes out everytime i tried to insert some record to a table...
11
21056
by: walterbyrd | last post by:
My MySQL table has a field that is set as type "date." I need to get today's date, and insert it into that field. The default for that MySQL field is 2006-00-00. I know about the date()...
0
3734
by: BBHKLAM | last post by:
Can someone help out on this problem we have or someone encouter this simiar issue? We are replicated data from MVS DB2 to Oracle 10G. Lately we often getting sql1476 with sqlcode "-430". I open...
6
19158
by: nickyazura | last post by:
hello, i would like to know how to do numbering format for "0000" where each time it will generate 0001,0002,0003 and so on untill 9999. counter = "0000" counter = counter + 1
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.