select @@servername the result as PTH12345
i need substract only number
Assistant
In SQL Server, if you want to extract only the numeric portion from the result of SELECT @@SERVERNAME, you can utilize string functions to achieve that. Here's an example of how you can do this:
sql
DECLARE @ServerName NVARCHAR(100);
SET @ServerName = @@SERVERNAME;
-- Use PATINDEX to find the position of the first digit and SUBSTRING to extract numbers.
SELECT
@ServerName AS ServerName,
CAST(SUBSTRING(@ServerName, PATINDEX('%[0-9]%', @ServerName), LEN(@ServerName)) AS NVARCHAR(MAX)) AS NumberOnly
FROM
sys.objects
WHERE
1 = 0; -- Just using this to allow selection; no actual rows will be returned.
However, the approach above has limitations, especially since SQL Server doesn't have a built-in function to simply extract numbers. Therefore, if the @@SERVERNAME might contain non-numeric text before and after the numbers, a more robust approach would be to loop through the string or use a CLR function to cleanly extract numbers.
If the result of SELECT @@SERVERNAME is consistently formatted (e.g., PTH12345), and you know the format will always be the same, you can directly pull the numeric value using something like this:
sql
DECLARE @ServerName NVARCHAR(100);
SET @ServerName = @@SERVERNAME;
-- Extracting numeric part only (assuming format is consistent)
SELECT
RIGHT(@ServerName, LEN(@ServerName) - PATINDEX('%[0-9]%', @ServerName) + 1) AS NumberOnly;
Remember that these examples may need modifying if the formatting may vary. If you have control over the database setup, consider having your server name formatted in a consistent, predictable way.
User
simple, one line
Assistant
You can extract only the numeric part of the server name in a single SQL statement like this:
sql
SELECT CAST(SUBSTRING(@@SERVERNAME, PATINDEX('%[0-9]%', @@SERVERNAME), LEN(@@SERVERNAME)) AS NVARCHAR(100)) AS NumberOnly;
This one-liner extracts the numeric portion from @@SERVERNAME assuming there is at least one digit present.
To verify that the date returned by your SQL command is correct, you can run the inner SELECT statement separately to check the formatted date. The function GETDATE() returns the current date and time, and you're subtracting one day from it.
Here’s how you can check it:
Check the value in isolation: Run the inner SELECT command by itself.
sql
SELECT CONVERT(VARCHAR, GETDATE() - 1, 112) AS PreviousDate;
This query will return the date of yesterday in the YYYYMMDD format, which matches the style number 112.
Execute the full command: If you want to check if the complete command works as expected, you can execute the full EXEC command and observe its results:
Verify the output: After executing the full command, you should see output (if applicable) from Gen_ReportEjournal. Ensure that the date parameter that's passed is the one you expect. For further verification, you can also log the output to a table or display the results right away.
Example of Cross-Checking Date Format
Here’s an example showing how you might check today's and yesterday's date to ensure accuracy.
sql
-- Check today's date
SELECT CONVERT(VARCHAR, GETDATE(), 112) AS TodayDate;
-- Check yesterday's date
SELECT CONVERT(VARCHAR, GETDATE() - 1, 112) AS YesterdayDate;
This way, you can see the output of both today’s and yesterday’s dates and confirm that GETDATE() - 1 is functioning as expected.
Note:
If you're running these queries in an environment with differing time zones or settings, ensure that your server's time corresponds to what you expect it to be.
User
need to print for checking command
Assistant
If you want to print the dates for checking purposes before executing the stored procedure, you can use the PRINT statement in SQL Server. This will allow you to output the date that will be passed to the stored procedure.
Here’s how you can do that:
sql
DECLARE @BranchCode VARCHAR(10) = '11190';
DECLARE @Date VARCHAR(8);
-- Get yesterday's date in 'YYYYMMDD' format
SET @Date = CONVERT(VARCHAR, GETDATE() - 1, 112);
-- Print the values for checking
PRINT 'Branch Code: ' + @BranchCode;
PRINT 'Date (Yesterday): ' + @Date;
-- Now execute the stored procedure
EXEC STDBENGINE..Gen_ReportEjournal @BranchCode, @Date;
Explanation:
Declare Variables: We declare two variables: one for the branch code and another for the date.
Set Date: We set the date variable to the value of yesterday’s date formatted as YYYYMMDD.
Printing: The PRINT command displays the value for both the branch code and the date to the output for verification.
Execution: After printing the values for checking, the stored procedure Gen_ReportEjournal is called with the specified parameters.
By running this script, you will first see the printed values in the output, which helps you verify the correctness of the parameters before executing the command.
User
(select cast(SUBSTRING(@@servername, patindex('%[0-9]%', @@servername), LEN(@@servername)) as nvarchar(100))) for @branchcode
Assistant
If you want to assign only the numeric part of @@SERVERNAME to the variable @BranchCode, you can do this using a simple SET statement in SQL Server. Here’s how you can extract the numeric part of @@SERVERNAME and assign it to @BranchCode in a single assignment:
sql
DECLARE @BranchCode NVARCHAR(100);
SET @BranchCode = CAST(SUBSTRING(@@SERVERNAME, PATINDEX('%[0-9]%', @@SERVERNAME), LEN(@@SERVERNAME)) AS NVARCHAR(100));
-- Print it to check the value
PRINT 'Branch Code: ' + @BranchCode;
Explanation:
Declare the Variable: We declare the variable @BranchCode to hold the numeric part of the server name.
Set the Value: We use SET to assign the numeric portion of @@SERVERNAME, starting from the first occurrence of a number (found using PATINDEX) to the end of the string.
Cast to NVARCHAR: The extracted substring is cast to NVARCHAR(100) to ensure it is in the correct type.
Print for Verification: The PRINT statement outputs the value of @BranchCode, which allows you to verify that the extraction was successful.
This script will work in any T-SQL environment where you want to extract just the numeric part of the server name and verify it. If there are no digits in @@SERVERNAME, @BranchCode will be empty.
User
DECLARE @Date VARCHAR(8);
DECLARE @BranchCode NVARCHAR(100);
SET @Date = CONVERT(VARCHAR, GETDATE() - 1, 112);
SET @BranchCode = CAST(SUBSTRING(@@SERVERNAME, PATINDEX('%[0-9]%', @@SERVERNAME), LEN(@@SERVERNAME)) AS NVARCHAR(100));
EXEC STDBENGINE..Gen_ReportEjournal @BranchCode, @Date;
make it shortest or just one line
Assistant
You can combine the declarations and assignments into a single statement using a single EXEC call and inline assignments. Here’s how you can do that in a more concise manner:
The entire command is made up of a single EXEC statement.
The expressions for @BranchCode and @Date are calculated directly within the EXEC call.
This effectively eliminates the need for intermediate variable declarations, making your command as short as possible while still fulfilling the requirements.