Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7535869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:25:02+00:00 2026-05-30T06:25:02+00:00

This is my test table: CREATE TABLE [General].[Test] ( [Name] NVARCHAR(20) NOT NULL, [SSN]

  • 0

This is my test table:

CREATE TABLE [General].[Test]
(
[Name] NVARCHAR(20) NOT NULL,
[SSN] CHAR(10) NOT NULL,

CONSTRAINT [UK_Test_SSN] 
    UNIQUE NONCLUSTERED ([SSN]) WITH (IGNORE_DUP_KEY = OFF),
)

And I inserted some values:

INSERT INTO [General].[Test] ([SSN], [Name]) VALUES (N'901223476', N'Lol1'); 
INSERT INTO [General].[Test] ([SSN], [Name]) VALUES (N'2591830061', N'Lol2'); 
INSERT INTO [General].[Test] ([SSN], [Name]) VALUES (N'4431776273', N'Lol3'); 
INSERT INTO [General].[Test] ([SSN], [Name]) VALUES (N'987654321', N'Lol4');
INSERT INTO [General].[Test] ([SSN], [Name]) VALUES (N'123456789', N'Lol5');
INSERT INTO [General].[Test] ([SSN], [Name]) VALUES (N'0123456789', N'Lol6');
INSERT INTO [General].[Test] ([SSN], [Name]) VALUES (N'0012345678', N'Lol7');
INSERT INTO [General].[Test] ([SSN], [Name]) VALUES (N'123', N'Lol8');

And the first SELECT query:

SELECT
CASE 
  WHEN [T].[SSN] LIKE REPLICATE('[0-9]',10) THEN [T].[SSN] 
  ELSE 1000000000 + ROW_NUMBER() OVER(ORDER BY [T].[Name]) END AS [SSN]
FROM [General].[Test] AS [T];
GO

And the second query:

SELECT [T].[SSN] FROM [General].[Test] AS [T];

The ambiguity that I can’t understand is about rows 6 and 7 whose [SSN] values start with 0

So for example about 6th row the first query returns 123456789 and second query return 0123456789, can anyone explain why? And I really need to get the real value of 0123456789 in the first query, what can I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:25:03+00:00Added an answer on May 30, 2026 at 6:25 am

    You have two different datatypes being returned from your first column in the first query – in the first case, the CHAR(10) of the [SSN] is returned, in the ELSE case a large INT number. That’s why SQL Server needs to convert one of the two datatypes to the other.

    What you need to do, if you want to get back the CHAR(10), is to CAST the second expression to that datatype:

    SELECT
       CASE 
          WHEN [T].[SSN] LIKE REPLICATE('[0-9]', 10) THEN [T].[SSN] 
          ELSE CAST(1000000000 + ROW_NUMBER() OVER(ORDER BY [T].[Name]) AS CHAR(10)) 
       END
    FROM [General].[Test] AS [T];
    

    Then you get back your CHAR(10) values – and nothing gets converted to a INT (or BIGINT) and thus looses it’s leading zeroes:

    (No column name)
    1000000001
    2591830061
    4431776273
    1000000004
    1000000005
    0123456789
    0012345678
    1000000008
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: create table `test` ( `id` bigint(20) NOT NULL
I created a simple test case: CREATE TABLE `t1` ( `id` int NOT NULL
When I have the following table: CREATE TABLE test ( id integer NOT NULL,
Why is this thing not working? [Database(Name=Relationships_Test)] [Table(Name = Order)] public class Order {
After trying everything I could, I finally created this test table: CREATE TABLE test_table
I have a test table like this CREATE TABLE #temp ( rid INT IDENTITY
If I have the following SQL query CREATE TABLE #t1 (a INT NOT NULL
I have a table that simplified looks like this: create table Test ( ValidFrom
I have a table that's created like this: CREATE TABLE bin_test (id INTEGER PRIMARY
I have a table which stores test results like this: user | score |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.