字符串中的字符位置编号

时间:2019-06-18 07:55:51

标签: sql

尝试从值中获取-“某种类型的tex @ 123fpe2” @符号后的所有字符 准备下面的代码 但是问题在于,符号@ 8个字符后的值的长度并不总是

select REVERSE(Left(Reverse(vValue), 8)),
(
select vValue from Runtime.dbo.Live where TagName = 'CurrentBaseRecipeName32000000io') as ValueForCheck,

from Runtime.dbo.Live where TagName = 'CurrentBaseRecipeName32000000io'

获取错误:

select REVERSE(Left(Reverse(vValue), POSITION('@' IN vValue))),
(
select vValue ...

select REVERSE(Left(Reverse(vValue), 8)),
(
select vValue from Runtime.dbo.Live where TagName = 'CurrentBaseRecipeName32000000io') as ValueForCheck,

from Runtime.dbo.Live where TagName = 'CurrentBaseRecipeName32000000io'

2 个答案:

答案 0 :(得分:1)

如果您确定字符串中只有1个@,或者如果有多个,并且您想在最后一次出现@之后使用字符串的那一部分,则可以使用SUBSTRING_INDEX()

SELECT SUBSTRING_INDEX('some kind of tex @123fpe2', '@', -1);

将返回:

123fpe2

答案 1 :(得分:0)

我认为这是MySQL,因为在问题中使用了POSITION。使用INSTR获取“ @”的位置,然后使用SUBSTRING返回由INSTR + 1返回的索引之后的所有内容

SELECT RIGHT(vValue,  LEN(vValue) - CHARINDEX('@', vValue)) FROM Runtime.dbo.Live
WHERE ...

以防万一,这是使用RIGHT的SQL Server版本

namespace push
{
    public partial class pushios : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
             SendPushNotification(txtDeviceToken.Text, txtMessage.Text);
        }
        private void  SendPushNotification(string deviceToken,string message)
        {
               try
        {
            //Get Certificate
            var appleCert = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Files/Certificate/IOS/Production_Certificate.p12"));
            // Configuration (NOTE: .pfx can also be used here)
            var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, appleCert, "1234567890");
            // Create a new broker
            var apnsBroker = new ApnsServiceBroker(config);
            // Wire up events
            apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
            {
                aggregateEx.Handle(ex =>
                {
                    // See what kind of exception it was to further diagnose
                    if (ex is ApnsNotificationException)
                    {
                        var notificationException = (ApnsNotificationException)ex;
                        // Deal with the failed notification
                        var apnsNotification = notificationException.Notification;
                        var statusCode = notificationException.ErrorStatusCode;
                        string desc = $"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}";
                        Console.WriteLine(desc);
                        Label1.Text = desc;
                    }
                    else
                    {
                        string desc = $"Apple Notification Failed for some unknown reason : {ex.InnerException}";
                        // Inner exception might hold more useful information like an ApnsConnectionException           
                        Console.WriteLine(desc);
                        Label1.Text = desc;
                    }
                    // Mark it as handled
                    return true;
                });
            };
            apnsBroker.OnNotificationSucceeded += (notification) =>
            {
                Label1.Text = "Apple Notification Sent successfully!";
            };
            var fbs = new FeedbackService(config);
            fbs.FeedbackReceived += (string devicToken, DateTime timestamp) =>
            {

            };

            apnsBroker.Start();
            if (deviceToken != "")
            {
                apnsBroker.QueueNotification(new ApnsNotification
                {
                    DeviceToken = deviceToken,
                    Payload = JObject.Parse(("{\"aps\":{\"badge\":1,\"sound\":\"oven.caf\",\"alert\":\"" + (message + "\"}}")))
                });
            }
            apnsBroker.Stop();
        }
        catch (Exception)
        {
            throw;
        }
        }
    }