0

I have problem that replace function does not work

DECLARE @Tabela nvarchar(25)
DECLARE @query nvarchar(max)

SET @Tabela = '_#tmp_tt2_POS_racuni_'
SET @query = 'SELECT * INTO '+@Tabela+((replace(convert(varchar(10), getdate(),121),'''-''',''''))+'-'+(replace(convert(nvarchar(10),getdate(),108),''':''','''')))+'NP'+' FROM _tabels'

PRINT @query
SELECT * 
INTO _#tmp_tt2_POS_racuni_2021-12-21-11:15:27NP 
FROM _tabels 

Completion time: 2021-12-21T11:15:27.0724917+01:00 
20
  • 2
    A table called _#tmp_tt2_POS_racuni_ isn't going to be temporary like it's name implies either. A temporary table's name must begin with a has (#); yours starts with an underscore (_). It seems both (read all?) your table (object) names do. I hope not. Commented Dec 21, 2021 at 10:32
  • 2
    The actual point is why create such a maintenance mess when you can handle data using the built-in features? If you can't even understand what that string formatting function does, you'll have far more serious problems going forward Commented Dec 21, 2021 at 10:37
  • 1
    Your REPLACE function is working, there's just nothing to replace. Style 121 returns a varchar in the format yyyy-mm-dd hh:mi:ss.mmm, and you want to replace the value '-' with ''. There are no '-' patterns in your string. You actually should be replacing just - with a zero length string. Commented Dec 21, 2021 at 10:37
  • 1
    Though, why not use style 112 (yyyymmdd); then you don't need to use the first 10 characters of style 121 (yyyy-mm-dd hh:mi:ss.mmm) and remove the hyphens (-). Commented Dec 21, 2021 at 10:41
  • 1
    @Nejc what format do you want? It's easier to directly produce the format you want than struggle with replacements Commented Dec 21, 2021 at 10:42

3 Answers 3

3

You should use FORMAT and specify the format you want directly instead of going through intermediate formats. For example :

select format(getdate(),'yyyyMMddhhmmss')

Produces 20211221124017. FORMAT is slower than CONVERT but in this case it's only called once. It's far more important to write a readable query that produces the correct result.

That said, it's probably better to use table partitioning instead of creating lots of temporary tables with a date in the name. All supported SQL Server versions and editions support partitioning, even LocalDB

Sign up to request clarification or add additional context in comments.

Comments

2

The quotes you use are two too many.

You are using replace(date,''':''',''''). This will replace ':' with ''. However, the getdate() doesn't have quotes itself. I guess you did that because of the dynamic sql you are using - but for the dates, you should omit the quotes:

replace(date,':','')

1 Comment

Yes this is solution. Thank you.
1

Firstly, let's get onto the real problem that is discussed at lengths in the comments; this is a terrible idea.

The fact you want to create a table for an exact point in time smells very strongly of an XY Problem. What is the real problem you are trying to solve with this? Most likely what you really want is a partitioned table or a temporal table, so that you can query the data for an exact point in time. Which you need, we don't know, but I would suggest that you rethink your "solution" here.


As for the problem, it's working exactly as intended. Let's look at your REPLACE in solitude:

replace(convert(varchar(10), getdate(),121),'''-''','''')

So, in the above, you want to replace '-' (a hyphen wrapped in single quotes) with '' (2 single quotes). You don't want to replace a hyphen (-) with a zero length string; that would be REPLACE(..., '-','').

The style you are using, 121 gives the format yyyy-mm-dd hh:mi:ss.mmm, which doesn't contain a single single quote ('), so no wonder it isn't finding the pattern.

Though you don't need REPLACE on that date at all. YOu are taking the first 10 characters or the style and then removing the hyphens (-) to get yyyyMMdd, but there is already a style for that; style 112.

The above could be rewritten as:

DECLARE @Tabela sysname;
DECLARE @query nvarchar(max);
SET @Tabela = N'_#tmp_tt2_POS_racuni_';
SET @query = N'SELECT * INTO dbo.'+QUOTENAME(CONCAT(@Tabela,CONVERT(nvarchar(8),GETDATE(),112),,N'-'.REPLACE(CONVERT(nvarchar(10),GETDATE(),108),':',''),N'',N'NP')+N' FROM dbo._tabels;'
PRINT @query;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.