vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Sorry to raise a stupid question but I tried many methods which did work. how can I conserve the initial zero when I try to convert STR(06) into string in SQL statment? It always gives me 6 instead of 06. Thanks a lot. |
| |||
| You can't "preserve" the zero. Integer 06 = Integer 6 = Integer 00000000006. If you want to convert an integer to a varchar you can prepend a 0 character to the result: SELECT '0' + CAST(06 AS VARCHAR) Returns '06'. The downside to this method is that if you do something like SELECT '0' + CAST(10 AS VARCHAR) You'll end up with '010' which may or may not be what you want. You can build on this example with the SUBSTRING function to get exactly what you really want out of it. <angellian@gmail.com> wrote in message news:1148779910.070643.296910@g10g2000cwb.googlegr oups.com... > Sorry to raise a stupid question but I tried many methods which did > work. > how can I conserve the initial zero when I try to convert STR(06) into > string in SQL statment? > It always gives me 6 instead of 06. > > Thanks a lot. > |
| |||
| You are confusing the PHYSICAL display with the internal LOGICAL model. This is SQL and not COBOL. There is no initial zero in a number; there is an internal binary, BCD or whatever the hard uses representation. Your next problem is that you do not understand that dispaly is NEVER done in the database, but in the front end application. That is the most basic concept of *any* tiered architecture, not just SQL. |
| |||
| --CELKO-- (jcelko212@earthlink.net) writes: > Your next problem is that you do not understand that dispaly is NEVER > done in the database, but in the front end application. That is the > most basic concept of *any* tiered architecture, not just SQL. Working so long as you have done in the database trade should have learnt you to never say never. There is at least one obvious case where formatting of output must be done in SQL: to wit when the display is done in a standard query tool like Query Analyzer. Which typically is the case for admin stuff. -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx |
| |||
| > This is SQL and not COBOL. There is no initial zero in a number; there > is an internal binary, BCD or whatever the hard uses representation. This is SQL SERVER not SQL and not COBOL, SQL SERVER has many facilities to aid the developer in creating a highly scalable, robust and maintainable architecture. Standard SQL is very weak in terms of features that we need out in the real world. > Your next problem is that you do not understand that dispaly is NEVER > done in the database, but in the front end application. That is the > most basic concept of *any* tiered architecture, not just SQL. "Display" can never be done in the database because the database is a service and has such has no UI, we use tools to get at the data. The big problem here is your continued misconception that ALL formatting should be done in the front end application, have you actually sat down and thought about what that means? The fundemental principle of tiered architecture design and development is that formatting is done where it is most sensible and efficient, in terms of development and support cost and in terms of performance. My blog entry on this covers in more detail: http://sqlblogcasts.com/blogs/tonyro...05/11/429.aspx I see you use CTE, why don't you pull the results down into the application, CTE's are a form of formatting for display purposes, as is COALESCE on the SELECT clause, as is ORDER BY etc... Just where do you draw the line? Anyway, you are still stuck in the mainframe model of all resources are in the same box and that you use the VTAM protocol out to remote terminals. -- Tony Rogerson SQL Server MVP http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL Server Consultant http://sqlserverfaq.com - free video tutorials "--CELKO--" <jcelko212@earthlink.net> wrote in message news:1148830663.994422.182500@y43g2000cwc.googlegr oups.com... > You are confusing the PHYSICAL display with the internal LOGICAL model. > > > This is SQL and not COBOL. There is no initial zero in a number; there > is an internal binary, BCD or whatever the hard uses representation. > > Your next problem is that you do not understand that dispaly is NEVER > done in the database, but in the front end application. That is the > most basic concept of *any* tiered architecture, not just SQL. > |
| |||
| Something like this may help...Assuming your column name is COLUMN1 and COLUMN1 has a numeric type. select case when COLUMN1 < 10 then '0' + cast(COLUMN1 as varchar(10)) end when COLUMN1 > = 10 then cast(COLUMN1 as varchar(10)) end .......(rest of your statement...) Hope this helps... |
| |||
| Hi Angellian, For 2 character string you can just use CASE... declare @number tinyint set @number = 2 select case when @number between 0 and 9 then '0' else '' end + cast( @number as varchar(2) ) Otherwise, if your resultant string needs to be bigger than 2 characters do this... declare @number int declare @string varchar(10) declare @size_of_fixed_string tinyint set @size_of_fixed_string = 10 set @number = 40 print replicate( '0', @size_of_fixed_string ) set @string = left( replicate( '0', @size_of_fixed_string ), @size_of_fixed_string - len( @number ) ) + cast( @number as varchar(10) ) print @string http://sqlblogcasts.com/blogs/tonyro...05/29/765.aspx -- Tony Rogerson SQL Server MVP http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL Server Consultant http://sqlserverfaq.com - free video tutorials <angellian@gmail.com> wrote in message news:1148779910.070643.296910@g10g2000cwb.googlegr oups.com... > Sorry to raise a stupid question but I tried many methods which did > work. > how can I conserve the initial zero when I try to convert STR(06) into > string in SQL statment? > It always gives me 6 instead of 06. > > Thanks a lot. > |
| |||
| >> There is at least one obvious case where formatting of output must be done in SQL: to wit when the display is done in a standard query tool like Query Analyzer. << No, that formatting is done in the Query Analyzer, which is a program and not part of SQL. Trust me, we never voted on a "standard query tool" in ANSI X3H2. |
| |||
| >> I see you use CTE, why don't you pull the results down into the application, CTE's are a form of formatting for display purposes, as is COALESCE on the SELECT clause, as is ORDER BY etc... Just where do you draw the line? << UNH? CTEs are virtual tables and have nothing to do with display. Do ypou also think that VIEWs and derived tables are formatting for user display? COALESCE is a function that works with NULLs and CAST() to get another internal data type result. Things like CONVERT() on dates or PRINT in T-SQL is formatting. |
| ||||
| Just cause I've seen a couple of examples using CASE; I use trick similar to your second example: SELECT RIGHT('0' +CONVERT(varchar(2), @number), 2) Granted, it only works on a two-digit number, but it saves typing. The REPLICATE idea is pretty smooth, though. Stu Tony Rogerson wrote: > Hi Angellian, > > For 2 character string you can just use CASE... > > declare @number tinyint > set @number = 2 > > select case when @number between 0 and 9 then '0' else '' end + cast( > @number as varchar(2) ) > > Otherwise, if your resultant string needs to be bigger than 2 characters do > this... > > declare @number int > declare @string varchar(10) > declare @size_of_fixed_string tinyint > set @size_of_fixed_string = 10 > set @number = 40 > > print replicate( '0', @size_of_fixed_string ) > > set @string = left( replicate( '0', @size_of_fixed_string ), > @size_of_fixed_string - len( @number ) ) + cast( @number as varchar(10) ) > > print @string > > http://sqlblogcasts.com/blogs/tonyro...05/29/765.aspx > > -- > Tony Rogerson > SQL Server MVP > http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL > Server Consultant > http://sqlserverfaq.com - free video tutorials > > > <angellian@gmail.com> wrote in message > news:1148779910.070643.296910@g10g2000cwb.googlegr oups.com... > > Sorry to raise a stupid question but I tried many methods which did > > work. > > how can I conserve the initial zero when I try to convert STR(06) into > > string in SQL statment? > > It always gives me 6 instead of 06. > > > > Thanks a lot. > > |
| Thread Tools | |
| Display Modes | |
|
|