选择包含特定条件的语句

时间:2017-10-09 21:03:19

标签: sql

我有一个包含表中这些值的列。

Colum1: dhd-29229 Table: Test
        dhd-29199
        dhd-00011

我的目标是编写一个select sql语句,它将返回以下值:所有数值而不考虑dhd -

4 个答案:

答案 0 :(得分:1)

使用like

where column1 like '%29229'

答案 1 :(得分:0)

如果你想要这种格式的XXX-29199,你可以使用:

where column1 like '___-29229'

如果您只需要29229,则可以使用:where column1 like '%29229',但在这种情况下,查询将返回以“29229”结尾的所有值。你可以考虑控制长度。

https://www.w3schools.com/sql/sql_like.asp

答案 2 :(得分:0)

在T-SQL(sql server)中: select substring(colum1,charindex(' - ',colum1)+1,5)作为test

的sub_string

答案 3 :(得分:0)

如果你想要一个严格的'仅限数字'而不回复格式或不一致...尝试编写UDF:

<article>
    <section class="experiment">
        <div class="make-center">
            <input type="text" id="room-id" value="abcdef">
            <button id="open-room">Open Room</button>
            <button id="join-room">Join Room</button>
            <button id="open-or-join-room">Auto Open Or Join Room</button>

            <br><br>
            <p>
                Use this button to share custom messages (strings, numbers, objects, whatever) among all the users on this page. Remember, all the users MUST be using same URL.
                <br>
                <button id="send-custom-message" disabled>Send Custom Message</button>
                <button onclick="sendCustomMessage(10)">Send Custom Message To 10</button>
                <button onclick="sendCustomMessage(11)">Send Custom Message To 11</button>
                <button onclick="sendCustomMessage(12)">Send Custom Message To 12</button>
            </p>

            <div id="room-urls" style="text-align: center;display: none;background: #F1EDED;margin: 15px -10px;border: 1px solid rgb(189, 189, 189);border-left: 0;border-right: 0;"></div>
        </div>

        <div id="videos-container"></div>
    </section>

    <script src="https://rtcmulticonnection.herokuapp.com/dist/RTCMultiConnection.min.js"></script>
    <script src="https://rtcmulticonnection.herokuapp.com/socket.io/socket.io.js"></script>

    <script>
        document.getElementById('open-room').onclick = function () {
            disableInputButtons();
            connection.open(document.getElementById('room-id').value, function () {
                showRoomURL(connection.sessionid);
            });
        };

        document.getElementById('join-room').onclick = function () {
            disableInputButtons();
            connection.join(document.getElementById('room-id').value);
        };

        document.getElementById('open-or-join-room').onclick = function () {
            disableInputButtons();
            connection.openOrJoin(document.getElementById('room-id').value, function (isRoomExists, roomid) {
                if (!isRoomExists) {
                    showRoomURL(roomid);
                }
            });
        };

        var chatContainer = document.querySelector('.chat-output');

        function appendDIV(event) {
            var div = document.createElement('div');
            div.innerHTML = event.data || event;
            chatContainer.insertBefore(div, chatContainer.firstChild);
            div.tabIndex = 0;
            div.focus();

            document.getElementById('input-text-chat').focus();
        }

        function sendCustomMessage(event_id) {
            var newConnection = new RTCMultiConnection();
            newConnection.socketURL = 'https://rtcmulticonnection.herokuapp.com:443/';
            newConnection.socketMessageEvent = 'custom-socket-event-demo-' + event_id;
            newConnection.session = {
                audio: true,
                video: true
            };

            newConnection.sdpConstraints.mandatory = {
                OfferToReceiveAudio: true,
                OfferToReceiveVideo: true
            };
            newConnection.socketCustomEvent = 'custom-socket-event-demo-' + event_id;
            newConnection.connectSocket(function (socket) {
                // listen custom messages from server
                socket.on(newConnection.socketCustomEvent, function (message) {
                    if (message.customMessage === 1) {
                        var customMessage = prompt('Enter test message.');
                        socket.emit(newConnection.socketCustomEvent, {
                            sender: newConnection.userid,
                            customMessage: customMessage
                        });
                    }
                    if (message.customMessage === "Yes") {
                        alert("Accepted - " + newConnection.connectionDescription.remoteUserId);
                        newConnection.peers[newConnection.connectionDescription.remoteUserId].peer.close();

                    }
                });

                // send custom messages to server
                var customMessage = prompt('Enter test message.');
                socket.emit(newConnection.socketCustomEvent, {
                    sender: newConnection.userid,
                    customMessage: customMessage
                });
            });
        }
        var connection = new RTCMultiConnection();

        connection.socketURL = 'https://rtcmulticonnection.herokuapp.com:443/';

        connection.socketMessageEvent = 'custom-socket-event-demo-<?= $_GET["id"] ?>';

        connection.session = {
            audio: true,
            video: true
        };

        connection.sdpConstraints.mandatory = {
            OfferToReceiveAudio: true,
            OfferToReceiveVideo: true
        };

        connection.videosContainer = document.getElementById('videos-container');
        connection.onstream = function (event) {
            connection.videosContainer.appendChild(event.mediaElement);
            event.mediaElement.play();
            setTimeout(function () {
                event.mediaElement.play();
            }, 5000);
        };
        connection.socketCustomEvent = 'custom-socket-event-demo-<?= $_GET["id"] ?>';

        connection.connectSocket(function (socket) {
            // listen custom messages from server
            socket.on(connection.socketCustomEvent, function (message) {
                if (message.customMessage === "1") {
                    var customMessage = prompt('Enter test message.');
                    socket.emit(connection.socketCustomEvent, {
                        sender: connection.userid,
                        customMessage: customMessage
                    });
                }
            });

            // send custom messages to server
            document.getElementById('send-custom-message').disabled = false;
            document.getElementById('send-custom-message').onclick = function () {
                var customMessage = prompt('Enter test message.');
                socket.emit(connection.socketCustomEvent, {
                    sender: connection.userid,
                    customMessage: customMessage
                });
            }
        });

        function disableInputButtons() {
            document.getElementById('open-or-join-room').disabled = true;
            document.getElementById('open-room').disabled = true;
            document.getElementById('join-room').disabled = true;
            document.getElementById('room-id').disabled = true;
        }

        function showRoomURL(roomid) {
            var roomHashURL = '#' + roomid;
            var roomQueryStringURL = '?roomid=' + roomid;

            var html = '<h2>Unique URL for your room:</h2><br>';

            html += 'Hash URL: <a href="' + roomHashURL + '" target="_blank">' + roomHashURL + '</a>';
            html += '<br>';
            html += 'QueryString URL: <a href="' + roomQueryStringURL + '" target="_blank">' + roomQueryStringURL + '</a>';

            var roomURLsDiv = document.getElementById('room-urls');
            roomURLsDiv.innerHTML = html;

            roomURLsDiv.style.display = 'block';
        }

        (function () {
            var params = {},
                    r = /([^&=]+)=?([^&]*)/g;

            function d(s) {
                return decodeURIComponent(s.replace(/\+/g, ' '));
            }
            var match, search = window.location.search;
            while (match = r.exec(search.substring(1)))
                params[d(match[1])] = d(match[2]);
            window.params = params;
        })();

        var roomid = '';
        if (localStorage.getItem(connection.socketMessageEvent)) {
            roomid = localStorage.getItem(connection.socketMessageEvent);
        } else {
            roomid = connection.token();
        }
        document.getElementById('room-id').value = roomid;
        document.getElementById('room-id').onkeyup = function () {
            localStorage.setItem(connection.socketMessageEvent, this.value);
        };

        var hashString = location.hash.replace('#', '');
        if (hashString.length && hashString.indexOf('comment-') == 0) {
            hashString = '';
        }

        var roomid = params.roomid;
        if (!roomid && hashString.length) {
            roomid = hashString;
        }

        if (roomid && roomid.length) {
            document.getElementById('room-id').value = roomid;
            localStorage.setItem(connection.socketMessageEvent, roomid);

            // auto-join-room
            (function reCheckRoomPresence() {
                connection.checkPresence(roomid, function (isRoomExists) {
                    if (isRoomExists) {
                        connection.join(roomid);
                        return;
                    }

                    setTimeout(reCheckRoomPresence, 5000);
                });
            })();

            disableInputButtons();
        }
    </script>
</article>

因此使用它......

Create FUNCTION [test].[numOnly]
(@text nvarchar (max))
RETURNS nvarchar (max)
AS
BEGIN

    DECLARE 
    @char nvarchar(1),
    @i_idx int,
    @temp int,
    @out nvarchar(max)

    While len(@text) > 0
    Begin
        Set @char = left(@text,1)
            if ascii(@char) between 49 and 57
                set @out = isnull(@out,'') + @char

            Set @text = RIGHT(@text,len(@text)-1)
    End

    Return @out
END
  

结果:445643